Skip to content

Commit f5f0c14

Browse files
feat(iterm): add iTerm features to the root configuration
BREAKING CHANGE: The iTerm segment has been removed and its features have been added to the root configuration. To re-enable the iTerm features, remove the iTerm segment and add the following to your oh-my-posh configuration: ```json { "iterm_features": ["prompt_mark", "current_dir", "remote_host"] } ``` Choose this option if you want to enable the prompt mark for shell integration and/or enable current directory and remote host in the iTerm status bar.
1 parent 391ceaf commit f5f0c14

File tree

13 files changed

+248
-255
lines changed

13 files changed

+248
-255
lines changed

src/ansi/ansi_writer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ type Writer struct {
114114
hyperlinkStart string
115115
hyperlinkCenter string
116116
hyperlinkEnd string
117+
118+
iTermPromptMark string
119+
iTermCurrentDir string
120+
iTermRemoteHost string
117121
}
118122

119123
func (w *Writer) Init(shellName string) {
@@ -137,6 +141,9 @@ func (w *Writer) Init(shellName string) {
137141
w.osc99 = "\\[\x1b]9;9;%s\x1b\\\\\\]"
138142
w.osc7 = "\\[\x1b]7;file://%s/%s\x1b\\\\\\]"
139143
w.osc51 = "\\[\x1b]51;A;%s@%s:%s\x1b\\\\\\]"
144+
w.iTermPromptMark = "\\[$(iterm2_prompt_mark)\\]"
145+
w.iTermCurrentDir = "\\[\x1b]1337;CurrentDir=%s\x07\\]"
146+
w.iTermRemoteHost = "\\[\x1b]1337;RemoteHost=%s@%s\x07\\]"
140147
case shell.ZSH, shell.TCSH:
141148
w.format = "%%{%s%%}"
142149
w.linechange = "%%{\x1b[%d%s%%}"
@@ -154,6 +161,9 @@ func (w *Writer) Init(shellName string) {
154161
w.osc99 = "%%{\x1b]9;9;%s\x1b\\%%}"
155162
w.osc7 = "%%{\x1b]7;file://%s/%s\x1b\\%%}"
156163
w.osc51 = "%%{\x1b]51;A%s@%s:%s\x1b\\%%}"
164+
w.iTermPromptMark = "%{$(iterm2_prompt_mark)%}"
165+
w.iTermCurrentDir = "%%{\x1b]1337;CurrentDir=%s\x07%%}"
166+
w.iTermRemoteHost = "%%{\x1b]1337;RemoteHost=%s@%s\x07%%}"
157167
default:
158168
w.linechange = "\x1b[%d%s"
159169
w.left = "\x1b[%dD"
@@ -171,6 +181,9 @@ func (w *Writer) Init(shellName string) {
171181
w.osc99 = "\x1b]9;9;%s\x1b\\"
172182
w.osc7 = "\x1b]7;file://%s/%s\x1b\\"
173183
w.osc51 = "\x1b]51;A%s@%s:%s\x1b\\"
184+
w.iTermPromptMark = "$(iterm2_prompt_mark)"
185+
w.iTermCurrentDir = "\x1b]1337;CurrentDir=%s\x07"
186+
w.iTermRemoteHost = "\x1b]1337;RemoteHost=%s@%s\x07"
174187
}
175188
}
176189

src/ansi/iterm.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ansi
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type iTermFeature string
9+
10+
const (
11+
PromptMark iTermFeature = "prompt_mark"
12+
CurrentDir iTermFeature = "current_dir"
13+
RemoteHost iTermFeature = "remote_host"
14+
)
15+
16+
type ITermFeatures []iTermFeature
17+
18+
func (w *Writer) RenderItermFeatures(features ITermFeatures, pwd, user, host string) string {
19+
var result strings.Builder
20+
for _, feature := range features {
21+
switch feature {
22+
case PromptMark:
23+
result.WriteString(w.iTermPromptMark)
24+
case CurrentDir:
25+
result.WriteString(fmt.Sprintf(w.iTermCurrentDir, pwd))
26+
case RemoteHost:
27+
result.WriteString(fmt.Sprintf(w.iTermRemoteHost, user, host))
28+
}
29+
}
30+
31+
return result.String()
32+
}

src/engine/config.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,28 @@ const (
3131

3232
// Config holds all the theme for rendering the prompt
3333
type Config struct {
34-
Version int `json:"version" toml:"version"`
35-
FinalSpace bool `json:"final_space,omitempty" toml:"final_space,omitempty"`
36-
ConsoleTitleTemplate string `json:"console_title_template,omitempty" toml:"console_title_template,omitempty"`
37-
TerminalBackground string `json:"terminal_background,omitempty" toml:"terminal_background,omitempty"`
38-
AccentColor string `json:"accent_color,omitempty" toml:"accent_color,omitempty"`
39-
Blocks []*Block `json:"blocks,omitempty" toml:"blocks,omitempty"`
40-
Tooltips []*Segment `json:"tooltips,omitempty" toml:"tooltips,omitempty"`
41-
TransientPrompt *Segment `json:"transient_prompt,omitempty" toml:"transient_prompt,omitempty"`
42-
ValidLine *Segment `json:"valid_line,omitempty" toml:"valid_line,omitempty"`
43-
ErrorLine *Segment `json:"error_line,omitempty" toml:"error_line,omitempty"`
44-
SecondaryPrompt *Segment `json:"secondary_prompt,omitempty" toml:"secondary_prompt,omitempty"`
45-
DebugPrompt *Segment `json:"debug_prompt,omitempty" toml:"debug_prompt,omitempty"`
46-
Palette ansi.Palette `json:"palette,omitempty" toml:"palette,omitempty"`
47-
Palettes *ansi.Palettes `json:"palettes,omitempty" toml:"palettes,omitempty"`
48-
Cycle ansi.Cycle `json:"cycle,omitempty" toml:"cycle,omitempty"`
49-
ShellIntegration bool `json:"shell_integration,omitempty" toml:"shell_integration,omitempty"`
50-
PWD string `json:"pwd,omitempty" toml:"pwd,omitempty"`
51-
Var map[string]any `json:"var,omitempty" toml:"var,omitempty"`
52-
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"`
53-
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"`
54-
DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"`
34+
Version int `json:"version" toml:"version"`
35+
FinalSpace bool `json:"final_space,omitempty" toml:"final_space,omitempty"`
36+
ConsoleTitleTemplate string `json:"console_title_template,omitempty" toml:"console_title_template,omitempty"`
37+
TerminalBackground string `json:"terminal_background,omitempty" toml:"terminal_background,omitempty"`
38+
AccentColor string `json:"accent_color,omitempty" toml:"accent_color,omitempty"`
39+
Blocks []*Block `json:"blocks,omitempty" toml:"blocks,omitempty"`
40+
Tooltips []*Segment `json:"tooltips,omitempty" toml:"tooltips,omitempty"`
41+
TransientPrompt *Segment `json:"transient_prompt,omitempty" toml:"transient_prompt,omitempty"`
42+
ValidLine *Segment `json:"valid_line,omitempty" toml:"valid_line,omitempty"`
43+
ErrorLine *Segment `json:"error_line,omitempty" toml:"error_line,omitempty"`
44+
SecondaryPrompt *Segment `json:"secondary_prompt,omitempty" toml:"secondary_prompt,omitempty"`
45+
DebugPrompt *Segment `json:"debug_prompt,omitempty" toml:"debug_prompt,omitempty"`
46+
Palette ansi.Palette `json:"palette,omitempty" toml:"palette,omitempty"`
47+
Palettes *ansi.Palettes `json:"palettes,omitempty" toml:"palettes,omitempty"`
48+
Cycle ansi.Cycle `json:"cycle,omitempty" toml:"cycle,omitempty"`
49+
ShellIntegration bool `json:"shell_integration,omitempty" toml:"shell_integration,omitempty"`
50+
PWD string `json:"pwd,omitempty" toml:"pwd,omitempty"`
51+
Var map[string]any `json:"var,omitempty" toml:"var,omitempty"`
52+
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"`
53+
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"`
54+
DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"`
55+
ITermFeatures ansi.ITermFeatures `json:"iterm_features,omitempty" toml:"iterm_features,omitempty"`
5556

5657
// Deprecated
5758
OSC99 bool `json:"osc99,omitempty" toml:"osc99,omitempty"`

src/engine/engine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ func (e *Engine) isWarp() bool {
132132
return e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal"
133133
}
134134

135+
func (e *Engine) isIterm() bool {
136+
return e.Env.Getenv("TERM_PROGRAM") == "iTerm.app"
137+
}
138+
135139
func (e *Engine) shouldFill(filler string, remaining, blockLength int) (string, bool) {
136140
if len(filler) == 0 {
137141
return "", false

src/engine/prompt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func (e *Engine) Primary() string {
6868
e.currentLineLength++
6969
}
7070

71+
if e.Config.ITermFeatures != nil && e.isIterm() {
72+
host, _ := e.Env.Host()
73+
e.write(e.Writer.RenderItermFeatures(e.Config.ITermFeatures, e.Env.Pwd(), e.Env.User(), host))
74+
}
75+
7176
if e.Config.ShellIntegration && e.Config.TransientPrompt == nil {
7277
e.write(e.Writer.CommandStart())
7378
}

src/engine/segment.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ const (
160160
HELM SegmentType = "helm"
161161
// IPIFY segment
162162
IPIFY SegmentType = "ipify"
163-
// ITERM inserts the Shell Integration prompt mark on iTerm zsh/bash/fish
164-
ITERM SegmentType = "iterm"
165163
// JAVA writes the active java version
166164
JAVA SegmentType = "java"
167165
// JULIA writes which julia version is currently active
@@ -307,7 +305,6 @@ var Segments = map[SegmentType]func() SegmentWriter{
307305
HASKELL: func() SegmentWriter { return &segments.Haskell{} },
308306
HELM: func() SegmentWriter { return &segments.Helm{} },
309307
IPIFY: func() SegmentWriter { return &segments.IPify{} },
310-
ITERM: func() SegmentWriter { return &segments.ITerm{} },
311308
JAVA: func() SegmentWriter { return &segments.Java{} },
312309
JULIA: func() SegmentWriter { return &segments.Julia{} },
313310
KOTLIN: func() SegmentWriter { return &segments.Kotlin{} },

src/platform/shell.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ type Shell struct {
200200
Var SimpleMap
201201

202202
cwd string
203+
host string
203204
cmdCache *commandCache
204205
fileCache *fileCache
205206
tmplCache *TemplateCache
@@ -496,13 +497,20 @@ func (env *Shell) User() string {
496497

497498
func (env *Shell) Host() (string, error) {
498499
defer env.Trace(time.Now())
500+
if len(env.host) != 0 {
501+
return env.host, nil
502+
}
503+
499504
hostName, err := os.Hostname()
500505
if err != nil {
501506
env.Error(err)
502507
return "", err
503508
}
509+
504510
hostName = cleanHostName(hostName)
505511
env.Debug(hostName)
512+
env.host = hostName
513+
506514
return hostName, nil
507515
}
508516

src/segments/iterm.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/segments/iterm_test.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)