repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / tui / common
Antonio Mika · 31 May 24

views.go

  1package common
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/charmbracelet/lipgloss"
  8)
  9
 10// State is a general UI state used to help style components.
 11type State int
 12
 13// UI states.
 14const (
 15	StateNormal State = iota
 16	StateSelected
 17	StateActive
 18	StateSpecial
 19	StateDeleting
 20)
 21
 22var lineColors = map[State]lipgloss.TerminalColor{
 23	StateNormal:   lipgloss.AdaptiveColor{Light: "#BCBCBC", Dark: "#646464"},
 24	StateSelected: lipgloss.Color("#F684FF"),
 25	StateDeleting: lipgloss.AdaptiveColor{Light: "#FF8BA7", Dark: "#893D4E"},
 26	StateSpecial:  lipgloss.Color("#04B575"),
 27}
 28
 29// VerticalLine return a vertical line colored according to the given state.
 30func VerticalLine(renderer *lipgloss.Renderer, state State) string {
 31	return renderer.NewStyle().
 32		SetString("│").
 33		Foreground(lineColors[state]).
 34		String()
 35}
 36
 37// KeyValueView renders key-value pairs.
 38func KeyValueView(styles Styles, stuff ...string) string {
 39	if len(stuff) == 0 {
 40		return ""
 41	}
 42
 43	var (
 44		s     string
 45		index int
 46	)
 47	for i := 0; i < len(stuff); i++ {
 48		if i%2 == 0 {
 49			// even: key
 50			s += fmt.Sprintf("%s %s: ", VerticalLine(styles.Renderer, StateNormal), stuff[i])
 51			continue
 52		}
 53		// odd: value
 54		s += styles.Label.Render(stuff[i])
 55		s += "\n"
 56		index++
 57	}
 58
 59	return strings.TrimSpace(s)
 60}
 61
 62// OKButtonView returns a button reading "OK".
 63func OKButtonView(styles Styles, focused bool, defaultButton bool) string {
 64	return styledButton(styles, "OK", defaultButton, focused)
 65}
 66
 67// CancelButtonView returns a button reading "Cancel.".
 68func CancelButtonView(styles Styles, focused bool, defaultButton bool) string {
 69	return styledButton(styles, "Cancel", defaultButton, focused)
 70}
 71
 72func styledButton(styles Styles, str string, underlined, focused bool) string {
 73	var st lipgloss.Style
 74	if focused {
 75		st = styles.FocusedButtonStyle
 76	} else {
 77		st = styles.BlurredButtonStyle
 78	}
 79	if underlined {
 80		st = st.Underline(true)
 81	}
 82	return st.Render(str)
 83}
 84
 85// HelpView renders text intended to display at help text, often at the
 86// bottom of a view.
 87func HelpView(styles Styles, sections ...string) string {
 88	var s string
 89	if len(sections) == 0 {
 90		return s
 91	}
 92
 93	for i := 0; i < len(sections); i++ {
 94		s += styles.HelpSection.Render(sections[i])
 95		if i < len(sections)-1 {
 96			s += styles.HelpDivider.Render()
 97		}
 98	}
 99
100	return s
101}
102
103func LogoView() string {
104	return `
105    .                   ."
106   i-~l^             'I~??!
107  I??_??-<I^     .,!_??+<-?I
108  _-+ .,!+??->:;<??-<;'  +-_
109 '-?i     ':i_??_!".     i?-'
110  _-+         ''         +-_
111  I??I                  I??I
112   !??l.              .l??i
113    ;_?_I'          'I_?_;
114     .I+??_>l:,,:l>_??+I.
115        ';i+--??--+i;'
116             ....`
117}