Skip to content

Commit

Permalink
Add Bash support (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion authored May 23, 2023
1 parent 6e29fac commit 6608789
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ ln -s "$ZSH_CUSTOM/themes/gokart-prompt/gokart.zsh-theme" "$ZSH_CUSTOM/themes/go
Set `ZSH_THEME="gokart"` in your `.zshrc`.
</details>

<details>
<summary>Bash</summary>

Download and extract the latest release:

```shell
curl -fsSL https://github.com/LucaScorpion/gokart-prompt/releases/latest/download/gokart-prompt.tar.gz | tar xzvf - -C "$HOME"
```

Source `gokart.bash-theme` in your `.bashrc`:

```shell
source "$HOME/gokart-prompt/gokart.bash-theme"
```
</details>

## Requirements ✅

### [Powerline](https://github.com/powerline/fonts) or [Nerd Font](https://www.nerdfonts.com)
Expand Down
3 changes: 3 additions & 0 deletions cmd/gokart.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func ps1() {
fmt.Print(git.Git())
fmt.Print(versions.All(wdFiles))
fmt.Print(ansi.Reset())

fmt.Println()
ps2()
}

func ps2() {
Expand Down
17 changes: 17 additions & 0 deletions gokart.bash-theme
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

GOKART_PROMPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"

gokart_prompt_precmd() {
export EXIT_CODE=$?
export GOKART_SHELL=bash

PS1=$("$GOKART_PROMPT_DIR/gokart" ps1)
PS2=$("$GOKART_PROMPT_DIR/gokart" ps2)
}

gokart_prompt_setup() {
PROMPT_COMMAND=gokart_prompt_precmd
}

gokart_prompt_setup
7 changes: 3 additions & 4 deletions gokart.zsh-theme
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env zsh

# Get the path to this file, make it absolute and strip the filename.
# See: https://stackoverflow.com/questions/9901210/bash-source0-equivalent-in-zsh/28336473#28336473
# See: https://stackoverflow.com/questions/9901210/bash-source0-equivalent-in-zsh/28336473#28336473
GOKART_PROMPT_DIR=${${(%):-%x}:A:h}

gokart_prompt_precmd() {
export EXIT_CODE=$?
export GOKART_SHELL=zsh

PS1=$("$GOKART_PROMPT_DIR/gokart" ps1)
PS2=$("$GOKART_PROMPT_DIR/gokart" ps2)

local NEWLINE=$'\n'
PROMPT="${PS1}${NEWLINE}${PS2}"
}

gokart_prompt_setup() {
Expand Down
19 changes: 16 additions & 3 deletions internal/ansi/ansi.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package ansi

import "os"

const esc = "\x1B"
const reset = "0"

// See: https://stackoverflow.com/a/19731390/2831123
const zeroWidthStart = "%{"
const zeroWidthEnd = "%}"
const zshZeroWidthStart = "%{"
const zshZeroWidthEnd = "%}"

// See: https://unix.stackexchange.com/a/504152/176402
const bashZeroWidthStart = "\x01"
const bashZeroWidthEnd = "\x02"

func mode(mode string) string {
return zeroWidthStart + esc + "[" + mode + "m" + zeroWidthEnd
return zeroWidth(esc + "[" + mode + "m")
}

func Reset() string {
return mode(reset)
}

func zeroWidth(s string) string {
if os.Getenv("GOKART_SHELL") == "zsh" {
return zshZeroWidthStart + s + zshZeroWidthEnd
}
return bashZeroWidthStart + s + bashZeroWidthEnd
}

0 comments on commit 6608789

Please sign in to comment.