Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/tofiles #32

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: clean up msg handlers by mode
  • Loading branch information
bashbunni committed Aug 11, 2023
commit a489f1bd52e299a16553594a83535521b52ad457
12 changes: 6 additions & 6 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ type keymap struct {
Delete key.Binding
Back key.Binding
Quit key.Binding
Open key.Binding
Edit key.Binding
}

// Keymap reusable key mappings shared across models
var Keymap = keymap{
Create: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "create"),
key.WithKeys("n"),
key.WithHelp("n", "new"),
),
Enter: key.NewBinding(
key.WithKeys("enter"),
Expand All @@ -55,8 +55,8 @@ var Keymap = keymap{
key.WithKeys("ctrl+c", "q"),
key.WithHelp("ctrl+c/q", "quit"),
),
Open: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "open in editor"),
Edit: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "open in editor"),
),
}
2 changes: 1 addition & 1 deletion entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (m Entry) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case key.Matches(msg, Keymap.Create):
cmds = append(cmds, openEditorCmd(NewFilePath(m.path)))
case key.Matches(msg, Keymap.Open):
case key.Matches(msg, Keymap.Edit):
if len(m.entries) == 0 {
cmds = append(cmds, openEditorCmd(NewFilePath(m.path)))
} else {
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/bubbletea v0.24.3-0.20230724163731-91dd12007337
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.7.1
)
Expand All @@ -20,20 +20,20 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/yuin/goldmark v1.5.2 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5
github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=
github.com/charmbracelet/bubbletea v0.24.2 h1:uaQIKx9Ai6Gdh5zpTbGiWpytMU+CfsPp06RaW2cx/SY=
github.com/charmbracelet/bubbletea v0.24.2/go.mod h1:XdrNrV4J8GiyshTtx3DNuYkR1FDaJmO3l2nejekbsgg=
github.com/charmbracelet/bubbletea v0.24.3-0.20230724163731-91dd12007337 h1:k0PGZqmFg5sgqaSnFnoqCZKDfgmzSLjgMisF0yET3NA=
github.com/charmbracelet/bubbletea v0.24.3-0.20230724163731-91dd12007337/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/glamour v0.5.0 h1:wu15ykPdB7X6chxugG/NNfDUbyyrCLV9XBalj5wdu3g=
github.com/charmbracelet/glamour v0.5.0/go.mod h1:9ZRtG19AUIzcTm7FGLGbq3D5WKQ5UyZBbQsMQN0XIqc=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
Expand Down Expand Up @@ -37,6 +39,8 @@ github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/microcosm-cc/bluemonday v1.0.17/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=
github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg=
github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM=
Expand All @@ -49,6 +53,8 @@ github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKt
github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -73,19 +79,27 @@ golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
100 changes: 53 additions & 47 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/charmbracelet/lipgloss"
)

// TODO: fix GormRepository vs Repository
// TODO: rendering is broken; gets fixed when you resize...?!
type (
SyncProjects struct{}
editorFinishedMsg struct {
Expand Down Expand Up @@ -80,7 +80,6 @@ func (m Model) Init() tea.Cmd {
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
currentProject := m.list.SelectedItem().FilterValue()
switch msg := msg.(type) {
case tea.WindowSizeMsg:
WindowSize.Width = msg.Width
Expand All @@ -91,58 +90,65 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.err = msg
case SyncProjects:
items, _ := newList()
m.list.SetItems(items)
m.mode = nav
m.input.Blur()
return m, m.list.SetItems(items)
case tea.KeyMsg:
if m.input.Focused() {
switch m.mode {
case nav:
return m.handleNav(msg)
case edit:
if key.Matches(msg, Keymap.Enter) {
if m.mode == create {
cmds = append(cmds, createProjectCmd(m.input.Value()))
}
if m.mode == edit {
cmds = append(cmds, renameProjectCmd(
Project(currentProject),
m.input.Value()))
}
m.input.SetValue("")
m.mode = nav
m.input.Blur()
}
if key.Matches(msg, Keymap.Back) {
m.input.SetValue("")
m.mode = nav
m.input.Blur()
return m, renameProjectCmd(
Project(m.list.SelectedItem().FilterValue()),
m.input.Value())
}
// only log keypresses for the input field when it's focused
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
} else {
switch {
case key.Matches(msg, Keymap.Create):
m.mode = create
m.input.Focus()
cmd = textinput.Blink
case key.Matches(msg, Keymap.Quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, Keymap.Enter):
p := Project(currentProject)
e := InitEntry(p.Path())
return e, e.Init()
case key.Matches(msg, Keymap.Rename):
m.mode = edit
m.input.Focus()
cmd = textinput.Blink
case key.Matches(msg, Keymap.Delete):
cmd = deleteProjectCmd(Project(currentProject))
default:
m.list, cmd = m.list.Update(msg)
case create:
if key.Matches(msg, Keymap.Enter) {
return m, createProjectCmd(m.input.Value())
}
cmds = append(cmds, cmd)
}
// keys no matter the state
if key.Matches(msg, Keymap.Back) {
m.input.SetValue("")
m.input.Blur()
m.mode = nav
}
if key.Matches(msg, Keymap.Quit) {
m.quitting = true
return m, tea.Quit
}

// only log keypresses for the input field when it's focused
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}

func (m Model) handleNav(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
currentProject := m.list.SelectedItem().FilterValue()
switch {
case key.Matches(msg, Keymap.Create):
m.mode = create
m.input.Focus()
return m, textinput.Blink
case key.Matches(msg, Keymap.Enter):
p := Project(currentProject)
e := InitEntry(p.Path())
return e, e.Init()
case key.Matches(msg, Keymap.Rename):
m.mode = edit
m.input.Focus()
return m, textinput.Blink
case key.Matches(msg, Keymap.Delete):
return m, deleteProjectCmd(Project(currentProject))
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}

// View return the text UI to be output to the terminal
func (m Model) View() string {
var err string
Expand All @@ -154,19 +160,19 @@ func (m Model) View() string {
} else {
err = m.err.Error()
}
if m.input.Focused() {
if m.mode == nav {
return DocStyle.Render(
lipgloss.JoinVertical(
lipgloss.Left,
m.list.View(),
m.input.View(),
err,
))
}
return DocStyle.Render(
lipgloss.JoinVertical(
lipgloss.Left,
m.list.View(),
m.input.View(),
err,
))
}
Expand Down