2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-04-26 23:43:22 +00:00

feat: implement alias for completion groups of type list

This commit is contained in:
sammyette 2025-04-24 11:57:25 -04:00
parent 8437a3b4bb
commit 8ea0667bdb
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD

17
rl.go
View File

@ -139,9 +139,11 @@ 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) itemDisplays := make(map[string]string)
itemAliases := 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 {
// TODO: remove in 3.0
// ['--flag'] = {'description', '--flag-alias'} // ['--flag'] = {'description', '--flag-alias'}
// OR // OR
// ['--flag'] = {description = '', alias = '', display = ''} // ['--flag'] = {description = '', alias = '', display = ''}
@ -156,19 +158,21 @@ func newLineReader(prompt string, noHist bool) *lineReader {
itemDescription, ok := vlTbl.Get(rt.IntValue(1)).TryString() itemDescription, ok := vlTbl.Get(rt.IntValue(1)).TryString()
if !ok { if !ok {
// if we can't get it by number index, try by string key // if we can't get it by number index, try by string key
itemDescription, ok = vlTbl.Get(rt.StringValue("description")).TryString() itemDescription, _ = vlTbl.Get(rt.StringValue("description")).TryString()
if !ok {
// TODO: error?
return
}
} }
itemDescriptions[itemName] = itemDescription itemDescriptions[itemName] = itemDescription
// display // 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 { if itemDisplay, ok := vlTbl.Get(rt.StringValue("display")).TryString(); ok {
itemDisplays[itemName] = itemDisplay itemDisplays[itemName] = itemDisplay
} }
itemAlias, ok := vlTbl.Get(rt.IntValue(2)).TryString()
if !ok {
// if we can't get it by number index, try by string key
itemAlias, _ = vlTbl.Get(rt.StringValue("alias")).TryString()
}
itemAliases[itemName] = itemAlias
} else if keytyp == rt.IntType { } else if keytyp == rt.IntType {
vlStr, ok := lval.TryString() vlStr, ok := lval.TryString()
if !ok { if !ok {
@ -192,6 +196,7 @@ func newLineReader(prompt string, noHist bool) *lineReader {
compGroups = append(compGroups, &readline.CompletionGroup{ compGroups = append(compGroups, &readline.CompletionGroup{
DisplayType: dispType, DisplayType: dispType,
Aliases: itemAliases,
Descriptions: itemDescriptions, Descriptions: itemDescriptions,
ItemDisplays: itemDisplays, ItemDisplays: itemDisplays,
Suggestions: items, Suggestions: items,