Skip to content

Commit

Permalink
feat: Split up main ui implementation
Browse files Browse the repository at this point in the history
- Add TranslatePage widget
- Implement footer as command input field
- Enable cycling through pages
  • Loading branch information
cluttrdev committed Dec 9, 2023
1 parent 65aa65b commit 5c84b31
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 81 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 12,7 @@ require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 8,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74 3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK i7ruzyGqttikkLy0=
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-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/rivo/tview v0.0.0-20231115183240-7c9e464bac02 h1:UkSrnoeeuKdeNFe4ghSjZmp7tA5B1CQKnvV1By9FSYw=
github.com/rivo/tview v0.0.0-20231115183240-7c9e464bac02/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down
81 changes: 81 additions & 0 deletions internal/ui/footer.go
Original file line number Diff line number Diff line change
@@ -0,0 1,81 @@
package ui

import (
"errors"

"github.com/gdamore/tcell/v2"
"github.com/mattn/go-shellwords"
"github.com/rivo/tview"
)

func (ui *UI) setupFooter() {
cmdline := tview.NewInputField()

cmdline.
SetFieldStyle(
tcell.StyleDefault.
Background(tview.Styles.PrimitiveBackgroundColor).
Foreground(tview.Styles.PrimaryTextColor),
).
SetLabelStyle(
tcell.StyleDefault,
).
SetBorder(true)

cmdline.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyBacktab:
// ignore backtab, else it would finish editing
return nil
case tcell.KeyTab:
// remap key, else it would finish editing
cmdline.Autocomplete()
return nil
}

return event
})

cmdline.SetDoneFunc(func(key tcell.Key) {
var err error
defer func() {
cmdline.
SetLabel("").
SetDisabled(true)

if err != nil {
cmdline.SetText(err.Error())
}

ui.SetFocus(ui.pages)
}()

text := cmdline.GetText()
cmdline.SetText("")

if key != tcell.KeyEnter {
return
}

args, err := shellwords.Parse(text)
if err != nil {
return
}

if len(args) < 1 {
return
} else if len(args) > 1 {
err = errors.New("invalid command")
return
}

switch args[0] {
case "translate", "glossaries":
ui.switchToPage(args[0])
default:
err = errors.New("invalid command")
}
})

ui.footer = cmdline
}
19 changes: 19 additions & 0 deletions internal/ui/glossaries.go
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
package ui

import (
"github.com/rivo/tview"
)

type GlossariesDialog struct {
tview.Modal

glossariesDropDown *tview.DropDown
}

func newGlossariesPage(ui *UI) *GlossariesDialog {
page := &GlossariesDialog{}

page.glossariesDropDown = tview.NewDropDown()

return page
}
87 changes: 87 additions & 0 deletions internal/ui/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 1,87 @@
package ui

import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type TranslatePage struct {
tview.Grid

sourceLangDropDown *tview.DropDown
targetLangDropDown *tview.DropDown

inputTextArea *tview.TextArea
outputTextView *tview.TextView
}

func newTranslatePage(ui *UI) *TranslatePage {
page := &TranslatePage{
Grid: *tview.NewGrid(),
}

page.sourceLangDropDown = tview.NewDropDown()
page.targetLangDropDown = tview.NewDropDown()

page.inputTextArea = tview.NewTextArea().
SetPlaceholder("Type to translate.")

page.outputTextView = tview.NewTextView()
page.outputTextView.SetChangedFunc(func() {
ui.Draw()
})

page.Grid.
SetRows(1, 0).
SetColumns(0, 0).
SetBorders(true).
AddItem(page.sourceLangDropDown, 0, 0, 1, 1, 0, 0, false).
AddItem(page.targetLangDropDown, 0, 1, 1, 1, 0, 0, false).
AddItem(page.inputTextArea, 1, 0, 1, 1, 0, 0, true).
AddItem(page.outputTextView, 1, 1, 1, 1, 0, 0, false)
page.Grid.SetBorderPadding(0, 0, 0, 0)

page.registerKeyBindings(ui)

return page
}

func (w *TranslatePage) registerKeyBindings(ui *UI) {
w.Grid.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Modifiers() == tcell.ModAlt {
switch event.Rune() {
case 's':
ui.SetFocus(w.sourceLangDropDown)
return nil
case 't':
ui.SetFocus(w.targetLangDropDown)
return nil
case 'i':
ui.SetFocus(w.inputTextArea)
return nil
}
}
return event
})

}

func (w *TranslatePage) adjustToSize() {
_, _, width, _ := w.GetInnerRect()

var (
sourceLangLabel string
targetLangLabel string
)

if width > 96 {
sourceLangLabel = "Select source language: "
targetLangLabel = "Select target language: "
} else {
sourceLangLabel = ""
targetLangLabel = ""
}

w.sourceLangDropDown.SetLabel(sourceLangLabel)
w.targetLangDropDown.SetLabel(targetLangLabel)
}
Loading

0 comments on commit 5c84b31

Please sign in to comment.