2022-08-04 08:39:37 -07:00
|
|
|
package add
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-08-05 08:36:03 -07:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2022-08-04 08:39:37 -07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/tgrosinger/ledger-tui/pkg/tui/currencyinput"
|
|
|
|
"github.com/tgrosinger/ledger-tui/pkg/tui/dateinput"
|
|
|
|
"github.com/tgrosinger/ledger-tui/pkg/tui/postingsinput"
|
2022-08-05 08:36:03 -07:00
|
|
|
"github.com/tgrosinger/ledger-tui/pkg/tui/suggestions"
|
2022-08-04 08:39:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var AddCmd = &cobra.Command{
|
|
|
|
Use: "add",
|
|
|
|
Short: "Add a new transaction to a ledger file",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: executeAddTUI,
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeAddTUI(cmd *cobra.Command, args []string) {
|
2022-12-12 08:26:10 -08:00
|
|
|
f, err := cmd.Flags().GetString("file")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Missing ledger file path: %s", err.Error())
|
|
|
|
}
|
|
|
|
log.Println("Using ledger file in " + f)
|
|
|
|
|
2022-08-04 08:39:37 -07:00
|
|
|
addTUI := AddTxTUI{
|
|
|
|
focused: date,
|
|
|
|
furthestFocus: date,
|
|
|
|
date: dateinput.New(time.Now(), true),
|
|
|
|
description: textinput.New(),
|
|
|
|
total: currencyinput.New("$"),
|
2022-12-12 08:26:10 -08:00
|
|
|
suggestionBox: suggestions.New(f),
|
2022-08-04 08:39:37 -07:00
|
|
|
help: help.New(),
|
|
|
|
keymap: keymap{
|
|
|
|
nextField: key.NewBinding(
|
|
|
|
key.WithKeys("tab"),
|
2022-08-05 08:36:03 -07:00
|
|
|
key.WithHelp("tab", "Next")),
|
2022-08-04 08:39:37 -07:00
|
|
|
prevField: key.NewBinding(
|
|
|
|
key.WithKeys("shift+tab"),
|
2022-08-05 08:36:03 -07:00
|
|
|
key.WithHelp("shift+tab", "Previous")),
|
2022-08-04 08:39:37 -07:00
|
|
|
quit: key.NewBinding(
|
|
|
|
key.WithKeys("esc", "ctrl+c"),
|
|
|
|
key.WithHelp("esc", "Quit")),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
addTUI.description.Placeholder = "Transaction Description"
|
|
|
|
addTUI.description.Validate = descriptionValidator
|
2022-08-05 08:36:03 -07:00
|
|
|
addTUI.description.Prompt = ""
|
2022-08-04 08:39:37 -07:00
|
|
|
|
2022-08-05 08:36:03 -07:00
|
|
|
p := tea.NewProgram(addTUI, tea.WithAltScreen())
|
2022-08-04 08:39:37 -07:00
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
fmt.Printf("Alas, there's been an error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func descriptionValidator(s string) error {
|
|
|
|
// The description must not have a line break
|
|
|
|
if strings.Contains(s, "\n") {
|
|
|
|
return errors.New("description must not contain a new line")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type focus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
none focus = iota
|
|
|
|
date
|
|
|
|
description
|
|
|
|
total
|
|
|
|
lines
|
|
|
|
)
|
|
|
|
|
|
|
|
type keymap struct {
|
|
|
|
nextField key.Binding
|
|
|
|
prevField key.Binding
|
|
|
|
//increment key.Binding
|
|
|
|
//decrement key.Binding
|
|
|
|
quit key.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddTxTUI struct {
|
|
|
|
// Add New Tx Form
|
|
|
|
focused focus
|
|
|
|
furthestFocus focus
|
2022-12-12 08:26:10 -08:00
|
|
|
|
|
|
|
// Fields
|
|
|
|
date dateinput.Model
|
|
|
|
description textinput.Model
|
|
|
|
total currencyinput.Model
|
2022-08-04 08:39:37 -07:00
|
|
|
|
2022-08-05 08:36:03 -07:00
|
|
|
suggestionBox suggestions.Model
|
|
|
|
|
2022-08-04 08:39:37 -07:00
|
|
|
keymap keymap
|
|
|
|
help help.Model
|
2022-08-05 08:36:03 -07:00
|
|
|
|
|
|
|
// 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
|
2022-08-04 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m AddTxTUI) Init() tea.Cmd {
|
2022-12-12 08:26:10 -08:00
|
|
|
return tea.Batch(
|
|
|
|
m.date.Init(),
|
|
|
|
m.total.Init(),
|
|
|
|
m.suggestionBox.Init(),
|
|
|
|
)
|
2022-08-04 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m AddTxTUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
cmds := make([]tea.Cmd, 3)
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
2022-08-05 08:36:03 -07:00
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
m.halfFrame = lipgloss.NewStyle().
|
|
|
|
Width((msg.Width/2)-2).
|
|
|
|
Height(msg.Height-4).
|
|
|
|
Border(lipgloss.NormalBorder(), true)
|
2022-08-04 08:39:37 -07:00
|
|
|
case dateinput.Msg:
|
|
|
|
switch msg {
|
|
|
|
case dateinput.NextInputMsg:
|
|
|
|
if m.focused == date {
|
|
|
|
log.Println("Focusing description")
|
|
|
|
m.focused = description
|
|
|
|
}
|
|
|
|
case dateinput.PrevInputMsg:
|
|
|
|
// No input field before date, so ignore
|
|
|
|
}
|
|
|
|
case postingsinput.Msg:
|
|
|
|
switch msg {
|
|
|
|
case postingsinput.NextInputMsg:
|
|
|
|
if m.focused == lines {
|
|
|
|
// TODO: Focus on save button
|
|
|
|
}
|
|
|
|
case postingsinput.PrevInputMsg:
|
|
|
|
if m.focused == lines {
|
|
|
|
m.focused = total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.Type {
|
|
|
|
|
|
|
|
// These keys should exit the program.
|
|
|
|
case tea.KeyCtrlC, tea.KeyEsc:
|
|
|
|
return m, tea.Quit
|
|
|
|
|
|
|
|
// Navigate between fields
|
|
|
|
case tea.KeyTab:
|
|
|
|
if m.focused == description {
|
|
|
|
m.focused = total
|
|
|
|
} else if m.focused == total {
|
|
|
|
m.focused = lines
|
|
|
|
}
|
|
|
|
case tea.KeyShiftTab:
|
|
|
|
if m.focused == description {
|
|
|
|
m.focused = date
|
|
|
|
} else if m.focused == total {
|
|
|
|
m.focused = description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.setFocus()
|
|
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.date, cmd = m.date.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
m.description, cmd = m.description.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
m.total, cmd = m.total.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
2022-12-12 08:26:10 -08:00
|
|
|
m.suggestionBox, cmd = m.suggestionBox.Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
2022-08-04 08:39:37 -07:00
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddTxTUI) setFocus() {
|
|
|
|
if m.focused != date && m.date.Focused() {
|
|
|
|
m.date.Blur()
|
|
|
|
}
|
|
|
|
if m.focused != description && m.description.Focused() {
|
|
|
|
m.description.Blur()
|
|
|
|
}
|
|
|
|
if m.focused != total && m.total.Focused() {
|
|
|
|
m.total.Blur()
|
|
|
|
}
|
|
|
|
// TODO: Blur accounts
|
|
|
|
|
|
|
|
switch m.focused {
|
|
|
|
case date:
|
|
|
|
m.date.Focus()
|
|
|
|
if m.furthestFocus < date {
|
|
|
|
m.furthestFocus = date
|
|
|
|
}
|
|
|
|
case description:
|
|
|
|
m.description.Focus()
|
|
|
|
if m.furthestFocus < description {
|
|
|
|
m.furthestFocus = description
|
|
|
|
}
|
|
|
|
case total:
|
|
|
|
m.total.Focus()
|
|
|
|
if m.furthestFocus < total {
|
|
|
|
m.furthestFocus = total
|
|
|
|
}
|
|
|
|
case lines:
|
|
|
|
// TODO: Focus accounts
|
|
|
|
if m.furthestFocus < lines {
|
|
|
|
m.furthestFocus = lines
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m AddTxTUI) helpView() string {
|
|
|
|
return "\n" + m.help.ShortHelpView([]key.Binding{
|
|
|
|
m.keymap.nextField,
|
|
|
|
m.keymap.prevField,
|
|
|
|
m.keymap.quit,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m AddTxTUI) View() string {
|
|
|
|
output := strings.Builder{}
|
2022-08-05 08:36:03 -07:00
|
|
|
output.WriteString("Date: " + m.date.View())
|
2022-08-04 08:39:37 -07:00
|
|
|
|
|
|
|
if m.furthestFocus >= description {
|
2022-08-05 08:36:03 -07:00
|
|
|
output.WriteString("\nDesc: " + m.description.View())
|
2022-08-04 08:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.furthestFocus >= total {
|
|
|
|
output.WriteString("\nTotal: " + m.total.View())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-05 08:36:03 -07:00
|
|
|
return lipgloss.JoinVertical(lipgloss.Left,
|
|
|
|
lipgloss.JoinHorizontal(lipgloss.Top,
|
|
|
|
m.halfFrame.Render(output.String()),
|
|
|
|
m.halfFrame.Render(m.suggestionBox.View())),
|
|
|
|
m.helpView(),
|
|
|
|
)
|
2022-08-04 08:39:37 -07:00
|
|
|
}
|