mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-25 19:14:50 -08:00
feat: tooltip support
This commit is contained in:
parent
08c0d9a957
commit
dcada48c9d
8
.vscode/launch.json
vendored
8
.vscode/launch.json
vendored
|
@ -9,6 +9,14 @@
|
|||
"program": "${workspaceRoot}/src",
|
||||
"args": ["--config=${workspaceRoot}/themes/jandedobbeleer.omp.json", "--shell=pwsh"]
|
||||
},
|
||||
{
|
||||
"name": "Launch Tooltip",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "debug",
|
||||
"program": "${workspaceRoot}/src",
|
||||
"args": ["--config=/Users/jan/.jandedobbeleer.omp.json", "--tooltip=git"]
|
||||
},
|
||||
{
|
||||
"name": "Launch tests",
|
||||
"type": "go",
|
||||
|
|
|
@ -27,6 +27,7 @@ type Config struct {
|
|||
ConsoleTitleTemplate string `config:"console_title_template"`
|
||||
TerminalBackground string `config:"terminal_background"`
|
||||
Blocks []*Block `config:"blocks"`
|
||||
Tooltips []*Segment `config:"tooltips"`
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
@ -177,3 +177,43 @@ func (e *engine) print() string {
|
|||
}
|
||||
return e.string()
|
||||
}
|
||||
|
||||
func (e *engine) renderTooltip(tip string) string {
|
||||
tip = strings.Trim(tip, " ")
|
||||
var tooltip *Segment
|
||||
for _, tp := range e.config.Tooltips {
|
||||
if !tp.shouldInvokeWithTip(tip) {
|
||||
continue
|
||||
}
|
||||
tooltip = tp
|
||||
}
|
||||
if tooltip == nil {
|
||||
return ""
|
||||
}
|
||||
if err := tooltip.mapSegmentWithWriter(e.env); err != nil {
|
||||
return ""
|
||||
}
|
||||
if !tooltip.enabled() {
|
||||
return ""
|
||||
}
|
||||
tooltip.stringValue = tooltip.string()
|
||||
// little hack to reuse the current logic
|
||||
block := &Block{
|
||||
Alignment: Right,
|
||||
Segments: []*Segment{tooltip},
|
||||
}
|
||||
switch e.env.getShellName() {
|
||||
case zsh:
|
||||
block.init(e.env, e.colorWriter, e.ansi)
|
||||
return block.renderSegments()
|
||||
case pwsh, powershell5:
|
||||
block.initPlain(e.env, e.config)
|
||||
tooltipText := block.renderSegments()
|
||||
e.write(e.ansi.clearEOL)
|
||||
e.write(e.ansi.carriageForward())
|
||||
e.write(e.ansi.getCursorForRightWrite(tooltipText, 0))
|
||||
e.write(tooltipText)
|
||||
return e.string()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ Set-DefaultEnvValue("POSH_GIT_ENABLED")
|
|||
|
||||
$global:PoshSettings = New-Object -TypeName PSObject -Property @{
|
||||
Theme = "";
|
||||
EnableToolTips = $false;
|
||||
}
|
||||
|
||||
# used to detect empty hit
|
||||
|
@ -56,6 +57,24 @@ function global:Initialize-ModuleSupport {
|
|||
}
|
||||
catch {}
|
||||
}
|
||||
|
||||
# Set the keyhandler to enable tooltips
|
||||
if ($global:PoshSettings.EnableToolTips -eq $true) {
|
||||
Set-PSReadlineKeyHandler -Key SpaceBar -ScriptBlock {
|
||||
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
|
||||
$position = $host.UI.RawUI.CursorPosition
|
||||
$omp = "::OMP::"
|
||||
$config = $global:PoshSettings.Theme
|
||||
$cleanPWD = $PWD.ProviderPath.TrimEnd("\")
|
||||
$cleanPSWD = $PWD.ToString().TrimEnd("\")
|
||||
$tooltip = $null
|
||||
$cursor = $null
|
||||
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$tooltip, [ref]$cursor)
|
||||
$standardOut = @(&$omp --pwd="$cleanPWD" --pswd="$cleanPSWD" --config="$config" --tooltip="$tooltip" 2>&1)
|
||||
Write-Host $standardOut -NoNewline
|
||||
$host.UI.RawUI.CursorPosition = $position
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[ScriptBlock]$Prompt = {
|
||||
|
|
|
@ -50,3 +50,22 @@ function export_poshconfig() {
|
|||
fi
|
||||
::OMP:: --config $POSH_THEME --print-config --config-format $format > $1
|
||||
}
|
||||
|
||||
function self-insert() {
|
||||
# ignore an empty buffer
|
||||
if [[ -z "$BUFFER" ]]; then
|
||||
zle .self-insert
|
||||
return
|
||||
fi
|
||||
tooltip=$(::OMP:: --config $POSH_THEME --shell zsh --tooltip $BUFFER)
|
||||
# ignore an empty tooltip
|
||||
if [[ ! -z "$tooltip" ]]; then
|
||||
RPROMPT=$tooltip
|
||||
zle reset-prompt
|
||||
fi
|
||||
zle .self-insert
|
||||
}
|
||||
|
||||
function enable_poshtooltips() {
|
||||
zle -N self-insert
|
||||
}
|
||||
|
|
10
src/main.go
10
src/main.go
|
@ -57,6 +57,7 @@ type args struct {
|
|||
CursorPadding *int
|
||||
RPromptOffset *int
|
||||
StackCount *int
|
||||
ToolTip *string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -141,6 +142,10 @@ func main() {
|
|||
"stack-count",
|
||||
0,
|
||||
"The current location stack count"),
|
||||
ToolTip: flag.String(
|
||||
"tooltip",
|
||||
"",
|
||||
"Render a tooltip based on the string value"),
|
||||
}
|
||||
flag.Parse()
|
||||
env := &environment{}
|
||||
|
@ -191,11 +196,14 @@ func main() {
|
|||
consoleTitle: title,
|
||||
ansi: ansi,
|
||||
}
|
||||
|
||||
if *args.Debug {
|
||||
fmt.Print(engine.debug())
|
||||
return
|
||||
}
|
||||
if len(*args.ToolTip) != 0 {
|
||||
fmt.Print(engine.renderTooltip(*args.ToolTip))
|
||||
return
|
||||
}
|
||||
prompt := engine.render()
|
||||
if !*args.ExportPNG {
|
||||
fmt.Print(prompt)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
// Segment represent a single segment and it's configuration
|
||||
type Segment struct {
|
||||
Type SegmentType `config:"type"`
|
||||
Tips []string `config:"tips"`
|
||||
Style SegmentStyle `config:"style"`
|
||||
PowerlineSymbol string `config:"powerline_symbol"`
|
||||
InvertPowerline bool `config:"invert_powerline"`
|
||||
|
@ -198,6 +199,15 @@ func (segment *Segment) getColor(templates []string, defaultColor string) string
|
|||
return defaultColor
|
||||
}
|
||||
|
||||
func (segment *Segment) shouldInvokeWithTip(tip string) bool {
|
||||
for _, t := range segment.Tips {
|
||||
if t == tip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (segment *Segment) foreground() string {
|
||||
color := segment.Foreground
|
||||
if segment.props != nil {
|
||||
|
|
Loading…
Reference in a new issue