-
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: Add formality and basic glossary support
- Loading branch information
Showing
7 changed files
with
408 additions
and
44 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,51 @@ | ||
package ui | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
type Dialog struct { | ||
tview.Flex | ||
|
||
buttons *tview.Flex | ||
cancel func() | ||
} | ||
|
||
func NewDialog(p tview.Primitive) *Dialog { | ||
dialog := &Dialog{ | ||
Flex: *tview.NewFlex(), | ||
} | ||
dialog.Flex.SetDirection(tview.FlexRow) | ||
|
||
dialog.buttons = tview.NewFlex() | ||
|
||
dialog.Flex. | ||
AddItem(p, 0, 1, true). | ||
AddItem(dialog.buttons, 1, 0, false) | ||
|
||
dialog.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if event.Key() == tcell.KeyEsc && dialog.cancel != nil { | ||
dialog.cancel() | ||
return nil | ||
} | ||
return event | ||
}) | ||
|
||
return dialog | ||
} | ||
|
||
func (d *Dialog) AddButton(label string, selected func()) *Dialog { | ||
button := tview.NewButton(label). | ||
SetSelectedFunc(selected) | ||
if d.buttons.GetItemCount() > 0 { | ||
d.buttons.AddItem(tview.NewBox(), 1, 0, false) | ||
} | ||
d.buttons.AddItem(button, 0, 1, false) | ||
return d | ||
} | ||
|
||
func (d *Dialog) SetCancelFunc(cancel func()) *Dialog { | ||
d.cancel = cancel | ||
return d | ||
} |
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 |
---|---|---|
@@ -1,19 1,125 @@ | ||
package ui | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
|
||
"github.com/cluttrdev/deepl-go/deepl" | ||
) | ||
|
||
type GlossariesDialog struct { | ||
tview.Modal | ||
type GlossariesWidget struct { | ||
tview.Flex | ||
|
||
dropDown *tview.DropDown | ||
selected func(string, int) | ||
|
||
table *tview.Table | ||
data func(string, int) (*deepl.GlossaryInfo, []deepl.GlossaryEntry) | ||
} | ||
|
||
type GlossaryTableContent struct { | ||
tview.TableContentReadOnly | ||
|
||
Info *deepl.GlossaryInfo | ||
Entries []deepl.GlossaryEntry | ||
} | ||
|
||
func (td *GlossaryTableContent) GetCell(row, column int) *tview.TableCell { | ||
if row < 0 || row > len(td.Entries) || column < 0 || column >= 2 { | ||
return nil | ||
} | ||
|
||
cell := tview.NewTableCell("") | ||
|
||
if row == 0 { | ||
text := td.Info.SourceLang | ||
if column == 1 { | ||
text = td.Info.TargetLang | ||
} | ||
cell. | ||
SetText(strings.ToUpper(text)). | ||
SetStyle(tcell.StyleDefault.Foreground(tview.Styles.SecondaryTextColor)) | ||
} else if column == 0 { | ||
cell.SetText(td.Entries[row-1].Source) | ||
} else if column == 1 { | ||
cell.SetText(td.Entries[row-1].Target) | ||
} | ||
|
||
glossariesDropDown *tview.DropDown | ||
return cell.SetExpansion(1) | ||
} | ||
|
||
func newGlossariesPage(ui *UI) *GlossariesDialog { | ||
page := &GlossariesDialog{} | ||
func (td *GlossaryTableContent) GetRowCount() int { | ||
if td == nil { | ||
return 0 | ||
} | ||
return len(td.Entries) 1 | ||
} | ||
|
||
func (td *GlossaryTableContent) GetColumnCount() int { | ||
return 2 | ||
} | ||
|
||
func newGlossariesWidget(ui *UI) *GlossariesWidget { | ||
w := &GlossariesWidget{ | ||
Flex: *tview.NewFlex(), | ||
} | ||
// layout | ||
w.Flex.SetDirection(tview.FlexRow) | ||
|
||
// dropdown | ||
w.dropDown = tview.NewDropDown(). | ||
SetLabel("Select: ") | ||
w.Flex.AddItem(w.dropDown, 1, 0, true) | ||
|
||
// table | ||
w.table = tview.NewTable(). | ||
SetFixed(1, 2). | ||
SetBorders(true) | ||
w.Flex.AddItem(w.table, 0, 1, true) | ||
|
||
return w | ||
} | ||
|
||
func (w *GlossariesWidget) GetCurrentOption() (int, string) { | ||
return w.dropDown.GetCurrentOption() | ||
} | ||
|
||
func (w *GlossariesWidget) SetOptions(options []string) *GlossariesWidget { | ||
opts := make([]string, 1, len(options) 1) | ||
opts[0] = " " | ||
opts = append(opts, options...) | ||
w.dropDown.SetOptions(opts, w.selectedFunc) | ||
return w | ||
} | ||
|
||
func (w *GlossariesWidget) SetDataFunc(data func(string, int) (*deepl.GlossaryInfo, []deepl.GlossaryEntry)) *GlossariesWidget { | ||
w.data = data | ||
return w | ||
} | ||
|
||
func (w *GlossariesWidget) SetSelectedFunc(selected func(string, int)) *GlossariesWidget { | ||
w.selected = selected | ||
return w | ||
} | ||
|
||
page.glossariesDropDown = tview.NewDropDown() | ||
func (w *GlossariesWidget) selectedFunc(text string, index int) { | ||
var content *GlossaryTableContent = nil | ||
if index > 0 { | ||
info, entries := w.data(text, index) | ||
if info != nil { | ||
content = &GlossaryTableContent{ | ||
Info: info, | ||
Entries: entries, | ||
} | ||
} | ||
} | ||
w.table. | ||
SetContent(content). | ||
ScrollToBeginning() | ||
|
||
return page | ||
if w.selected != nil { | ||
w.selected(text, index) | ||
} | ||
} |
Oops, something went wrong.