Skip to content
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

Portable shell wrappers #53

Merged
merged 14 commits into from
Aug 7, 2022
69 changes: 44 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,70 @@ To start using `tere`, follow these steps:
- Install `tere` with [Cargo](https://www.rust-lang.org/tools/install) by running `cargo install tere`.
- Build from source, see [below](#hacking).

1. Configure your shell to `cd` to the folder which `tere` prints when it exits. It has to be usually done using a function or alias, since a subprocess cannot change the working directory of the parent.
1. Configure your shell to `cd` to the folder which `tere` prints when it exits. It has to be usually done using a function or alias, since a subprocess cannot change the working directory of the parent. See instructions for your shell below.

For bash/zsh, put this in your `.bashrc` or `.zshrc`:
<details>
<summary>Bash/Zsh</summary>

Put this in your `.bashrc` or `.zshrc`:

```sh
tere() {
local result=$(/path/to/tere "$@")
local result=$(command tere "$@")
[ -n "$result" ] && cd -- "$result"
}
```
</details>

<details>
<summary>fish</summary>

Put this in your `config.fish`:

```sh
function tere
set --local result (command tere $argv)
[ -n "$result" ] && cd -- "$result"
end
```
</details>

For xonsh v0.10 or newer, put this in your `.xonshrc`:
<details>
<summary>Xonsh</summary>

Put this in your `.xonshrc` (Xonsh v0.10. or newer is required):

```py
def _tere(args):
result = $(/path/to/tere @(args)).strip()
result = $(tere @(args)).strip()
if result:
cd @(result)

aliases["tere"] = _tere
```
</details>

For fish, put this in your `.config.fish`:
```sh
function tere
set --local result (/path/to/tere $argv)
[ -n "$result" ] && cd -- "$result"
end
```
<details>
<summary>PowerShell</summary>

For powershell core, put this in your `$PROFILE`:
```sh
Put this in your `$PROFILE`:

```powershell
function Invoke-Tere() {
$tere_dir = '/path/to/tere'
if ($isWindows) {
$tere_path = Join-Path $tere_dir 'tere.exe'
}
else {
$tere_path = Join-Path $tere_dir 'tere'
}
$result = . $tere_path
$result = . (Get-Command -CommandType Application tere) $args
if ($result) {
Set-Location $result
}
}
Set-Alias tere Invoke-Tere
```
```
</details>

<details>
<summary>Windows Command Prompt (CMD)</summary>

Put this in a batch script file called `tere.bat` in a folder included in your `PATH` environment variable such as `C:\Windows`:

For Windows Command Prompt, put this in a batch script file called `tere.bat` in a folder included in your `PATH` environment variable such as `C:\Windows`:
```batch
@echo off

Expand All @@ -88,7 +103,11 @@ To start using `tere`, follow these steps:
FOR /F "tokens=*" %%a in ('%TereEXE% %*') do SET OUTPUT=%%a
IF [%OUTPUT%] == [] goto :EOF
cd %OUTPUT%
```
```
Note that if you want to make `tere` work with *both* PowerShell and CMD, you should *not* put `tere.exe` to a location that is in your `PATH`, because then the `.exe` will be run instead of the `.bat`. Place `tere.exe` somewhere that is not in your `PATH`, and use the full path to the exe in both the `.bat` file and in the PowerShell `$PROFILE`.
</details>

If `tere` is not in your `PATH`, use an absolute path to the tere binary in your shell config file. For example, for Bash/Zsh, you would need to replace `local result=$(command tere "$@")` with `local result=$(/path/to/tere "$@")`, or for PowerShell, replace `(Get-Command -CommandType Application tere)` with `C:\path\to\tere.exe`.

If instructions for your shell are missing, feel free to send a pull request that includes them!

Expand Down