Skip to content

Commit 3d49426

Browse files
authored
Merge pull request #6 from xeyossr/enhance/git-module
Refactor: Git status handling, component separation, and format adjustments
2 parents 29eafc6 + 3400ad3 commit 3d49426

File tree

9 files changed

+140
-78
lines changed

9 files changed

+140
-78
lines changed

aur/.SRCINFO

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pkgbase = pulsarship
22
pkgdesc = 🚀🌠 A minimal, fast, and customizable prompt written in Go
3-
pkgver = 0.2.0
3+
pkgver = 0.2.1
44
pkgrel = 1
55
url = https://github.com/xeyossr/pulsarship
66
arch = x86_64
77
license = GPL3
88
makedepends = go
99
makedepends = git
10-
source = git+https://github.com/xeyossr/pulsarship.git#tag=v0.1.0
10+
source = git+https://github.com/xeyossr/pulsarship.git#tag=v0.2.1
1111
sha256sums = SKIP
1212

1313
pkgname = pulsarship

aur/PKGBUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Maintainer: kamisaki
22
# Description: 🚀🌠 A minimal, fast, and customizable prompt written in Go
33
pkgname=pulsarship
4-
pkgver=0.2.0
4+
pkgver=0.2.1
55
pkgrel=1
66
pkgdesc="🚀🌠 A minimal, fast, and customizable prompt written in Go"
77
arch=('x86_64')

internal/components/character.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func (c *CharacterComponent) Val() (string, error) {
2424
}
2525

2626
func (c *CharacterComponent) Render() (models.Result, error) {
27-
utils.SetDefault(&c.Config.Icon, ">")
28-
utils.SetDefault(&c.Config.Format, ">")
27+
utils.SetDefault(&c.Config.Icon, "")
28+
utils.SetDefault(&c.Config.Format, "^(#f2bfa3){character}^")
2929
var format string = *c.Config.Format
3030

3131
val, err := c.Val()

internal/components/cwd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *CwdComponent) Val() (string, error) {
4242

4343
func (c *CwdComponent) Render() (models.Result, error) {
4444
utils.SetDefault(&c.Config.MaxLength, 3)
45-
utils.SetDefault(&c.Config.Format, "{cwd}")
45+
utils.SetDefault(&c.Config.Format, "^(#8e9ae6){cwd}^")
4646
var format string = *c.Config.Format
4747

4848
val, err := c.Val()

internal/components/git_branch.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package components
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/xeyossr/pulsarship/internal/models"
11+
"github.com/xeyossr/pulsarship/internal/utils"
12+
)
13+
14+
type GitBranchComponent struct {
15+
Config models.GitBranchConfig
16+
Palette models.PaletteConfig
17+
}
18+
19+
func init() {
20+
Registry["git_branch"] = func(config models.PromptConfig) models.Component {
21+
return &GitBranchComponent{
22+
Config: config.GitBranch,
23+
Palette: config.Palette,
24+
}
25+
}
26+
}
27+
28+
func findGitRoot(start string) (string, error) {
29+
dir := start
30+
for {
31+
if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
32+
return dir, nil
33+
}
34+
parent := filepath.Dir(dir)
35+
if parent == dir {
36+
return "", fmt.Errorf(".git directory not found")
37+
}
38+
dir = parent
39+
}
40+
}
41+
42+
func (g *GitBranchComponent) Val() (string, error) {
43+
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
44+
branchOut, err := branchCmd.Output()
45+
if err != nil {
46+
return "", err
47+
}
48+
branch := strings.TrimSpace(string(branchOut))
49+
50+
return branch, nil
51+
}
52+
53+
func (g *GitBranchComponent) Render() (models.Result, error) {
54+
utils.SetDefault(&g.Config.Format, "^(#e6e7ae)on^ ^(#b8a9f9) {branch}^")
55+
val, err := g.Val()
56+
if err != nil {
57+
return models.Result{Skip: true}, err
58+
}
59+
60+
rendered, err := utils.RenderFormat(*g.Config.Format, map[string]string{
61+
"git_branch": val,
62+
"branch": val,
63+
}, (*map[string]string)(&g.Palette))
64+
65+
return models.Result{Value: rendered}, err
66+
}
67+
68+
func (g *GitBranchComponent) RenderAsync() <-chan models.Result {
69+
ch := make(chan models.Result, 1)
70+
go func() {
71+
val, err := g.Render()
72+
ch <- models.Result{Value: val.Value, Error: err}
73+
}()
74+
return ch
75+
}
76+
77+
func (g GitBranchComponent) Name() string {
78+
return "git_branch"
79+
}

internal/components/git.go renamed to internal/components/git_status.go

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,40 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7-
"path/filepath"
87
"strings"
98

109
"github.com/xeyossr/pulsarship/internal/models"
1110
"github.com/xeyossr/pulsarship/internal/utils"
1211
)
1312

14-
type GitComponent struct {
15-
Config models.GitConfig
13+
type GitStatusComponent struct {
14+
Config models.GitStatusConfig
1615
Palette models.PaletteConfig
1716
}
1817

1918
func init() {
20-
Registry["git"] = func(config models.PromptConfig) models.Component {
21-
return &GitComponent{
22-
Config: config.Git,
19+
Registry["git_status"] = func(config models.PromptConfig) models.Component {
20+
return &GitStatusComponent{
21+
Config: config.GitStatus,
2322
Palette: config.Palette,
2423
}
2524
}
2625
}
2726

28-
func findGitRoot(start string) (string, error) {
29-
dir := start
30-
for {
31-
if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
32-
return dir, nil
33-
}
34-
parent := filepath.Dir(dir)
35-
if parent == dir {
36-
return "", fmt.Errorf(".git directory not found")
37-
}
38-
dir = parent
39-
}
40-
}
41-
42-
func Status(g *GitComponent) (string, error) {
27+
func (g *GitStatusComponent) Val() (string, error) {
28+
utils.SetDefault(&g.Config.Format, "^(#f28fad)[^{status}^(#f28fad)]^")
4329
utils.SetDefault(&g.Config.CleanSuffix, "")
44-
utils.SetDefault(&g.Config.UpToDate, " ^(#ff0000)[✓]^")
45-
utils.SetDefault(&g.Config.Conflicted, " ^(#ff0000)[!?]^")
46-
utils.SetDefault(&g.Config.Ahead, " ^(#ff0000)[↑+]^")
47-
utils.SetDefault(&g.Config.Behind, " ^(#ff0000)[↓-]^")
48-
utils.SetDefault(&g.Config.Diverged, " ^(#ff0000)[⇅!?]^")
49-
utils.SetDefault(&g.Config.Untracked, " ^(#ff0000)[?{count}]^")
50-
utils.SetDefault(&g.Config.Stashed, " ^(#ff0000)[S{count}]^")
51-
utils.SetDefault(&g.Config.Modified, " ^(#ff0000)[M{count}]^")
52-
utils.SetDefault(&g.Config.Staged, " ^(#ff0000)[+{count}]^")
53-
utils.SetDefault(&g.Config.Renamed, " ^(#ff0000)[R{count}]^")
54-
utils.SetDefault(&g.Config.Deleted, " ^(#ff0000)[X{count}]^")
30+
utils.SetDefault(&g.Config.Conflicted, "^(#f28fad)✖^")
31+
utils.SetDefault(&g.Config.Ahead, "^(#f28fad)⇡^")
32+
utils.SetDefault(&g.Config.Behind, "^(#f28fad)⇣^")
33+
utils.SetDefault(&g.Config.Diverged, "^(#f28fad)⇕^")
34+
utils.SetDefault(&g.Config.UpToDate, "^(#f28fad)✓^")
35+
utils.SetDefault(&g.Config.Untracked, "^(#f28fad)?^")
36+
utils.SetDefault(&g.Config.Stashed, "^(#f28fad)S^")
37+
utils.SetDefault(&g.Config.Modified, "^(#f28fad)~^")
38+
utils.SetDefault(&g.Config.Staged, "^(#f28fad)+^")
39+
utils.SetDefault(&g.Config.Renamed, "^(#f28fad)»^")
40+
utils.SetDefault(&g.Config.Deleted, "^(#f28fad)✘^")
5541

5642
cwd, _ := os.Getwd()
5743
_, err := findGitRoot(cwd)
@@ -157,39 +143,26 @@ func Status(g *GitComponent) (string, error) {
157143
return suffix, nil
158144
}
159145

160-
func (g *GitComponent) Val() (string, error) {
161-
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
162-
branchOut, err := branchCmd.Output()
163-
if err != nil {
164-
return "", err
165-
}
166-
branch := strings.TrimSpace(string(branchOut))
167-
168-
return branch, nil
169-
}
170-
171-
func (g *GitComponent) Render() (models.Result, error) {
172-
utils.SetDefault(&g.Config.Format, "^(#f2a971) {git}^")
146+
func (g *GitStatusComponent) Render() (models.Result, error) {
147+
utils.SetDefault(&g.Config.Format, "{status}")
173148
val, err := g.Val()
174-
if err != nil {
175-
return models.Result{Skip: true}, err
149+
if val == "" {
150+
return models.Result{Skip: true}, nil
176151
}
177152

178-
status, err := Status(g)
179153
if err != nil {
180154
return models.Result{Skip: true}, err
181155
}
182156

183157
rendered, err := utils.RenderFormat(*g.Config.Format, map[string]string{
184-
"git": val,
185-
"branch": val,
186-
"status": status,
158+
"git_status": val,
159+
"status": val,
187160
}, (*map[string]string)(&g.Palette))
188161

189162
return models.Result{Value: rendered}, err
190163
}
191164

192-
func (g *GitComponent) RenderAsync() <-chan models.Result {
165+
func (g *GitStatusComponent) RenderAsync() <-chan models.Result {
193166
ch := make(chan models.Result, 1)
194167
go func() {
195168
val, err := g.Render()
@@ -198,6 +171,6 @@ func (g *GitComponent) RenderAsync() <-chan models.Result {
198171
return ch
199172
}
200173

201-
func (g GitComponent) Name() string {
202-
return "git"
174+
func (g GitStatusComponent) Name() string {
175+
return "git_status"
203176
}

internal/components/time.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (t *TimeComponent) Val() (string, error) {
3232

3333
func (t *TimeComponent) Render() (models.Result, error) {
3434
utils.SetDefault(&t.Config.TimeFormat, "15:04:05")
35-
utils.SetDefault(&t.Config.Format, "{time}")
35+
utils.SetDefault(&t.Config.Format, "^(#97a1c3){time}^")
3636
var format string = *t.Config.Format
3737

3838
val, err := t.Val()

internal/config/default.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
var DefaultConfig = models.PromptConfig{
9-
Prompt: "{cwd} {git}\n{character} ",
9+
Prompt: "{cwd} {git_branch} {git_status}\n{character} ",
1010
AddNewLine: true,
1111

1212
Hostname: models.HostnameConfig{
@@ -27,21 +27,26 @@ var DefaultConfig = models.PromptConfig{
2727
Icon: utils.Ptr("❯"),
2828
Format: utils.Ptr("^(peach){character}^"),
2929
},
30-
Git: models.GitConfig{
31-
Format: utils.Ptr("^(mauve) {branch} {status}^ "),
32-
CleanSuffix: utils.Ptr("^(overlay1)✔^ "),
33-
Conflicted: utils.Ptr("^(red)Conflicted {count}^ "),
34-
Ahead: utils.Ptr("^(green)↑{count}^ "),
35-
Behind: utils.Ptr("^(peach)↓{count}^ "),
36-
Diverged: utils.Ptr("^(yellow)↕{count}^ "),
37-
UpToDate: utils.Ptr("^(green)✓^ "),
38-
Untracked: utils.Ptr("^(overlay1)?{count}^ "),
39-
Stashed: utils.Ptr("^(sky)★{count}^ "),
40-
Modified: utils.Ptr("^(flamingo)!{count}^ "),
41-
Staged: utils.Ptr("^(lavender)+{count}^ "),
42-
Renamed: utils.Ptr("^(sapphire)»{count}^ "),
43-
Deleted: utils.Ptr("^(red)-{count} ^"),
30+
GitBranch: models.GitBranchConfig{
31+
Format: utils.Ptr("^(yellow)on^ ^(mauve) {branch}^"),
4432
},
33+
34+
GitStatus: models.GitStatusConfig{
35+
Format: utils.Ptr("^(red)[^{status}^(red)]^"),
36+
CleanSuffix: utils.Ptr(""),
37+
Conflicted: utils.Ptr("^(red)✖^"),
38+
Ahead: utils.Ptr("^(red)⇡^"),
39+
Behind: utils.Ptr("^(red)⇣^"),
40+
Diverged: utils.Ptr("^(red)⇕^"),
41+
UpToDate: utils.Ptr("^(red)✓^"),
42+
Untracked: utils.Ptr("^(red)?^"),
43+
Stashed: utils.Ptr("^(red)S^"),
44+
Modified: utils.Ptr("^(red)~^"),
45+
Staged: utils.Ptr("^(red)+^"),
46+
Renamed: utils.Ptr("^(red)»^"),
47+
Deleted: utils.Ptr("^(red)✘^"),
48+
},
49+
4550
Palette: models.PaletteConfig{
4651
"rosewater": "#f5e0dc",
4752
"flamingo": "#f2cdcd",

internal/models/models.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ type PromptConfig struct {
2525
Time TimeConfig `toml:"time"`
2626
Character CharacterConfig `toml:"character"`
2727
Palette PaletteConfig `toml:"palette"`
28-
Git GitConfig `toml:"git"`
28+
GitBranch GitBranchConfig `toml:"git_branch"`
29+
GitStatus GitStatusConfig `toml:"git_status"`
2930
}
3031

3132
type HostnameConfig struct {
@@ -56,7 +57,11 @@ type CustomComponentConfig struct {
5657
Run *string `toml:"run"`
5758
}
5859

59-
type GitConfig struct {
60+
type GitBranchConfig struct {
61+
Format *string `toml:"format"`
62+
}
63+
64+
type GitStatusConfig struct {
6065
Format *string `toml:"format"`
6166
CleanSuffix *string `toml:"clean_suffix"`
6267
Conflicted *string `toml:"conflicted"`

0 commit comments

Comments
 (0)