mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
parent
07118352f6
commit
522a216c00
|
@ -57,6 +57,7 @@ var printCmd = &cobra.Command{
|
||||||
Shell: shellName,
|
Shell: shellName,
|
||||||
ShellVersion: shellVersion,
|
ShellVersion: shellVersion,
|
||||||
Plain: plain,
|
Plain: plain,
|
||||||
|
Primary: args[0] == "primary",
|
||||||
}
|
}
|
||||||
|
|
||||||
eng := engine.New(flags)
|
eng := engine.New(flags)
|
||||||
|
|
|
@ -264,3 +264,7 @@ func (env *MockedEnvironment) DirIsWritable(path string) bool {
|
||||||
args := env.Called(path)
|
args := env.Called(path)
|
||||||
return args.Bool(0)
|
return args.Bool(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *MockedEnvironment) SetPromptCount() {
|
||||||
|
_ = env.Called()
|
||||||
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ const (
|
||||||
LINUX = "linux"
|
LINUX = "linux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getPID() string {
|
func pid() string {
|
||||||
pid := os.Getenv("POSH_PID")
|
pid := os.Getenv("POSH_PID")
|
||||||
if len(pid) == 0 {
|
if len(pid) == 0 {
|
||||||
pid = strconv.Itoa(os.Getppid())
|
pid = strconv.Itoa(os.Getppid())
|
||||||
|
@ -43,8 +43,9 @@ func getPID() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", getPID())
|
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", pid())
|
||||||
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", getPID())
|
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
|
||||||
|
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
|
||||||
)
|
)
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
|
@ -63,6 +64,8 @@ type Flags struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
Manual bool
|
Manual bool
|
||||||
Plain bool
|
Plain bool
|
||||||
|
Primary bool
|
||||||
|
PromptCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandError struct {
|
type CommandError struct {
|
||||||
|
@ -148,6 +151,7 @@ type TemplateCache struct {
|
||||||
Env map[string]string
|
Env map[string]string
|
||||||
OS string
|
OS string
|
||||||
WSL bool
|
WSL bool
|
||||||
|
PromptCount int
|
||||||
Segments SegmentsCache
|
Segments SegmentsCache
|
||||||
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
@ -205,6 +209,7 @@ type Environment interface {
|
||||||
Connection(connectionType ConnectionType) (*Connection, error)
|
Connection(connectionType ConnectionType) (*Connection, error)
|
||||||
TemplateCache() *TemplateCache
|
TemplateCache() *TemplateCache
|
||||||
LoadTemplateCache()
|
LoadTemplateCache()
|
||||||
|
SetPromptCount()
|
||||||
Debug(message string)
|
Debug(message string)
|
||||||
Error(err error)
|
Error(err error)
|
||||||
Trace(start time.Time, args ...string)
|
Trace(start time.Time, args ...string)
|
||||||
|
@ -254,6 +259,7 @@ func (env *Shell) Init() {
|
||||||
env.cmdCache = &commandCache{
|
env.cmdCache = &commandCache{
|
||||||
commands: NewConcurrentMap(),
|
commands: NewConcurrentMap(),
|
||||||
}
|
}
|
||||||
|
env.SetPromptCount()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *Shell) resolveConfigPath() {
|
func (env *Shell) resolveConfigPath() {
|
||||||
|
@ -712,6 +718,7 @@ func (env *Shell) TemplateCache() *TemplateCache {
|
||||||
if env.tmplCache != nil {
|
if env.tmplCache != nil {
|
||||||
return env.tmplCache
|
return env.tmplCache
|
||||||
}
|
}
|
||||||
|
|
||||||
tmplCache := &TemplateCache{
|
tmplCache := &TemplateCache{
|
||||||
Root: env.Root(),
|
Root: env.Root(),
|
||||||
Shell: env.Shell(),
|
Shell: env.Shell(),
|
||||||
|
@ -719,8 +726,10 @@ func (env *Shell) TemplateCache() *TemplateCache {
|
||||||
Code: env.ErrorCode(),
|
Code: env.ErrorCode(),
|
||||||
WSL: env.IsWsl(),
|
WSL: env.IsWsl(),
|
||||||
Segments: make(map[string]interface{}),
|
Segments: make(map[string]interface{}),
|
||||||
|
PromptCount: env.CmdFlags.PromptCount,
|
||||||
}
|
}
|
||||||
tmplCache.Env = make(map[string]string)
|
tmplCache.Env = make(map[string]string)
|
||||||
|
|
||||||
const separator = "="
|
const separator = "="
|
||||||
values := os.Environ()
|
values := os.Environ()
|
||||||
for value := range values {
|
for value := range values {
|
||||||
|
@ -730,21 +739,25 @@ func (env *Shell) TemplateCache() *TemplateCache {
|
||||||
}
|
}
|
||||||
tmplCache.Env[key] = val
|
tmplCache.Env[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
pwd := env.Pwd()
|
pwd := env.Pwd()
|
||||||
tmplCache.PWD = ReplaceHomeDirPrefixWithTilde(env, pwd)
|
tmplCache.PWD = ReplaceHomeDirPrefixWithTilde(env, pwd)
|
||||||
tmplCache.Folder = Base(env, pwd)
|
tmplCache.Folder = Base(env, pwd)
|
||||||
if env.GOOS() == WINDOWS && strings.HasSuffix(tmplCache.Folder, ":") {
|
if env.GOOS() == WINDOWS && strings.HasSuffix(tmplCache.Folder, ":") {
|
||||||
tmplCache.Folder += `\`
|
tmplCache.Folder += `\`
|
||||||
}
|
}
|
||||||
|
|
||||||
tmplCache.UserName = env.User()
|
tmplCache.UserName = env.User()
|
||||||
if host, err := env.Host(); err == nil {
|
if host, err := env.Host(); err == nil {
|
||||||
tmplCache.HostName = host
|
tmplCache.HostName = host
|
||||||
}
|
}
|
||||||
|
|
||||||
goos := env.GOOS()
|
goos := env.GOOS()
|
||||||
tmplCache.OS = goos
|
tmplCache.OS = goos
|
||||||
if goos == LINUX {
|
if goos == LINUX {
|
||||||
tmplCache.OS = env.Platform()
|
tmplCache.OS = env.Platform()
|
||||||
}
|
}
|
||||||
|
|
||||||
env.tmplCache = tmplCache
|
env.tmplCache = tmplCache
|
||||||
return tmplCache
|
return tmplCache
|
||||||
}
|
}
|
||||||
|
@ -786,6 +799,28 @@ func dirMatchesOneOf(dir, home, goos string, regexes []string) bool {
|
||||||
return false
|
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 {
|
func IsPathSeparator(env Environment, c uint8) bool {
|
||||||
if c == '/' {
|
if c == '/' {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -2,6 +2,7 @@ export POSH_THEME=::CONFIG::
|
||||||
export POSH_PID=$$
|
export POSH_PID=$$
|
||||||
export POWERLINE_COMMAND="oh-my-posh"
|
export POWERLINE_COMMAND="oh-my-posh"
|
||||||
export CONDA_PROMPT_MODIFIER=false
|
export CONDA_PROMPT_MODIFIER=false
|
||||||
|
export POSH_PROMPT_COUNT=0
|
||||||
|
|
||||||
# set secondary prompt
|
# set secondary prompt
|
||||||
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=zsh)"
|
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_now=$(::OMP:: get millis --shell=zsh)
|
||||||
omp_elapsed=$(($omp_now-$omp_start_time))
|
omp_elapsed=$(($omp_now-$omp_start_time))
|
||||||
fi
|
fi
|
||||||
|
count=$((POSH_PROMPT_COUNT+1))
|
||||||
|
export POSH_PROMPT_COUNT=$count
|
||||||
set_poshcontext
|
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")"
|
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
|
unset omp_start_time
|
||||||
|
|
|
@ -88,6 +88,7 @@ func (t *Text) cleanTemplate() {
|
||||||
"WSL",
|
"WSL",
|
||||||
"Segments",
|
"Segments",
|
||||||
"Templates",
|
"Templates",
|
||||||
|
"PromptCount",
|
||||||
}
|
}
|
||||||
|
|
||||||
knownVariable := func(variable string) bool {
|
knownVariable := func(variable string) bool {
|
||||||
|
|
|
@ -14,7 +14,7 @@ offers a few standard properties to work with.
|
||||||
## Global properties
|
## Global properties
|
||||||
|
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
| --------------- | --------- | ------------------------------------- |
|
| --------------- | --------- | ----------------------------------------------------------------- |
|
||||||
| `.Root` | `boolean` | is the current user root/admin or not |
|
| `.Root` | `boolean` | is the current user root/admin or not |
|
||||||
| `.PWD` | `string` | the current working directory |
|
| `.PWD` | `string` | the current working directory |
|
||||||
| `.Folder` | `string` | the current working folder |
|
| `.Folder` | `string` | the current working folder |
|
||||||
|
@ -26,6 +26,7 @@ offers a few standard properties to work with.
|
||||||
| `.OS` | `string` | the operating system |
|
| `.OS` | `string` | the operating system |
|
||||||
| `.WSL` | `boolean` | in WSL yes/no |
|
| `.WSL` | `boolean` | in WSL yes/no |
|
||||||
| `.Templates` | `string` | the [templates][templates] result |
|
| `.Templates` | `string` | the [templates][templates] result |
|
||||||
|
| `.PromptCount` | `int` | the prompt counter, increments with 1 for every prompt invocation |
|
||||||
|
|
||||||
## Environment variables
|
## Environment variables
|
||||||
|
|
||||||
|
@ -177,6 +178,7 @@ to distinct between both. For example:
|
||||||
"template": "{{ .Segments.Hello.Output }} {{ .Segments.World.Output }}"
|
"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:
|
If you want to know if a specific segment is active, you can use the `.Segments.Contains` function, for example:
|
||||||
|
|
Loading…
Reference in a new issue