Switch to a fullscreen layout with side-by-side frames

This commit is contained in:
Tony Grosinger 2022-08-05 08:36:03 -07:00
parent 61ed5836bd
commit 25bfb521a6
5 changed files with 53 additions and 24 deletions

9
go.mod
View File

@ -2,14 +2,15 @@ module github.com/tgrosinger/ledger-tui
go 1.18 go 1.18
require github.com/charmbracelet/bubbletea v0.22.0 require (
github.com/charmbracelet/bubbletea v0.22.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/spf13/cobra v1.5.0
)
require ( require (
github.com/atotto/clipboard v0.1.4 // indirect github.com/atotto/clipboard v0.1.4 // indirect
github.com/charmbracelet/lipgloss v0.5.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lrstanley/bubblezone v0.0.0-20220729154607-e408d1dc3890 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
) )

3
go.sum
View File

@ -14,8 +14,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lrstanley/bubblezone v0.0.0-20220729154607-e408d1dc3890 h1:mAJcJhjnJiwT3w9q1whqbnvDfezUSR+ECVb+B1j3D64=
github.com/lrstanley/bubblezone v0.0.0-20220729154607-e408d1dc3890/go.mod h1:CxaUrg7Y6DmnquTpb1Rgxib+u+NcRxrDi8m/mR1poTM=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
@ -24,7 +22,6 @@ github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 h1:kMlmsLSbjkikxQJ1IPwaM+7LJ9ltFu/fi8CRzvSnQmA= github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 h1:kMlmsLSbjkikxQJ1IPwaM+7LJ9ltFu/fi8CRzvSnQmA=
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=

View File

@ -12,11 +12,13 @@ import (
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/tgrosinger/ledger-tui/pkg/tui/currencyinput" "github.com/tgrosinger/ledger-tui/pkg/tui/currencyinput"
"github.com/tgrosinger/ledger-tui/pkg/tui/dateinput" "github.com/tgrosinger/ledger-tui/pkg/tui/dateinput"
"github.com/tgrosinger/ledger-tui/pkg/tui/postingsinput" "github.com/tgrosinger/ledger-tui/pkg/tui/postingsinput"
"github.com/tgrosinger/ledger-tui/pkg/tui/suggestions"
) )
var AddCmd = &cobra.Command{ var AddCmd = &cobra.Command{
@ -33,14 +35,15 @@ func executeAddTUI(cmd *cobra.Command, args []string) {
date: dateinput.New(time.Now(), true), date: dateinput.New(time.Now(), true),
description: textinput.New(), description: textinput.New(),
total: currencyinput.New("$"), total: currencyinput.New("$"),
suggestionBox: suggestions.New(),
help: help.New(), help: help.New(),
keymap: keymap{ keymap: keymap{
nextField: key.NewBinding( nextField: key.NewBinding(
key.WithKeys("tab"), key.WithKeys("tab"),
key.WithHelp("tab", "Next Field")), key.WithHelp("tab", "Next")),
prevField: key.NewBinding( prevField: key.NewBinding(
key.WithKeys("shift+tab"), key.WithKeys("shift+tab"),
key.WithHelp("shift+tab", "Previous Field")), key.WithHelp("shift+tab", "Previous")),
quit: key.NewBinding( quit: key.NewBinding(
key.WithKeys("esc", "ctrl+c"), key.WithKeys("esc", "ctrl+c"),
key.WithHelp("esc", "Quit")), key.WithHelp("esc", "Quit")),
@ -49,8 +52,9 @@ func executeAddTUI(cmd *cobra.Command, args []string) {
addTUI.description.Placeholder = "Transaction Description" addTUI.description.Placeholder = "Transaction Description"
addTUI.description.Validate = descriptionValidator addTUI.description.Validate = descriptionValidator
addTUI.description.Prompt = ""
p := tea.NewProgram(addTUI) p := tea.NewProgram(addTUI, tea.WithAltScreen())
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err) fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1) os.Exit(1)
@ -91,8 +95,14 @@ type AddTxTUI struct {
description textinput.Model description textinput.Model
total currencyinput.Model total currencyinput.Model
suggestionBox suggestions.Model
keymap keymap keymap keymap
help help.Model help help.Model
// halfFrame is a style which can fit two side by side, separated by a
// simple border. Will be resized when the window resizes.
halfFrame lipgloss.Style
} }
func (m AddTxTUI) Init() tea.Cmd { func (m AddTxTUI) Init() tea.Cmd {
@ -103,6 +113,11 @@ func (m AddTxTUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := make([]tea.Cmd, 3) cmds := make([]tea.Cmd, 3)
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.halfFrame = lipgloss.NewStyle().
Width((msg.Width/2)-2).
Height(msg.Height-4).
Border(lipgloss.NormalBorder(), true)
case dateinput.Msg: case dateinput.Msg:
switch msg { switch msg {
case dateinput.NextInputMsg: case dateinput.NextInputMsg:
@ -205,13 +220,11 @@ func (m AddTxTUI) helpView() string {
} }
func (m AddTxTUI) View() string { func (m AddTxTUI) View() string {
log.Println("Rendering LedgerTUI")
output := strings.Builder{} output := strings.Builder{}
output.WriteString("Tx Date: " + m.date.View()) output.WriteString("Date: " + m.date.View())
if m.furthestFocus >= description { if m.furthestFocus >= description {
output.WriteString("\nDescription: " + m.description.View()) output.WriteString("\nDesc: " + m.description.View())
} }
if m.furthestFocus >= total { if m.furthestFocus >= total {
@ -219,12 +232,10 @@ func (m AddTxTUI) View() string {
} }
// TODO: When accounts is focused, only show the account view. return lipgloss.JoinVertical(lipgloss.Left,
// The account view should have a text box at the top, and a list of lipgloss.JoinHorizontal(lipgloss.Top,
// suggestions below that can be selected. m.halfFrame.Render(output.String()),
// Maybe use a "next" button or something to make it less jarring. m.halfFrame.Render(m.suggestionBox.View())),
m.helpView(),
output.WriteString(m.helpView()) )
return output.String()
} }

View File

@ -2,7 +2,6 @@ package currencyinput
import ( import (
"errors" "errors"
"log"
"strconv" "strconv"
"strings" "strings"
@ -39,7 +38,6 @@ func numberValidator(s string) error {
} }
decimalPlaces := (len(s) - 1) - decimalIndex decimalPlaces := (len(s) - 1) - decimalIndex
log.Println(decimalPlaces)
if decimalPlaces > 2 { if decimalPlaces > 2 {
return errors.New("currency may only have two decimal places") return errors.New("currency may only have two decimal places")
} }

View File

@ -0,0 +1,22 @@
package suggestions
import tea "github.com/charmbracelet/bubbletea"
type Model struct {
}
func New() Model {
return Model{}
}
func Init() tea.Cmd {
return nil
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
}
func (m Model) View() string {
return "Suggestions"
}