2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-04-27 07:53:23 +00:00

feat: implement display for completion groups of type list

This commit is contained in:
sammyette 2025-04-24 11:47:11 -04:00
parent ef4c925e37
commit 8437a3b4bb
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 35 additions and 2 deletions

View File

@ -47,3 +47,16 @@ end)
bait.catch('hilbish.notification', function(notif) bait.catch('hilbish.notification', function(notif)
doNotifyPrompt() doNotifyPrompt()
end) end)
hilbish.complete('command.comp', function(query, ctx, fields)
local cg = {
items = {
'list item 1',
['--command-flag-here'] = {'this does a thing', '--the-flag-alias'},
['--styled-command-flag-here'] = {'this does a thing', '--the-flag-alias', display = lunacolors.blue '--styled-command-flag-here'}
},
type = 'list'
}
return {cg}, prefix
end)

View File

@ -14,6 +14,7 @@ type CompletionGroup struct {
Suggestions []string Suggestions []string
Aliases map[string]string // A candidate has an alternative name (ex: --long, -l option flags) Aliases map[string]string // A candidate has an alternative name (ex: --long, -l option flags)
Descriptions map[string]string // Items descriptions Descriptions map[string]string // Items descriptions
ItemDisplays map[string]string // What to display the item as (can be used for styling items)
DisplayType TabDisplayType // Map, list or normal DisplayType TabDisplayType // Map, list or normal
MaxLength int // Each group can be limited in the number of comps offered MaxLength int // Each group can be limited in the number of comps offered

View File

@ -217,6 +217,11 @@ func (g *CompletionGroup) writeList(rl *Instance) (comp string) {
alt = strings.Repeat(" ", maxLengthAlt+1) // + 2 to keep account of spaces alt = strings.Repeat(" ", maxLengthAlt+1) // + 2 to keep account of spaces
} }
styledSugg, ok := g.ItemDisplays[item]
if ok {
sugg = fmt.Sprintf("\r%s%-"+cellWidth+"s", highlight(y, 0), fmtEscape(styledSugg))
}
// Description // Description
description := g.Descriptions[g.Suggestions[i]] description := g.Descriptions[g.Suggestions[i]]
if len(description) > maxDescWidth { if len(description) > maxDescWidth {

18
rl.go
View File

@ -138,10 +138,13 @@ func newLineReader(prompt string, noHist bool) *lineReader {
items := []string{} items := []string{}
itemDescriptions := make(map[string]string) itemDescriptions := make(map[string]string)
itemDisplays := make(map[string]string)
util.ForEach(luaCompItems.AsTable(), func(lkey rt.Value, lval rt.Value) { util.ForEach(luaCompItems.AsTable(), func(lkey rt.Value, lval rt.Value) {
if keytyp := lkey.Type(); keytyp == rt.StringType { if keytyp := lkey.Type(); keytyp == rt.StringType {
// ['--flag'] = {'description', '--flag-alias'} // ['--flag'] = {'description', '--flag-alias'}
// OR
// ['--flag'] = {description = '', alias = '', display = ''}
itemName, ok := lkey.TryString() itemName, ok := lkey.TryString()
vlTbl, okk := lval.TryTable() vlTbl, okk := lval.TryTable()
if !ok && !okk { if !ok && !okk {
@ -152,10 +155,20 @@ func newLineReader(prompt string, noHist bool) *lineReader {
items = append(items, itemName) items = append(items, itemName)
itemDescription, ok := vlTbl.Get(rt.IntValue(1)).TryString() itemDescription, ok := vlTbl.Get(rt.IntValue(1)).TryString()
if !ok { if !ok {
// TODO: error // if we can't get it by number index, try by string key
return itemDescription, ok = vlTbl.Get(rt.StringValue("description")).TryString()
if !ok {
// TODO: error?
return
}
} }
itemDescriptions[itemName] = itemDescription itemDescriptions[itemName] = itemDescription
// display
// this is optional, so only act when we get it and it's a string
if itemDisplay, ok := vlTbl.Get(rt.StringValue("display")).TryString(); ok {
itemDisplays[itemName] = itemDisplay
}
} else if keytyp == rt.IntType { } else if keytyp == rt.IntType {
vlStr, ok := lval.TryString() vlStr, ok := lval.TryString()
if !ok { if !ok {
@ -180,6 +193,7 @@ func newLineReader(prompt string, noHist bool) *lineReader {
compGroups = append(compGroups, &readline.CompletionGroup{ compGroups = append(compGroups, &readline.CompletionGroup{
DisplayType: dispType, DisplayType: dispType,
Descriptions: itemDescriptions, Descriptions: itemDescriptions,
ItemDisplays: itemDisplays,
Suggestions: items, Suggestions: items,
TrimSlash: false, TrimSlash: false,
NoSpace: true, NoSpace: true,