Skip to content

Commit adc79eb

Browse files
fix(iterm): initialize prompt mark correctly for all supported shells
1 parent 5dee9d7 commit adc79eb

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

src/ansi/ansi_writer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ func (w *Writer) Init(shellName string) {
181181
w.osc99 = "\x1b]9;9;%s\x1b\\"
182182
w.osc7 = "\x1b]7;file://%s/%s\x1b\\"
183183
w.osc51 = "\x1b]51;A%s@%s:%s\x1b\\"
184-
w.iTermPromptMark = "$(iterm2_prompt_mark)"
185184
w.iTermCurrentDir = "\x1b]1337;CurrentDir=%s\x07"
186185
w.iTermRemoteHost = "\x1b]1337;RemoteHost=%s@%s\x07"
187186
}

src/ansi/iterm.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package ansi
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
7+
8+
"github.com/jandedobbeleer/oh-my-posh/src/shell"
69
)
710

811
type iTermFeature string
@@ -15,11 +18,27 @@ const (
1518

1619
type ITermFeatures []iTermFeature
1720

18-
func (w *Writer) RenderItermFeatures(features ITermFeatures, pwd, user, host string) string {
21+
func (f ITermFeatures) Contains(feature iTermFeature) bool {
22+
for _, item := range f {
23+
if item == feature {
24+
return true
25+
}
26+
}
27+
28+
return false
29+
}
30+
31+
func (w *Writer) RenderItermFeatures(features ITermFeatures, sh, pwd, user, host string) string {
32+
supportedShells := []string{shell.BASH, shell.ZSH}
33+
1934
var result strings.Builder
2035
for _, feature := range features {
2136
switch feature {
2237
case PromptMark:
38+
if !slices.Contains(supportedShells, sh) {
39+
continue
40+
}
41+
2342
result.WriteString(w.iTermPromptMark)
2443
case CurrentDir:
2544
result.WriteString(fmt.Sprintf(w.iTermCurrentDir, pwd))

src/cli/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"fmt"
55

6+
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
67
"github.com/jandedobbeleer/oh-my-posh/src/engine"
78
"github.com/jandedobbeleer/oh-my-posh/src/platform"
89
"github.com/jandedobbeleer/oh-my-posh/src/shell"
@@ -71,6 +72,7 @@ func runInit(shellName string) {
7172
shell.ErrorLine = cfg.ErrorLine != nil || cfg.ValidLine != nil
7273
shell.Tooltips = len(cfg.Tooltips) > 0
7374
shell.ShellIntegration = cfg.ShellIntegration
75+
shell.PromptMark = shellName == shell.FISH && cfg.ITermFeatures != nil && cfg.ITermFeatures.Contains(ansi.PromptMark)
7476

7577
for i, block := range cfg.Blocks {
7678
// only fetch cursor position when relevant

src/engine/prompt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (e *Engine) Primary() string {
7070

7171
if e.Config.ITermFeatures != nil && e.isIterm() {
7272
host, _ := e.Env.Host()
73-
e.write(e.Writer.RenderItermFeatures(e.Config.ITermFeatures, e.Env.Pwd(), e.Env.User(), host))
73+
e.write(e.Writer.RenderItermFeatures(e.Config.ITermFeatures, e.Env.Shell(), e.Env.Pwd(), e.Env.User(), host))
7474
}
7575

7676
if e.Config.ShellIntegration && e.Config.TransientPrompt == nil {

src/shell/init.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
ShellIntegration bool
5353
RPrompt bool
5454
CursorPositioning bool
55+
PromptMark bool
5556
)
5657

5758
func getExecutablePath(env platform.Environment) (string, error) {
@@ -207,9 +208,18 @@ func PrintInit(env platform.Environment) string {
207208
if env.Flags().Manual {
208209
return "false"
209210
}
211+
210212
return strconv.FormatBool(setting)
211213
}
212214

215+
promptMark := func() string {
216+
if PromptMark {
217+
return "iterm2_prompt_mark"
218+
}
219+
220+
return ""
221+
}
222+
213223
shell := env.Flags().Shell
214224
configFile := env.Flags().Config
215225

@@ -273,6 +283,7 @@ func PrintInit(env platform.Environment) string {
273283
"::CURSOR::", strconv.FormatBool(CursorPositioning),
274284
"::UPGRADE::", strconv.FormatBool(hasNotice),
275285
"::UPGRADENOTICE::", notice,
286+
"::PROMPT_MARK::", promptMark(),
276287
).Replace(script)
277288
}
278289

src/shell/scripts/omp.fish

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ function fish_prompt
2323
::OMP:: print transient --config $POSH_THEME --shell fish --status $omp_status_cache --pipestatus="$omp_pipestatus_cache" --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --no-status=$omp_no_exit_code
2424
return
2525
end
26+
2627
set --global omp_status_cache $omp_status_cache_temp
2728
set --global omp_pipestatus_cache $omp_pipestatus_cache_temp
2829
set --global omp_stack_count (count $dirstack)
2930
set --global omp_duration "$CMD_DURATION$cmd_duration"
3031
set --global omp_no_exit_code false
32+
3133
# check if variable set, < 3.2 case
3234
if set --query omp_lastcommand; and test "$omp_lastcommand" = ""
3335
set omp_duration 0
3436
set omp_no_exit_code true
3537
end
38+
3639
# works with fish >=3.2
3740
if set --query omp_last_status_generation; and test "$omp_last_status_generation" = "$status_generation"
3841
set omp_duration 0
@@ -41,16 +44,23 @@ function fish_prompt
4144
# first execution - $status_generation is 0, $omp_last_status_generation is empty
4245
set omp_no_exit_code true
4346
end
47+
4448
if set --query status_generation
4549
set --global --export omp_last_status_generation $status_generation
4650
end
51+
4752
set_poshcontext
53+
4854
# validate if the user cleared the screen
4955
set --local omp_cleared false
5056
set --local last_command (history search --max 1)
57+
5158
if test "$last_command" = "clear"
5259
set omp_cleared true
5360
end
61+
62+
::PROMPT_MARK::
63+
5464
::OMP:: print primary --config $POSH_THEME --shell fish --status $omp_status_cache --pipestatus="$omp_pipestatus_cache" --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION --cleared=$omp_cleared --no-status=$omp_no_exit_code
5565
end
5666

0 commit comments

Comments
 (0)