feat(shell): add version

resolves #2108
This commit is contained in:
Jan De Dobbeleer 2022-04-20 19:16:02 +02:00 committed by Jan De Dobbeleer
parent a55f94d4cd
commit 9214b88475
22 changed files with 47 additions and 76 deletions

View file

@ -14,6 +14,7 @@ offers a few standard properties to work with.
- `.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

View file

@ -42,5 +42,6 @@ Show the current shell name (ZSH, powershell, bash, ...).
### Properties
- `.Name`: `string` - the shell name
- `.Version`: `string` - the shell version
[templates]: /docs/configuration/templates

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
@ -24,8 +20,9 @@ var (
terminalWidth int
eval bool
command string
plain bool
command string
shellVersion string
plain bool
)
// printCmd represents the prompt command
@ -61,6 +58,7 @@ var printCmd = &cobra.Command{
TerminalWidth: terminalWidth,
Eval: eval,
Shell: shellName,
ShellVersion: shellVersion,
},
}
env.Init(false)
@ -122,6 +120,7 @@ func init() { // nolint:gochecknoinits
printCmd.Flags().StringVar(&pwd, "pwd", "", "current working directory")
printCmd.Flags().StringVar(&pswd, "pswd", "", "current working directory (according to pwsh)")
printCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
printCmd.Flags().StringVar(&shellVersion, "shell-version", "", "the shell version")
printCmd.Flags().IntVarP(&exitCode, "error", "e", 0, "last exit code")
printCmd.Flags().Float64Var(&timing, "execution-time", 0, "timing of the last command")
printCmd.Flags().IntVarP(&stackCount, "stack-count", "s", 0, "number of locations on the stack")

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -1,7 +1,3 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (

View file

@ -35,6 +35,7 @@ type Flags struct {
ErrorCode int
Config string
Shell string
ShellVersion string
PWD string
PSWD string
ExecutionTime float64
@ -108,16 +109,17 @@ type WifiInfo struct {
}
type TemplateCache struct {
Root bool
PWD string
Folder string
Shell string
UserName string
HostName string
Code int
Env map[string]string
OS string
WSL bool
Root bool
PWD string
Folder string
Shell string
ShellVersion string
UserName string
HostName string
Code int
Env map[string]string
OS string
WSL bool
}
type Environment interface {
@ -697,10 +699,11 @@ func (env *ShellEnvironment) TemplateCache() *TemplateCache {
return env.tmplCache
}
tmplCache := &TemplateCache{
Root: env.Root(),
Shell: env.Shell(),
Code: env.ErrorCode(),
WSL: env.IsWsl(),
Root: env.Root(),
Shell: env.Shell(),
ShellVersion: env.CmdFlags.ShellVersion,
Code: env.ErrorCode(),
WSL: env.IsWsl(),
}
tmplCache.Env = make(map[string]string)
const separator = "="

View file

@ -10,7 +10,8 @@ type Shell struct {
props properties.Properties
env environment.Environment
Name string
Name string
Version string
}
const (
@ -25,6 +26,7 @@ func (s *Shell) Template() string {
func (s *Shell) Enabled() bool {
mappedNames := s.props.GetKeyValueMap(MappedShellNames, make(map[string]string))
s.Name = s.env.Shell()
s.Version = s.env.Flags().ShellVersion
for key, val := range mappedNames {
if strings.EqualFold(s.Name, key) {
s.Name = val

View file

@ -1,6 +1,7 @@
package segments
import (
"oh-my-posh/environment"
"oh-my-posh/mock"
"oh-my-posh/properties"
"testing"
@ -12,6 +13,7 @@ func TestWriteCurrentShell(t *testing.T) {
expected := "zsh"
env := new(mock.MockedEnvironment)
env.On("Shell").Return(expected, nil)
env.On("Flags").Return(&environment.Flags{ShellVersion: "1.2.3"})
s := &Shell{
env: env,
props: properties.Map{},
@ -32,6 +34,7 @@ func TestUseMappedShellNames(t *testing.T) {
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Shell").Return(tc.Expected, nil)
env.On("Flags").Return(&environment.Flags{ShellVersion: "1.2.3"})
s := &Shell{
env: env,
props: properties.Map{

View file

@ -12,7 +12,7 @@ fi
# start timer on command start
PS0='$(::OMP:: get millis > "$TIMER_START")'
# set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=bash)"
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION")"
function _omp_hook() {
local ret=$?
@ -25,7 +25,7 @@ function _omp_hook() {
omp_elapsed=$((omp_now-omp_start_time))
rm -f "$TIMER_START"
fi
PS1="$(::OMP:: print primary --config="$POSH_THEME" --shell=bash --error="$ret" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" | tr -d '\0')"
PS1="$(::OMP:: print primary --config="$POSH_THEME" --shell=bash --shell-version="$BASH_VERSION" --error="$ret" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" | tr -d '\0')"
return $ret
}

View file

@ -7,7 +7,7 @@ set --global omp_transient 0
function fish_prompt
set --local omp_status_cache_temp $status
if test "$omp_transient" = "1"
::OMP:: print transient --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count
::OMP:: print transient --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION
return
end
set --global omp_status_cache $omp_status_cache_temp
@ -25,7 +25,7 @@ function fish_prompt
set --global --export omp_last_status_generation $status_generation
end
::OMP:: print primary --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count
::OMP:: print primary --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION
end
function fish_right_prompt
@ -35,14 +35,14 @@ function fish_right_prompt
return
end
if test -n "$omp_tooltip_command"
set omp_tooltip_prompt (::OMP:: print tooltip --config $POSH_THEME --shell fish --command $omp_tooltip_command)
set omp_tooltip_prompt (::OMP:: print tooltip --config $POSH_THEME --shell fish --shell-version $FISH_VERSION --command $omp_tooltip_command)
if test -n "$omp_tooltip_prompt"
echo -n $omp_tooltip_prompt
set omp_tooltip_command ""
return
end
end
::OMP:: print right --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count
::OMP:: print right --config $POSH_THEME --shell fish --error $omp_status_cache --execution-time $omp_duration --stack-count $omp_stack_count --shell-version $FISH_VERSION
end
function postexec_omp --on-event fish_postexec

View file

@ -5,11 +5,12 @@ let-env PROMPT_INDICATOR = ""
# making it annoying when you have a multiline prompt
# making the behavior different compared to other shells
let-env PROMPT_COMMAND_RIGHT = {''}
let-env NU_VERSION = (version | get version)
# PROMPTS
let-env PROMPT_MULTILINE_INDICATOR = (^::OMP:: print secondary $"--config=($env.POSH_THEME)" --shell=nu)
let-env PROMPT_MULTILINE_INDICATOR = (^::OMP:: print secondary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.NU_VERSION)")
let-env PROMPT_COMMAND = {
let width = (term size -c | get columns | into string)
^::OMP:: print primary $"--config=($env.POSH_THEME)" $"--execution-time=($env.CMD_DURATION_MS)" $"--error=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)"
^::OMP:: print primary $"--config=($env.POSH_THEME)" --shell=nu $"--shell-version=($env.NU_VERSION)" $"--execution-time=($env.CMD_DURATION_MS)" $"--error=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)"
}

View file

@ -26,6 +26,7 @@ function global:Start-Utf8Process {
$env:POWERLINE_COMMAND = "oh-my-posh"
$env:CONDA_PROMPT_MODIFIER = $false
$env:SHELL_VERSION = $PSVersionTable.PSVersion.ToString()
# specific module support (disabled by default)
$omp_value = $env:POSH_GIT_ENABLED
@ -73,7 +74,7 @@ function global:Initialize-ModuleSupport {
$omp = "::OMP::"
$cleanPWD, $cleanPSWD = Get-PoshContext
if ($env:POSH_TRANSIENT -eq $true) {
@(Start-Utf8Process $omp "print transient --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --config=""$Env:POSH_THEME""") -join "`n"
@(Start-Utf8Process $omp "print transient --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --config=""$Env:POSH_THEME"" --shell-version=""$env:SHELL_VERSION""") -join "`n"
$env:POSH_TRANSIENT = $false
return
}
@ -110,7 +111,7 @@ function global:Initialize-ModuleSupport {
}
Set-PoshContext
$terminalWidth = $Host.UI.RawUI.WindowSize.Width
$standardOut = @(Start-Utf8Process $omp "print primary --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --stack-count=$stackCount --config=""$Env:POSH_THEME"" --terminal-width=$terminalWidth")
$standardOut = @(Start-Utf8Process $omp "print primary --error=$global:OMP_ERRORCODE --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --execution-time=$global:OMP_EXECUTIONTIME --stack-count=$stackCount --config=""$Env:POSH_THEME"" --shell-version=""$env:SHELL_VERSION"" --terminal-width=$terminalWidth")
# make sure PSReadLine knows we have a multiline prompt
$extraLines = ($standardOut | Measure-Object -Line).Lines - 1
if ($extraLines -gt 0) {
@ -180,7 +181,7 @@ function global:Enable-PoshTooltips {
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor)
$command = ($command -split " ")[0]
$standardOut = @(Start-Utf8Process $omp "print tooltip --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$Env:POSH_THEME"" --command=""$command""")
$standardOut = @(Start-Utf8Process $omp "print tooltip --pwd=""$cleanPWD"" --pswd=""$cleanPSWD"" --config=""$Env:POSH_THEME"" --command=""$command"" --shell-version=""$env:SHELL_VERSION""")
Write-Host $standardOut -NoNewline
$host.UI.RawUI.CursorPosition = $position
}

View file

@ -17,7 +17,7 @@ function prompt_ohmyposh_precmd() {
omp_now=$(::OMP:: get millis)
omp_elapsed=$(($omp_now-$omp_start_time))
fi
eval "$(::OMP:: print primary --config="$POSH_THEME" --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh)"
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_now
}
@ -48,7 +48,7 @@ function self-insert() {
zle .self-insert
return
fi
tooltip=$(::OMP:: print tooltip --config="$POSH_THEME" --shell=zsh --command="$BUFFER")
tooltip=$(::OMP:: print tooltip --config="$POSH_THEME" --shell=zsh --command="$BUFFER" --shell-version="$ZSH_VERSION")
# ignore an empty tooltip
if [[ ! -z "$tooltip" ]]; then
RPROMPT=$tooltip
@ -70,7 +70,7 @@ _posh-zle-line-init() {
local -i ret=$?
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2]
eval "$(::OMP:: print transient --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --config="$POSH_THEME" --eval --shell=zsh)"
eval "$(::OMP:: print transient --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --config="$POSH_THEME" --eval --shell=zsh --shell-version="$ZSH_VERSION")"
zle .reset-prompt
# If we received EOT, we exit the shell

View file

@ -75,7 +75,7 @@ func (t *Text) cleanTemplate() {
*knownVariables = append(*knownVariables, splitted[0])
return splitted[0], true
}
knownVariables := []string{"Root", "PWD", "Folder", "Shell", "UserName", "HostName", "Env", "Data", "Code", "OS", "WSL"}
knownVariables := []string{"Root", "PWD", "Folder", "Shell", "ShellVersion", "UserName", "HostName", "Env", "Data", "Code", "OS", "WSL"}
matches := regex.FindAllNamedRegexMatch(`(?: |{|\()(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
for _, match := range matches {
if variable, OK := unknownVariable(match["var"], &knownVariables); OK {