Skip to content

Commit d684a81

Browse files
johnstegemanJanDeDobbeleer
authored andcommitted
feat(jujutsu): add counter for number of changes in working copy relative to bookmark
1 parent ad8b7b9 commit d684a81

3 files changed

Lines changed: 65 additions & 8 deletions

File tree

src/segments/jujutsu.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package segments
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/jandedobbeleer/oh-my-posh/src/log"
@@ -15,6 +16,8 @@ const (
1516

1617
IgnoreWorkingCopy options.Option = "ignore_working_copy"
1718
ChangeIDMinLen options.Option = "change_id_min_len"
19+
FetchAhead options.Option = "fetch_ahead_counter"
20+
AheadIcon options.Option = "ahead_icon"
1821
)
1922

2023
type JujutsuStatus struct {
@@ -77,7 +80,45 @@ func (jj *Jujutsu) ClosestBookmarks() string {
7780
}
7881

7982
lines := strings.Split(statusString, "\n")
80-
return lines[0]
83+
84+
if !jj.options.Bool(FetchAhead, false) || len(lines[0]) == 0 {
85+
return lines[0]
86+
}
87+
88+
aheadIcon := jj.options.String(AheadIcon, "\u21e1")
89+
marks := strings.Split(lines[0], " ")
90+
// String to return for status
91+
var endString strings.Builder
92+
93+
// Closest bookmarks are all the same distance away from the working copy
94+
// so retrieve the distance to the first one and use it for all of them
95+
96+
rangeString := strings.Trim(marks[0], "*") + "..@"
97+
98+
aheadString, err := jj.getJujutsuCommandOutput("log", "--no-graph", "-T", "'.'", "-r", rangeString)
99+
if err != nil {
100+
return lines[0]
101+
}
102+
103+
aheadCounter := len(aheadString)
104+
aheadCounterString := ""
105+
106+
if aheadCounter != 0 {
107+
aheadCounterString = aheadIcon + strconv.Itoa(aheadCounter)
108+
}
109+
110+
log.Debug("distance to nearest jj bookmark:" + aheadCounterString)
111+
112+
// Loop through each bookmark
113+
for index, mark := range marks {
114+
if index > 0 {
115+
endString.WriteString(" ")
116+
}
117+
118+
endString.WriteString(mark + aheadCounterString)
119+
}
120+
121+
return endString.String()
81122
}
82123

83124
func (jj *Jujutsu) shouldDisplay(displayStatus bool) bool {

themes/schema.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,18 @@
21342134
"description": "Don't snapshot the working copy, and don't update it",
21352135
"default": true
21362136
},
2137+
"fetch_ahead_counter": {
2138+
"type": "boolean",
2139+
"title": "Fetch Ahead Counter",
2140+
"description": "Fetch working copy # of changes ahead of the nearest bookmark",
2141+
"default": false
2142+
},
2143+
"ahead_icon": {
2144+
"type": "string",
2145+
"title": "Ahead icon",
2146+
"description": "Icon to separate bookmark name and ahead counter",
2147+
"default": false
2148+
},
21372149
"status_formats": {
21382150
"$ref": "#/definitions/status_formats"
21392151
},

website/docs/segments/scm/jujutsu.mdx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import Config from "@site/src/components/Config.js";
2222
options: {
2323
fetch_status: true,
2424
ignore_working_copy: false,
25+
fetch_ahead_counter: true,
26+
ahead_icon: "\u21e1",
2527
},
2628
}}
2729
/>
@@ -33,13 +35,15 @@ import Config from "@site/src/components/Config.js";
3335
As doing Jujutsu (jj) calls can slow down the prompt experience, we do not fetch information by default.
3436
Set `status_formats` to `true` to enable fetching additional information (and populate the template).
3537

36-
| Name | Type | Default | Description |
37-
| --------------------- | :-----------------: | :-----: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
38-
| `change_id_min_len` | `int` | `0` | `ChangeID` will be at least this many characters, even if a shorter one would be unique |
39-
| `fetch_status` | `boolean` | `false` | fetch the local changes |
40-
| `ignore_working_copy` | `boolean` | `true` | don't snapshot/update the working copy |
41-
| `native_fallback` | `boolean` | `false` | when set to `true` and `jj.exe` is not available when inside a WSL2 shared Windows drive, we will fallback to the native `jj` executable to fetch data. Not all information can be displayed in this case |
42-
| `status_formats` | `map[string]string` | | a key, value map allowing to override how individual status items are displayed. For example, `"status_formats": { "Added": "Added: %d" }` will display the added count as `Added: 1` instead of `+1`. See the [Status](#status) section for available overrides |
38+
| Name | Type | Default | Description |
39+
| --------------------- | :-----------------: | :------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
40+
| `change_id_min_len` | `int` | `0` | `ChangeID` will be at least this many characters, even if a shorter one would be unique |
41+
| `fetch_status` | `boolean` | `false` | fetch the local changes |
42+
| `ignore_working_copy` | `boolean` | `true` | don't snapshot/update the working copy |
43+
| `fetch_ahead_counter` | `boolean` | `false` | fetch a counter for number of changes between working copy and closest bookmark |
44+
| `ahead_icon` | `string` | `\u21e1` | icon/character between bookmark and ahead counter |
45+
| `native_fallback` | `boolean` | `false` | when set to `true` and `jj.exe` is not available when inside a WSL2 shared Windows drive, we will fallback to the native `jj` executable to fetch data. Not all information can be displayed in this case |
46+
| `status_formats` | `map[string]string` | | a key, value map allowing to override how individual status items are displayed. For example, `"status_formats": { "Added": "Added: %d" }` will display the added count as `Added: 1` instead of `+1`. See the [Status](#status) section for available overrides |
4347

4448
## Template ([info][templates])
4549

0 commit comments

Comments
 (0)