90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
te "github.com/muesli/termenv"
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
)
|
|
|
|
var (
|
|
color = te.ColorProfile().Color
|
|
errorColor = "9"
|
|
blurredColor = "8"
|
|
focusedColor = "3"
|
|
focusedPrompt = te.String("> ").Foreground(color(focusedColor)).String()
|
|
blurredPrompt = "> "
|
|
)
|
|
|
|
func highlightText(text string) string {
|
|
return te.String(text).Foreground(color(focusedColor)).String()
|
|
}
|
|
|
|
func getButton(title string, focused bool) string {
|
|
clr := color(blurredColor)
|
|
if focused == true {
|
|
clr = color(focusedColor)
|
|
}
|
|
return "[ " + te.String(title).Foreground(clr).String() + " ]"
|
|
}
|
|
|
|
func updateIndex(msg tea.Msg, index int, length int, bindVim bool, buttons []int) (bool, int, bool) {
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
s := msg.String()
|
|
inputs := []string{"down", "tab", "j", "enter", "up", "shift+tab", "k"}
|
|
update := false
|
|
for i := 0; i < len(inputs); i++ {
|
|
if s == inputs[i] {
|
|
update = true
|
|
}
|
|
}
|
|
if !update {
|
|
return false, index, false
|
|
}
|
|
if !bindVim && (s == "j" || s == "k") {
|
|
return false, index, false
|
|
}
|
|
if s == "down" || s == "tab" || s == "j" || s == "enter" {
|
|
button := false
|
|
if len(buttons) == 0 {
|
|
button = true;
|
|
} else {
|
|
for i := 0; i < len(buttons); i++ {
|
|
if index == buttons[i] {
|
|
button = true
|
|
}
|
|
}
|
|
}
|
|
if s == "enter" && button {
|
|
return false, index, true
|
|
}
|
|
if index >= length - 1 {
|
|
return true, 0, false
|
|
}
|
|
index++
|
|
return true, index, false
|
|
} else {
|
|
if index <= 0 {
|
|
return true, length - 1, false
|
|
}
|
|
index--
|
|
return true, index, false
|
|
}
|
|
}
|
|
return false, index, false
|
|
}
|
|
|
|
func inputFocus(input textinput.Model) textinput.Model {
|
|
input.Focus()
|
|
input.Prompt = focusedPrompt
|
|
input.TextColor = focusedColor
|
|
return input
|
|
}
|
|
|
|
func inputBlur(input textinput.Model) textinput.Model {
|
|
input.Blur()
|
|
input.Prompt = blurredPrompt
|
|
input.TextColor = ""
|
|
return input
|
|
}
|