feat(tooltips): allow multiple tooltips for tip

Instead of rendering only the last tooltip that matches a given tip it
will now render all tooltips that trigger for that tip.
This commit is contained in:
Michael Rose 2024-06-14 08:12:33 +02:00 committed by Jan De Dobbeleer
parent ad4b27b8dd
commit 3ff30f993e
2 changed files with 40 additions and 16 deletions

View file

@ -8,32 +8,32 @@ import (
func (e *Engine) Tooltip(tip string) string {
tip = strings.Trim(tip, " ")
var tooltip *Segment
for _, tp := range e.Config.Tooltips {
if !tp.shouldInvokeWithTip(tip) {
tooltips := make([]*Segment, 0, 1)
for _, tooltip := range e.Config.Tooltips {
if !tooltip.shouldInvokeWithTip(tip) {
continue
}
tooltip = tp
if err := tooltip.mapSegmentWithWriter(e.Env); err != nil {
continue
}
if !tooltip.writer.Enabled() {
continue
}
tooltips = append(tooltips, tooltip)
}
if tooltip == nil {
if len(tooltips) == 0 {
return ""
}
if err := tooltip.mapSegmentWithWriter(e.Env); err != nil {
return ""
}
if !tooltip.writer.Enabled() {
return ""
}
tooltip.Enabled = true
// little hack to reuse the current logic
block := &Block{
Alignment: Right,
Segments: []*Segment{tooltip},
Segments: tooltips,
}
switch e.Env.Shell() {

View file

@ -47,4 +47,28 @@ This configuration will render a right-aligned git segment when you type `git` o
A tip should not include any spaces. Keep in mind that this is a blocking call, meaning that if the segment renders slow,
you can't type until it's visible. Optimizations in this space are being explored.
Note that you can also define multiple tooltips for the same tip to compose tooltips for individual commands. For example,
this configuration will render the AWS profile as well as the Azure subscription information when you type `terraform`
followed by a space.
<Config data={{
"blocks": [],
"tooltips": [
{
"type": "aws",
"tips": ["aws", "terraform"],
"style": "plain",
"foreground": "#e0af68",
"template": "\uf0e0f {{.Profile}}{{if .Region}}@{{.Region}}{{end}}"
},
{
"type": "az",
"tips": ["az", "terraform"],
"style": "plain",
"foreground": "#b4f9f8",
"template": "\uebd8 {{ .Name }}"
}
]
}}/>
[clink]: https://chrisant996.github.io/clink/