feat: add .PromptCount

relates to #3388
This commit is contained in:
Jan De Dobbeleer 2023-01-22 20:06:31 +01:00 committed by Jan De Dobbeleer
parent 07118352f6
commit 522a216c00
6 changed files with 62 additions and 16 deletions

View file

@ -57,6 +57,7 @@ var printCmd = &cobra.Command{
Shell: shellName,
ShellVersion: shellVersion,
Plain: plain,
Primary: args[0] == "primary",
}
eng := engine.New(flags)

View file

@ -264,3 +264,7 @@ func (env *MockedEnvironment) DirIsWritable(path string) bool {
args := env.Called(path)
return args.Bool(0)
}
func (env *MockedEnvironment) SetPromptCount() {
_ = env.Called()
}

View file

@ -34,7 +34,7 @@ const (
LINUX = "linux"
)
func getPID() string {
func pid() string {
pid := os.Getenv("POSH_PID")
if len(pid) == 0 {
pid = strconv.Itoa(os.Getppid())
@ -43,8 +43,9 @@ func getPID() string {
}
var (
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", getPID())
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", getPID())
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", pid())
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
)
type Flags struct {
@ -63,6 +64,8 @@ type Flags struct {
Debug bool
Manual bool
Plain bool
Primary bool
PromptCount int
}
type CommandError struct {
@ -148,6 +151,7 @@ type TemplateCache struct {
Env map[string]string
OS string
WSL bool
PromptCount int
Segments SegmentsCache
sync.RWMutex
@ -205,6 +209,7 @@ type Environment interface {
Connection(connectionType ConnectionType) (*Connection, error)
TemplateCache() *TemplateCache
LoadTemplateCache()
SetPromptCount()
Debug(message string)
Error(err error)
Trace(start time.Time, args ...string)
@ -254,6 +259,7 @@ func (env *Shell) Init() {
env.cmdCache = &commandCache{
commands: NewConcurrentMap(),
}
env.SetPromptCount()
}
func (env *Shell) resolveConfigPath() {
@ -712,6 +718,7 @@ func (env *Shell) TemplateCache() *TemplateCache {
if env.tmplCache != nil {
return env.tmplCache
}
tmplCache := &TemplateCache{
Root: env.Root(),
Shell: env.Shell(),
@ -719,8 +726,10 @@ func (env *Shell) TemplateCache() *TemplateCache {
Code: env.ErrorCode(),
WSL: env.IsWsl(),
Segments: make(map[string]interface{}),
PromptCount: env.CmdFlags.PromptCount,
}
tmplCache.Env = make(map[string]string)
const separator = "="
values := os.Environ()
for value := range values {
@ -730,21 +739,25 @@ func (env *Shell) TemplateCache() *TemplateCache {
}
tmplCache.Env[key] = val
}
pwd := env.Pwd()
tmplCache.PWD = ReplaceHomeDirPrefixWithTilde(env, pwd)
tmplCache.Folder = Base(env, pwd)
if env.GOOS() == WINDOWS && strings.HasSuffix(tmplCache.Folder, ":") {
tmplCache.Folder += `\`
}
tmplCache.UserName = env.User()
if host, err := env.Host(); err == nil {
tmplCache.HostName = host
}
goos := env.GOOS()
tmplCache.OS = goos
if goos == LINUX {
tmplCache.OS = env.Platform()
}
env.tmplCache = tmplCache
return tmplCache
}
@ -786,6 +799,28 @@ func dirMatchesOneOf(dir, home, goos string, regexes []string) bool {
return false
}
func (env *Shell) SetPromptCount() {
countStr := os.Getenv("POSH_PROMPT_COUNT")
if len(countStr) > 0 {
// this counter is incremented by the shell
count, err := strconv.Atoi(countStr)
if err == nil {
env.CmdFlags.PromptCount = count
return
}
}
var count int
if val, found := env.Cache().Get(PROMPTCOUNTCACHE); found {
count, _ = strconv.Atoi(val)
}
// only write to cache if we're the primary prompt
if env.CmdFlags.Primary {
count++
env.Cache().Set(PROMPTCOUNTCACHE, strconv.Itoa(count), 1440)
}
env.CmdFlags.PromptCount = count
}
func IsPathSeparator(env Environment, c uint8) bool {
if c == '/' {
return true

View file

@ -2,6 +2,7 @@ export POSH_THEME=::CONFIG::
export POSH_PID=$$
export POWERLINE_COMMAND="oh-my-posh"
export CONDA_PROMPT_MODIFIER=false
export POSH_PROMPT_COUNT=0
# set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=zsh)"
@ -23,6 +24,8 @@ function prompt_ohmyposh_precmd() {
omp_now=$(::OMP:: get millis --shell=zsh)
omp_elapsed=$(($omp_now-$omp_start_time))
fi
count=$((POSH_PROMPT_COUNT+1))
export POSH_PROMPT_COUNT=$count
set_poshcontext
eval "$(::OMP:: print primary --config="$POSH_THEME" --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh --shell-version="$ZSH_VERSION")"
unset omp_start_time

View file

@ -88,6 +88,7 @@ func (t *Text) cleanTemplate() {
"WSL",
"Segments",
"Templates",
"PromptCount",
}
knownVariable := func(variable string) bool {

View file

@ -13,19 +13,20 @@ offers a few standard properties to work with.
## Global properties
| Name | Type | Description |
| --------------- | --------- | ------------------------------------- |
| `.Root` | `boolean` | is the current user root/admin or not |
| `.PWD` | `string` | the current working directory |
| `.Folder` | `string` | the current working folder |
| `.Shell` | `string` | the current shell name |
| `.ShellVersion` | `string` | the current shell version |
| `.UserName` | `string` | the current user name |
| `.HostName` | `string` | the host name |
| `.Code` | `int` | the last exit code |
| `.OS` | `string` | the operating system |
| `.WSL` | `boolean` | in WSL yes/no |
| `.Templates` | `string` | the [templates][templates] result |
| Name | Type | Description |
| --------------- | --------- | ----------------------------------------------------------------- |
| `.Root` | `boolean` | is the current user root/admin or not |
| `.PWD` | `string` | the current working directory |
| `.Folder` | `string` | the current working folder |
| `.Shell` | `string` | the current shell name |
| `.ShellVersion` | `string` | the current shell version |
| `.UserName` | `string` | the current user name |
| `.HostName` | `string` | the host name |
| `.Code` | `int` | the last exit code |
| `.OS` | `string` | the operating system |
| `.WSL` | `boolean` | in WSL yes/no |
| `.Templates` | `string` | the [templates][templates] result |
| `.PromptCount` | `int` | the prompt counter, increments with 1 for every prompt invocation |
## Environment variables
@ -177,6 +178,7 @@ to distinct between both. For example:
"template": "{{ .Segments.Hello.Output }} {{ .Segments.World.Output }}"
}
```
:::
If you want to know if a specific segment is active, you can use the `.Segments.Contains` function, for example: