Skip to content

Commit 1b36b97

Browse files
authored
feat: rename none to reset and add docs (#48)
1 parent 2f64291 commit 1b36b97

File tree

9 files changed

+204
-23
lines changed

9 files changed

+204
-23
lines changed

README.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,187 @@ Add git-prompt-string to your prompt. For example,
3636
PROMPT_COMMAND='PS1="\[\n \e[0;33m\w\e[0m$(git-prompt-string)\n \e[0;32m\u@local \e[0;36m\$\e[0m \]"'
3737
```
3838

39+
## Configuration
40+
41+
### Nerd Font
42+
43+
By default, the powerline icon  is used as a prefix in the prompt. It is recommended to use a [Nerd Font](https://www.nerdfonts.com/)
44+
to properly display the  (nf-pl-branch) icon. See https://www.nerdfonts.com/ to download a Nerd Font. If you
45+
do not want this symbol, replace the prompt prefix with " ". For example, add the following to you git-prompt-string
46+
configuration.
47+
48+
```toml
49+
prompt_prefix = ' '
50+
```
51+
52+
### Configuration file
53+
54+
git-prompt-string will first check if the `--config` option was passed as an argument. If
55+
`--config` is set, the filepath defined in the value will be used as the configuration
56+
file.
57+
58+
If `--config` is not set, then git-prompt-string will check if the environment variable
59+
`$GIT_PROMPT_STRING_CONFIG` is set. If `$GIT_PROMPT_STRING_CONFIG` is set, the
60+
filepath defined in the value will be used as the configuration file.
61+
62+
If `$GIT_PROMPT_STRING_CONFIG` is not set, then git-prompt-string will check if the environment
63+
variable `$XDG_CONFIG_HOME` is set. If `$XDG_CONFIG_HOME` is set, the directory defined in then
64+
value will be used as the base directory for git-prompt-string configurations. See
65+
[XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
66+
for more information on XDG environment variables.
67+
68+
If `$XDG_CONFIG_HOME` is not set, then `~/.config` and `~/AppData/Local` will be used as the
69+
base directory for Unix and Windows, respectively.
70+
71+
The file defined at `git-prompt-string/config.toml` in the base directory will be used to configure
72+
git-prompt-string.
73+
74+
| OS | Base directory | Configuration file |
75+
| :------ | :-------------- | :-------------------------------------------- |
76+
| Unix | ~/.config | ~/.config/git-prompt-string/config.toml |
77+
| Windows | ~/AppData/Local | ~/AppData/Local/git-prompt-string/config.toml |
78+
79+
If the configuration filepath is set to the special value of `NONE`, then all user configurations will
80+
be ignored. For example, `git-prompt-string --config=NONE` or `GIT_PROMPT_STRING_CONFIG=NONE git-prompt-string`
81+
will use the default configuration values defined by git-prompt-string.
82+
83+
### Configuration options
84+
85+
The following configuration options are available in either as a command-line argument or TOML key.
86+
87+
```text
88+
--ahead-format or ahead_format
89+
The format used to indicate the number of commits ahead of the
90+
remote branch. The %v verb represents the number of commits
91+
ahead. One %v verb is required. (default "↑[%v]")
92+
93+
--behind-format or behind_format
94+
The format used to indicate the number of commits behind the
95+
remote branch. The %v verb represents the number of commits
96+
behind. One %v verb is required. (default "↓[%v]")
97+
98+
--color-clean or color_clean
99+
The color of the prompt when the working directory is clean.
100+
(default "green")
101+
102+
--color-delta or color_delta
103+
The color of the prompt when the local branch is ahead, behind,
104+
or has diverged from the remote branch. (default "yellow")
105+
106+
--color-dirty or color_dirty
107+
The color of the prompt when the working directory has changes
108+
that have not yet been committed. (default "red")
109+
110+
--color-disabled or color_disabled
111+
Disable all colors in the color-disabled
112+
113+
--color-merging or color_merging
114+
The color of the prompt during a merge, rebase, cherry-pick,
115+
revert, or bisect. (default "blue")
116+
117+
--color-no-upstream or color_no_upstream
118+
The color of the prompt when there is no remote upstream branch.
119+
(default "bright-black")
120+
121+
--color-untracked or color_untracked
122+
The color of the prompt when there are untracked files in the
123+
working directory. (default "magenta")
124+
125+
--diverged-format or diverged_format
126+
The format used to indicate the number of commits diverged
127+
from the remote branch. The first %v verb represents the number
128+
of commits ahead of the remote branch. The second %v verb
129+
represents the number of commits behind the remote branch. Two
130+
%v verbs are required. (default "↕ ↑[%v] ↓[%v]")
131+
132+
--no-upstream-remote-format or no_upstream_remote_format
133+
The format used to indicate when there is no remote upstream,
134+
but there is still a remote branch configured. The first %v
135+
represents the remote repository. The second %v represents the
136+
remote branch. Two %v are required. (default " → %v/%v")
137+
138+
--prompt-prefix or prompt_prefix
139+
A prefix that is added to the beginning of the prompt. The
140+
powerline icon  is used be default. It is recommended to
141+
use a Nerd Font to properly display the  (nf-pl-branch) icon.
142+
See https://www.nerdfonts.com/ to download a Nerd Font. If you
143+
do not want this symbol, replace the prompt prefix with " ".
144+
\ue0a0 is the unicode representation of . (default " \ue0a0 ")
145+
146+
--prompt-suffix or prompt_suffix
147+
A suffix that is added to the end of the prompt.
148+
```
149+
150+
### Specifying colors
151+
152+
A color value in the configuration must be either a single color or multiple colors
153+
separated by white space.
154+
155+
Valid formats for a color are:
156+
- `color`
157+
- fg:`color`
158+
- bg:`color`
159+
- #ffffff
160+
- #fg:ffffff
161+
- #bg:ffffff
162+
- `reset`
163+
164+
The value `reset` will clear all text formatting and reset the color to the default value.
165+
Colors starting with `bg` or `#bg` are background colors. All other formats are considered
166+
foreground colors. i.e., `red` is equivalent to `fg:reg`.
167+
168+
Colors starting with `#` are considered a hex color code and must have 6 digits.
169+
170+
Valid colors are defined in the following table.
171+
172+
| Color | Code |
173+
| :------------- | :-- |
174+
| black | 0 |
175+
| red | 1 |
176+
| green | 2 |
177+
| yellow | 3 |
178+
| blue | 4 |
179+
| magenta | 5 |
180+
| cyan | 6 |
181+
| white | 7 |
182+
| bright-black | 8 |
183+
| bright-red | 9 |
184+
| bright-green | 10 |
185+
| bright-yellow | 11 |
186+
| bright-blue | 12 |
187+
| bright-magenta | 13 |
188+
| bright-cyan | 14 |
189+
| bright-white | 15 |
190+
191+
The following are examples of valid color configurations:
192+
193+
```toml
194+
color_clean='#e5ee04'
195+
color_no_upstream="reset fg:black bg:white"
196+
color_dirty="bg:#b30559"
197+
color_delta="fg:#fcb728"
198+
color_untracked="fg:#ff0000 bg:#16f2aa"
199+
color_merging="bg:#ccccff magenta"
200+
```
201+
202+
### Default configuration
203+
204+
```toml
205+
prompt_prefix = ''
206+
prompt_suffix = ''
207+
ahead_format = '↑[%v]'
208+
behind_format = '↓[%v]'
209+
diverged_format = '↕ ↑[%v] ↓[%v]'
210+
no_upstream_remote_format = ' → %v/%v'
211+
color_disabled = false
212+
color_clean = 'green'
213+
color_delta = 'yellow'
214+
color_dirty = 'red'
215+
color_untracked = 'magenta'
216+
color_no_upstream = 'bright-black'
217+
color_merging = 'blue'
218+
```
219+
39220
## 📌 Alternatives
40221
- [git-prompt.sh](https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh) - bash/zsh git prompt support
41222
- [bash-git-prompt](https://github.com/magicmonty/bash-git-prompt) - An informative and fancy bash prompt for Git users

integration/gps_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010
)
1111

12-
func TestGPS(t *testing.T) {
12+
func TestGitPromptString(t *testing.T) {
1313
tests := []struct {
1414
dir string
1515
input []string
@@ -63,7 +63,7 @@ func TestGPS(t *testing.T) {
6363

6464
// color overrides
6565
{"clean", []string{"--config=../configs/color_overrides.toml"}, "\x1b[38;2;230;238;4m \ue0a0 main\x1b[0m", nil, nil},
66-
{"no_upstream", []string{"--config=../configs/color_overrides.toml"}, "\x1b[30m\x1b[47m \ue0a0 main\x1b[0m", nil, nil},
66+
{"no_upstream", []string{"--config=../configs/color_overrides.toml"}, "\x1b[0m\x1b[30m\x1b[47m \ue0a0 main\x1b[0m", nil, nil},
6767
{"dirty", []string{"--config=../configs/color_overrides.toml"}, "\x1b[48;2;179;5;89m \ue0a0 main *\x1b[0m", nil, nil},
6868
{"conflict_ahead", []string{"--config=../configs/color_overrides.toml"}, "\x1b[38;2;252;183;40m \ue0a0 main ↑[1]\x1b[0m", nil, nil},
6969
{"untracked", []string{"--config=../configs/color_overrides.toml"}, "\x1b[38;2;255;0;0m\x1b[48;2;22;242;170m \ue0a0 main *\x1b[0m", nil, nil},

integration/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
2626
cmd := exec.Command("go", "build", "-o", builtBinaryPath, "..")
2727
output, err := cmd.CombinedOutput()
2828
if err != nil {
29-
panic(fmt.Sprintf("failed to build gps: %s, %s", output, err))
29+
panic(fmt.Sprintf("failed to build git-prompt-string: %s, %s", output, err))
3030
}
3131

3232
copyCommand, copyArgs := copyTestDataCmd("..", tmpDir)

main.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func header() string {
4747
}
4848

4949
func main() {
50-
cfg := config.GPSConfig{
50+
cfg := config.GitPromptStringConfig{
5151
PromptPrefix: *promptPrefix,
5252
PromptSuffix: *promptSuffix,
5353
AheadFormat: *aheadFormat,
@@ -86,14 +86,14 @@ func main() {
8686

8787
flag.Parse()
8888

89-
var gpsConfig string
90-
gpsConfigEnv := os.Getenv("GIT_PROMPT_STRING_CONFIG")
89+
var cfgPath string
90+
cfgEnv := os.Getenv("GIT_PROMPT_STRING_CONFIG")
9191
if *configPath == "" {
92-
gpsConfig = gpsConfigEnv
92+
cfgPath = cfgEnv
9393
} else {
94-
gpsConfig = *configPath
94+
cfgPath = *configPath
9595
}
96-
if gpsConfig == "" {
96+
if cfgPath == "" {
9797
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
9898
if xdgConfigHome == "" {
9999
home, err := os.UserHomeDir()
@@ -102,20 +102,20 @@ func main() {
102102
}
103103
xdgConfigHome = path.Join(home, util.XDGConfigPath)
104104
}
105-
gpsConfig = path.Join(xdgConfigHome, "git-prompt-string", "config.toml")
105+
cfgPath = path.Join(xdgConfigHome, "git-prompt-string", "config.toml")
106106
}
107107

108-
if gpsConfig != "NONE" {
109-
gpsConfigRaw, err := os.ReadFile(gpsConfig)
108+
if cfgPath != "NONE" {
109+
cfgBytes, err := os.ReadFile(cfgPath)
110110
if err != nil && !os.IsNotExist(err) {
111111
util.ErrMsg("read config exists", err)
112112
}
113113

114-
if err != nil && (*configPath != "" || gpsConfigEnv != "") {
114+
if err != nil && (*configPath != "" || cfgEnv != "") {
115115
util.ErrMsg("read config", err)
116116
}
117117

118-
err = toml.Unmarshal(gpsConfigRaw, &cfg)
118+
err = toml.Unmarshal(cfgBytes, &cfg)
119119
if err != nil {
120120
util.ErrMsg("unmarshal config", err)
121121
}
@@ -168,9 +168,9 @@ func main() {
168168
os.Exit(0)
169169
}
170170

171-
clearColor, err := color.Color("none")
171+
resetColor, err := color.Color("reset")
172172
if err != nil {
173-
util.ErrMsg("color none", err)
173+
util.ErrMsg("color reset", err)
174174
}
175175

176176
gitRepo, stderr, err := git.RevParse()
@@ -194,5 +194,5 @@ func main() {
194194
util.ErrMsg("branch status", err)
195195
}
196196

197-
fmt.Printf("%s%s%s%s%s%s", promptColor, cfg.PromptPrefix, branchInfo, branchStatus, cfg.PromptSuffix, clearColor)
197+
fmt.Printf("%s%s%s%s%s%s", promptColor, cfg.PromptPrefix, branchInfo, branchStatus, cfg.PromptSuffix, resetColor)
198198
}

pkg/color/color.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var standardColors = map[string]string{
7171
"bg:bright-cyan": codeToEscapeSequence(106),
7272
"bg:bright-white": codeToEscapeSequence(107),
7373

74-
"none": codeToEscapeSequence(0),
74+
"reset": codeToEscapeSequence(0),
7575
}
7676

7777
func hexToRGB(hex string) (int, int, int, error) {

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package config
22

3-
type GPSConfig struct {
3+
type GitPromptStringConfig struct {
44
PromptPrefix string `toml:"prompt_prefix"`
55
PromptSuffix string `toml:"prompt_suffix"`
66
AheadFormat string `toml:"ahead_format"`

pkg/git/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (g *GitRepo) ReadGitDirFileExitOnError(name string) string {
7171
return content
7272
}
7373

74-
func (g *GitRepo) BranchInfo(cfg config.GPSConfig) (string, error) {
74+
func (g *GitRepo) BranchInfo(cfg config.GitPromptStringConfig) (string, error) {
7575
var err error
7676
ref := ""
7777
step := ""
@@ -190,7 +190,7 @@ func (g *GitRepo) BranchInfo(cfg config.GPSConfig) (string, error) {
190190
return prompt, nil
191191
}
192192

193-
func (g *GitRepo) BranchStatus(cfg config.GPSConfig) (string, string, error) {
193+
func (g *GitRepo) BranchStatus(cfg config.GitPromptStringConfig) (string, string, error) {
194194
status := ""
195195
statusColor := ""
196196

pkg/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func ReadFileTrimNewline(name string) (string, error) {
3636

3737
func ErrMsg(hint string, e error) {
3838
errorColor, _ := color.Color("red")
39-
clearColor, _ := color.Color("none")
39+
clearColor, _ := color.Color("reset")
4040
var error_msg string
4141
if e == nil {
4242
error_msg = "no error message provided"

testdata/configs/color_overrides.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
color_clean='#e6ee04'
2-
color_no_upstream="fg:black bg:white"
2+
color_no_upstream="reset fg:black bg:white"
33
color_dirty="bg:#b30559"
44
color_delta="fg:#fcb728"
55
color_untracked="fg:#ff0000 bg:#16f2aa"

0 commit comments

Comments
 (0)