feat(context): allow context setting where possible

relates to #2776
This commit is contained in:
Jan De Dobbeleer 2022-09-23 20:44:53 +02:00 committed by Jan De Dobbeleer
parent 7c9cddf8f2
commit 17572d7f3f
4 changed files with 67 additions and 3 deletions

View file

@ -12,6 +12,11 @@ function _omp_start_timer() {
::OMP:: get millis
}
# template function for context loading
function set_poshcontext() {
return
}
function _omp_hook() {
local ret=$?
local omp_stack_count=$((${#DIRSTACK[@]} - 1))
@ -21,6 +26,7 @@ function _omp_hook() {
omp_elapsed=$((omp_now-omp_start_time))
omp_start_time=""
fi
set_poshcontext
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

@ -4,6 +4,11 @@ set --global CONDA_PROMPT_MODIFIER false
set --global omp_tooltip_command ""
set --global omp_transient 0
# template function for context loading
function set_poshcontext
return
end
function fish_prompt
set --local omp_status_cache_temp $status
# clear from cursor to end of screen as
@ -28,7 +33,7 @@ function fish_prompt
if set --query status_generation
set --global --export omp_last_status_generation $status_generation
end
set_poshcontext
::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
@ -94,6 +99,8 @@ end
# legacy functions
function enable_poshtooltips
return
end
function enable_poshtransientprompt
return
end

View file

@ -5,6 +5,11 @@ export CONDA_PROMPT_MODIFIER=false
# set secondary prompt
PS2="$(::OMP:: print secondary --config="$POSH_THEME" --shell=zsh)"
# template function for context loading
function set_poshcontext() {
return
}
function prompt_ohmyposh_preexec() {
omp_start_time=$(::OMP:: get millis)
}
@ -17,6 +22,7 @@ function prompt_ohmyposh_precmd() {
omp_now=$(::OMP:: get millis --shell=zsh)
omp_elapsed=$(($omp_now-$omp_start_time))
fi
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
unset omp_now

View file

@ -4,6 +4,9 @@ title: Templates
sidebar_label: Templates
---
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
Every segment has a `template` property to tweak the text that is displayed.
Under the hood, this uses go's [text/template][go-text-template] feature extended with [sprig][sprig] and
offers a few standard properties to work with.
@ -31,8 +34,20 @@ offers a few standard properties to work with.
| `.Env.VarName` | `string` | Any environment variable where `VarName` is the environment variable name |
:::tip
If you're using PowerShell, you can override a function to populate environment variables before the
prompt is rendered.
For the shells below, you can override a function to execute some logic before the prompt is rendered.
This can be used to for example populate an environment variable with a specific context.
<Tabs
defaultValue="powershell"
groupId="shell"
values={[
{ label: 'powershell', value: 'powershell', },
{ label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', },
{ label: 'fish', value: 'fish', },
]
}>
<TabItem value="powershell">
```powershell
function Set-EnvVar {
@ -41,6 +56,36 @@ function Set-EnvVar {
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force
```
</TabItem>
<TabItem value="zsh">
```bash
function set_poshcontext() {
export POSH=$(date)
}
```
</TabItem>
<TabItem value="bash">
```bash
function set_poshcontext() {
export POSH=$(date)
}
```
</TabItem>
<TabItem value="fish">
```shell
function set_poshcontext
set --export POSH $(date)
end
```
</TabItem>
</Tabs>
:::
## Template logic