fix list index, add blank line function

This commit is contained in:
Shane C 2024-08-08 10:43:10 -04:00
parent 538949679f
commit 628eb8343d
Signed by: shanec
GPG key ID: E46B5FEA35B22FF9
2 changed files with 24 additions and 6 deletions

20
ansi.go
View file

@ -1,6 +1,11 @@
package tui package tui
import "strings" import (
"fmt"
"golang.org/x/term"
"os"
"strings"
)
type Colors = string type Colors = string
type Fmt = string type Fmt = string
@ -34,6 +39,19 @@ const (
FgColorRed = "\033[38;5;167m" FgColorRed = "\033[38;5;167m"
) )
func BlankLine() {
width, _, _ := term.GetSize(int(os.Stdin.Fd()))
// Assume ptero terminal if no width.
if width == 0 {
fmt.Print("\033[1B")
} else {
fmt.Println("")
}
}
func Format(codes ...string) string { func Format(codes ...string) string {
var str strings.Builder var str strings.Builder

View file

@ -241,16 +241,16 @@ func (l *ListData) renderList() {
if _, ok := item.Value.(string); ok { if _, ok := item.Value.(string); ok {
if item.Value == "action_back" { if item.Value == "action_back" {
listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorRed) + string(rune(index+1)) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset + listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorRed) + strconv.Itoa(index+1) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset +
fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n" fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n"
//listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;167m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor) //listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;167m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
} else { } else {
listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorGold) + string(rune(index+1)) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset + listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorGold) + strconv.Itoa(index+1) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset +
fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n" fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n"
//listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor) //listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
} }
} else { } else {
listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorGold) + string(rune(index+1)) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset + listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorGold) + strconv.Itoa(index+1) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset +
fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n" fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n"
//listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor) //listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
} }
@ -282,7 +282,7 @@ func (l *ListData) inputHandler(items []ListItem) ListItem {
totalLineNum = len(l.strLengths) totalLineNum = len(l.strLengths)
} else { } else {
for _, strLength := range l.strLengths { for _, strLength := range l.strLengths {
totalLineNum += ((strLength) / width) totalLineNum += (strLength) / width
} }
} }
totalLineNum++ //User input line totalLineNum++ //User input line
@ -312,7 +312,7 @@ func (l *ListData) inputHandler(items []ListItem) ListItem {
totalLineNum = len(l.strLengths) totalLineNum = len(l.strLengths)
} else { } else {
for _, strLength := range l.strLengths { for _, strLength := range l.strLengths {
totalLineNum += ((strLength) / width) totalLineNum += (strLength) / width
} }
} }
totalLineNum++ //User input line totalLineNum++ //User input line