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 - `.PWD`: `string` - the current working directory
- `.Folder`: `string` - the current working folder - `.Folder`: `string` - the current working folder
- `.Shell`: `string` - the current shell name - `.Shell`: `string` - the current shell name
- `.ShellVersion`: `string` - the current shell version
- `.UserName`: `string` - the current user name - `.UserName`: `string` - the current user name
- `.HostName`: `string` - the host name - `.HostName`: `string` - the host name
- `.Code`: `int` - the last exit code - `.Code`: `int` - the last exit code

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -35,6 +35,7 @@ type Flags struct {
ErrorCode int ErrorCode int
Config string Config string
Shell string Shell string
ShellVersion string
PWD string PWD string
PSWD string PSWD string
ExecutionTime float64 ExecutionTime float64
@ -112,6 +113,7 @@ type TemplateCache struct {
PWD string PWD string
Folder string Folder string
Shell string Shell string
ShellVersion string
UserName string UserName string
HostName string HostName string
Code int Code int
@ -699,6 +701,7 @@ func (env *ShellEnvironment) TemplateCache() *TemplateCache {
tmplCache := &TemplateCache{ tmplCache := &TemplateCache{
Root: env.Root(), Root: env.Root(),
Shell: env.Shell(), Shell: env.Shell(),
ShellVersion: env.CmdFlags.ShellVersion,
Code: env.ErrorCode(), Code: env.ErrorCode(),
WSL: env.IsWsl(), WSL: env.IsWsl(),
} }

View file

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

View file

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

View file

@ -12,7 +12,7 @@ fi
# start timer on command start # start timer on command start
PS0='$(::OMP:: get millis > "$TIMER_START")' PS0='$(::OMP:: get millis > "$TIMER_START")'
# set secondary prompt # 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() { function _omp_hook() {
local ret=$? local ret=$?
@ -25,7 +25,7 @@ function _omp_hook() {
omp_elapsed=$((omp_now-omp_start_time)) omp_elapsed=$((omp_now-omp_start_time))
rm -f "$TIMER_START" rm -f "$TIMER_START"
fi 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 return $ret
} }

View file

@ -7,7 +7,7 @@ set --global omp_transient 0
function fish_prompt function fish_prompt
set --local omp_status_cache_temp $status set --local omp_status_cache_temp $status
if test "$omp_transient" = "1" 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 return
end end
set --global omp_status_cache $omp_status_cache_temp 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 set --global --export omp_last_status_generation $status_generation
end 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 end
function fish_right_prompt function fish_right_prompt
@ -35,14 +35,14 @@ function fish_right_prompt
return return
end end
if test -n "$omp_tooltip_command" 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" if test -n "$omp_tooltip_prompt"
echo -n $omp_tooltip_prompt echo -n $omp_tooltip_prompt
set omp_tooltip_command "" set omp_tooltip_command ""
return return
end end
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 end
function postexec_omp --on-event fish_postexec 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 it annoying when you have a multiline prompt
# making the behavior different compared to other shells # making the behavior different compared to other shells
let-env PROMPT_COMMAND_RIGHT = {''} let-env PROMPT_COMMAND_RIGHT = {''}
let-env NU_VERSION = (version | get version)
# PROMPTS # 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-env PROMPT_COMMAND = {
let width = (term size -c | get columns | into string) 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:POWERLINE_COMMAND = "oh-my-posh"
$env:CONDA_PROMPT_MODIFIER = $false $env:CONDA_PROMPT_MODIFIER = $false
$env:SHELL_VERSION = $PSVersionTable.PSVersion.ToString()
# specific module support (disabled by default) # specific module support (disabled by default)
$omp_value = $env:POSH_GIT_ENABLED $omp_value = $env:POSH_GIT_ENABLED
@ -73,7 +74,7 @@ function global:Initialize-ModuleSupport {
$omp = "::OMP::" $omp = "::OMP::"
$cleanPWD, $cleanPSWD = Get-PoshContext $cleanPWD, $cleanPSWD = Get-PoshContext
if ($env:POSH_TRANSIENT -eq $true) { 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 $env:POSH_TRANSIENT = $false
return return
} }
@ -110,7 +111,7 @@ function global:Initialize-ModuleSupport {
} }
Set-PoshContext Set-PoshContext
$terminalWidth = $Host.UI.RawUI.WindowSize.Width $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 # make sure PSReadLine knows we have a multiline prompt
$extraLines = ($standardOut | Measure-Object -Line).Lines - 1 $extraLines = ($standardOut | Measure-Object -Line).Lines - 1
if ($extraLines -gt 0) { if ($extraLines -gt 0) {
@ -180,7 +181,7 @@ function global:Enable-PoshTooltips {
$cursor = $null $cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor) [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$cursor)
$command = ($command -split " ")[0] $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 Write-Host $standardOut -NoNewline
$host.UI.RawUI.CursorPosition = $position $host.UI.RawUI.CursorPosition = $position
} }

View file

@ -17,7 +17,7 @@ function prompt_ohmyposh_precmd() {
omp_now=$(::OMP:: get millis) omp_now=$(::OMP:: get millis)
omp_elapsed=$(($omp_now-$omp_start_time)) omp_elapsed=$(($omp_now-$omp_start_time))
fi 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_start_time
unset omp_now unset omp_now
} }
@ -48,7 +48,7 @@ function self-insert() {
zle .self-insert zle .self-insert
return return
fi 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 # ignore an empty tooltip
if [[ ! -z "$tooltip" ]]; then if [[ ! -z "$tooltip" ]]; then
RPROMPT=$tooltip RPROMPT=$tooltip
@ -70,7 +70,7 @@ _posh-zle-line-init() {
local -i ret=$? local -i ret=$?
(( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2] (( $+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 zle .reset-prompt
# If we received EOT, we exit the shell # If we received EOT, we exit the shell

View file

@ -75,7 +75,7 @@ func (t *Text) cleanTemplate() {
*knownVariables = append(*knownVariables, splitted[0]) *knownVariables = append(*knownVariables, splitted[0])
return splitted[0], true 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) matches := regex.FindAllNamedRegexMatch(`(?: |{|\()(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
for _, match := range matches { for _, match := range matches {
if variable, OK := unknownVariable(match["var"], &knownVariables); OK { if variable, OK := unknownVariable(match["var"], &knownVariables); OK {