-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Split up main ui implementation
- Add TranslatePage widget - Implement footer as command input field - Enable cycling through pages
- Loading branch information
Showing
6 changed files
with
243 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.