Skip to content

Commit

Permalink
feat(iterm): add iTerm features to the root configuration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JanDeDobbeleer committed May 19, 2024
1 parent 391ceaf commit e7a10ac
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 255 deletions.
13 changes: 13 additions & 0 deletions src/ansi/ansi_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ type Writer struct {
hyperlinkStart string
hyperlinkCenter string
hyperlinkEnd string

iTermPromptMark string
iTermCurrentDir string
iTermRemoteHost string
}

func (w *Writer) Init(shellName string) {
Expand All @@ -137,6 +141,9 @@ func (w *Writer) Init(shellName string) {
w.osc99 = "\\[\x1b]9;9;%s\x1b\\\\\\]"
w.osc7 = "\\[\x1b]7;file://%s/%s\x1b\\\\\\]"
w.osc51 = "\\[\x1b]51;A;%s@%s:%s\x1b\\\\\\]"
w.iTermPromptMark = "\\[$(iterm2_prompt_mark)\\]"
w.iTermCurrentDir = "\\[\x1b]1337;CurrentDir=%s\x07\\]"
w.iTermRemoteHost = "\\[\x1b]1337;RemoteHost=%s@%s\x07\\]"
case shell.ZSH, shell.TCSH:
w.format = "%%{%s%%}"
w.linechange = "%%{\x1b[%d%s%%}"
Expand All @@ -154,6 +161,9 @@ func (w *Writer) Init(shellName string) {
w.osc99 = "%%{\x1b]9;9;%s\x1b\\%%}"
w.osc7 = "%%{\x1b]7;file://%s/%s\x1b\\%%}"
w.osc51 = "%%{\x1b]51;A%s@%s:%s\x1b\\%%}"
w.iTermPromptMark = "%{$(iterm2_prompt_mark)%}"
w.iTermCurrentDir = "%%{\x1b]1337;CurrentDir=%s\x07%%}"
w.iTermRemoteHost = "%%{\x1b]1337;RemoteHost=%s@%s\x07%%}"
default:
w.linechange = "\x1b[%d%s"
w.left = "\x1b[%dD"
Expand All @@ -171,6 +181,9 @@ func (w *Writer) Init(shellName string) {
w.osc99 = "\x1b]9;9;%s\x1b\\"
w.osc7 = "\x1b]7;file://%s/%s\x1b\\"
w.osc51 = "\x1b]51;A%s@%s:%s\x1b\\"
w.iTermPromptMark = "$(iterm2_prompt_mark)"
w.iTermCurrentDir = "\x1b]1337;CurrentDir=%s\x07"
w.iTermRemoteHost = "\x1b]1337;RemoteHost=%s@%s\x07"
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/ansi/iterm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ansi

import (
"fmt"
"strings"
)

type iTermFeature string

const (
PromptMark iTermFeature = "prompt_mark"
CurrentDir iTermFeature = "current_dir"
RemoteHost iTermFeature = "remote_host"
)

type ITermFeatures []iTermFeature

func (w *Writer) RenderItermFeatures(features ITermFeatures, pwd, user, host string) string {
var result strings.Builder
for _, feature := range features {
switch feature {
case PromptMark:
result.WriteString(w.iTermPromptMark)
case CurrentDir:
result.WriteString(fmt.Sprintf(w.iTermCurrentDir, pwd))
case RemoteHost:
result.WriteString(fmt.Sprintf(w.iTermRemoteHost, user, host))
}
}

return result.String()
}
43 changes: 22 additions & 21 deletions src/engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,28 @@ const (

// Config holds all the theme for rendering the prompt
type Config struct {
Version int `json:"version" toml:"version"`
FinalSpace bool `json:"final_space,omitempty" toml:"final_space,omitempty"`
ConsoleTitleTemplate string `json:"console_title_template,omitempty" toml:"console_title_template,omitempty"`
TerminalBackground string `json:"terminal_background,omitempty" toml:"terminal_background,omitempty"`
AccentColor string `json:"accent_color,omitempty" toml:"accent_color,omitempty"`
Blocks []*Block `json:"blocks,omitempty" toml:"blocks,omitempty"`
Tooltips []*Segment `json:"tooltips,omitempty" toml:"tooltips,omitempty"`
TransientPrompt *Segment `json:"transient_prompt,omitempty" toml:"transient_prompt,omitempty"`
ValidLine *Segment `json:"valid_line,omitempty" toml:"valid_line,omitempty"`
ErrorLine *Segment `json:"error_line,omitempty" toml:"error_line,omitempty"`
SecondaryPrompt *Segment `json:"secondary_prompt,omitempty" toml:"secondary_prompt,omitempty"`
DebugPrompt *Segment `json:"debug_prompt,omitempty" toml:"debug_prompt,omitempty"`
Palette ansi.Palette `json:"palette,omitempty" toml:"palette,omitempty"`
Palettes *ansi.Palettes `json:"palettes,omitempty" toml:"palettes,omitempty"`
Cycle ansi.Cycle `json:"cycle,omitempty" toml:"cycle,omitempty"`
ShellIntegration bool `json:"shell_integration,omitempty" toml:"shell_integration,omitempty"`
PWD string `json:"pwd,omitempty" toml:"pwd,omitempty"`
Var map[string]any `json:"var,omitempty" toml:"var,omitempty"`
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"`
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"`
DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"`
Version int `json:"version" toml:"version"`
FinalSpace bool `json:"final_space,omitempty" toml:"final_space,omitempty"`
ConsoleTitleTemplate string `json:"console_title_template,omitempty" toml:"console_title_template,omitempty"`
TerminalBackground string `json:"terminal_background,omitempty" toml:"terminal_background,omitempty"`
AccentColor string `json:"accent_color,omitempty" toml:"accent_color,omitempty"`
Blocks []*Block `json:"blocks,omitempty" toml:"blocks,omitempty"`
Tooltips []*Segment `json:"tooltips,omitempty" toml:"tooltips,omitempty"`
TransientPrompt *Segment `json:"transient_prompt,omitempty" toml:"transient_prompt,omitempty"`
ValidLine *Segment `json:"valid_line,omitempty" toml:"valid_line,omitempty"`
ErrorLine *Segment `json:"error_line,omitempty" toml:"error_line,omitempty"`
SecondaryPrompt *Segment `json:"secondary_prompt,omitempty" toml:"secondary_prompt,omitempty"`
DebugPrompt *Segment `json:"debug_prompt,omitempty" toml:"debug_prompt,omitempty"`
Palette ansi.Palette `json:"palette,omitempty" toml:"palette,omitempty"`
Palettes *ansi.Palettes `json:"palettes,omitempty" toml:"palettes,omitempty"`
Cycle ansi.Cycle `json:"cycle,omitempty" toml:"cycle,omitempty"`
ShellIntegration bool `json:"shell_integration,omitempty" toml:"shell_integration,omitempty"`
PWD string `json:"pwd,omitempty" toml:"pwd,omitempty"`
Var map[string]any `json:"var,omitempty" toml:"var,omitempty"`
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"`
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"`
DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"`
ITermFeatures ansi.ITermFeatures `json:"iterm_features,omitempty" toml:"iterm_features,omitempty"`

// Deprecated
OSC99 bool `json:"osc99,omitempty" toml:"osc99,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions src/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (e *Engine) isWarp() bool {
return e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal"
}

func (e *Engine) isIterm() bool {
return e.Env.Getenv("TERM_PROGRAM") == "iTerm.app"
}

func (e *Engine) shouldFill(filler string, remaining, blockLength int) (string, bool) {
if len(filler) == 0 {
return "", false
Expand Down
5 changes: 5 additions & 0 deletions src/engine/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (e *Engine) Primary() string {
e.currentLineLength++
}

if e.Config.ITermFeatures != nil && e.isIterm() {
host, _ := e.Env.Host()
e.write(e.Writer.RenderItermFeatures(e.Config.ITermFeatures, e.Env.Pwd(), e.Env.User(), host))
}

if e.Config.ShellIntegration && e.Config.TransientPrompt == nil {
e.write(e.Writer.CommandStart())
}
Expand Down
3 changes: 0 additions & 3 deletions src/engine/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ const (
HELM SegmentType = "helm"
// IPIFY segment
IPIFY SegmentType = "ipify"
// ITERM inserts the Shell Integration prompt mark on iTerm zsh/bash/fish
ITERM SegmentType = "iterm"
// JAVA writes the active java version
JAVA SegmentType = "java"
// JULIA writes which julia version is currently active
Expand Down Expand Up @@ -307,7 +305,6 @@ var Segments = map[SegmentType]func() SegmentWriter{
HASKELL: func() SegmentWriter { return &segments.Haskell{} },
HELM: func() SegmentWriter { return &segments.Helm{} },
IPIFY: func() SegmentWriter { return &segments.IPify{} },
ITERM: func() SegmentWriter { return &segments.ITerm{} },
JAVA: func() SegmentWriter { return &segments.Java{} },
JULIA: func() SegmentWriter { return &segments.Julia{} },
KOTLIN: func() SegmentWriter { return &segments.Kotlin{} },
Expand Down
8 changes: 8 additions & 0 deletions src/platform/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ type Shell struct {
Var SimpleMap

cwd string
host string
cmdCache *commandCache
fileCache *fileCache
tmplCache *TemplateCache
Expand Down Expand Up @@ -496,13 +497,20 @@ func (env *Shell) User() string {

func (env *Shell) Host() (string, error) {
defer env.Trace(time.Now())
if len(env.host) != 0 {
return env.host, nil
}

hostName, err := os.Hostname()
if err != nil {
env.Error(err)
return "", err
}

hostName = cleanHostName(hostName)
env.Debug(hostName)
env.host = hostName

return hostName, nil
}

Expand Down
69 changes: 0 additions & 69 deletions src/segments/iterm.go

This file was deleted.

46 changes: 0 additions & 46 deletions src/segments/iterm_test.go

This file was deleted.

Loading

0 comments on commit e7a10ac

Please sign in to comment.