Skip to content

Powershell snippet #1453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 67 additions & 11 deletions .github/wiki/Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Onefetch is installed, then what?
```sh
> onefetch /path/of/your/repo
```

Or

```sh
> cd /path/of/your/repo
> onefetch
Expand All @@ -15,38 +16,50 @@ Onefetch is installed, then what?
### Misc

By [**@spenserblack**](https://github.com/spenserblack)

```sh
# Runs `onefetch -a Assembly`, `onefetch -a C`, etc.
onefetch -l | tr "[:upper:] " "[:lower:]-" | while read line; do echo "$line"; onefetch -a $line; done;
```

### Automatic repo detection and running

If you want to automate the detection and running of `onefetch` every time you `cd` into a repository you can leverage one of the methods below:

#### 1. Bash / Zsh

By [**@quazar-omega**](https://github.com/quazar-omega)

A script to put in your `.bashrc` - or `.zshrc` - to run onefetch whenever you open a shell into a repository or `cd` into a repository, making sure that it's different from the last one you were in:

```sh
# git repository greeter
last_repository=
check_directory_for_new_repository() {
current_repository=$(git rev-parse --show-toplevel 2> /dev/null)
if [ "$current_repository" ] && \
[ "$current_repository" != "$last_repository" ]; then
onefetch
fi
last_repository=$current_repository
current_repository=$(git rev-parse --show-toplevel 2> /dev/null)
if [ "$current_repository" ] && \
[ "$current_repository" != "$last_repository" ]; then
onefetch
fi
last_repository=$current_repository
}
cd() {
builtin cd "$@"
check_directory_for_new_repository
builtin cd "$@"
check_directory_for_new_repository
}

# optional, greet also when opening shell directly in repository directory
# adds time to startup
check_directory_for_new_repository
```

#### 2. Fish

By [**@TheSast**](https://github.com/TheSast)

A fish adaptation of the previous script, run it once in your shell to save it:

```fish
function cd -w='cd'
builtin cd $argv || return
Expand All @@ -66,11 +79,14 @@ funcsave cd
funcsave check_directory_for_new_repository
```

#### 3. CMD

By [**@mataha**](https://github.com/mataha)

An adaptation of the above snippet suited for Windows's `cmd.exe`,
specifically for inclusion in AutoRun scripts or DOSKEY macrofiles:
```batchfile

```bat
@set LAST_REPOSITORY=

@doskey cd = ( ^
Expand Down Expand Up @@ -102,7 +118,47 @@ specifically for inclusion in AutoRun scripts or DOSKEY macrofiles:
)
```

#### 4. Powershell

By [**@kiapanahi**](https://github.com/kiapanahi)

An adaptation of the above snippet suited for `Powershell`. Put this script in the `$PROFILE`.

```pwsh
# git repository greeter
$global:lastRepository = $null

function Check-DirectoryForNewRepository {
$currentRepository = git rev-parse --show-toplevel 2>$null
if ($currentRepository -and ($currentRepository -ne $global:lastRepository)) {
onefetch
}
$global:lastRepository = $currentRepository
}

function Set-Location {
param (
[string]$path
)

# Use the default Set-Location to change the directory
Microsoft.PowerShell.Management\Set-Location -Path $path

# Check if we are in a new Git repository
Check-DirectoryForNewRepository
}

# Optional: Check the repository also when opening a shell directly in a repository directory
# Uncomment the following line if desired
#Check-DirectoryForNewRepository
```

### Git alias

By [**@mbrehin**](https://github.com/mbrehin)

You can also add git alias to run onefetch during your git workflows

```sh
# Add Git alias for onefetch.
git config --global alias.project-summary '!which onefetch && onefetch'
Expand Down