Skip to content

Commit

Permalink
feat: Make widgets adjust to screen size
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Nov 30, 2023
1 parent 449756d commit b3c86c8
Showing 1 changed file with 124 additions and 47 deletions.
171 changes: 124 additions & 47 deletions ui.go
Original file line number Diff line number Diff line change
@@ -1,16 1,66 @@
package main

import (
"fmt"
"strings"

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

const (
headerTextLarge = `
░░░░░░░░░░░░▒▓██▓▒░░░░░░░░░░░░
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░
░░░░░░▓█████▓▓█████████▓░░░░░░ _____ _ _______ _ _
░░░░░░█████▓░░▓█████████░░░░░░ | __ \ | | |__ __| | | | |
░░░░░░██████▓█▓▓▓▓▒▓████░░░░░░ | | | | ___ ___ _ __ | | | |_ __ __ _ _ __ ___| | __ _| |_ ___ _ __
░░░░░░██████████▓▒░▒████░░░░░░ | | | |/ _ \/ _ \ '_ \| | | | '__/ _\ | '_ \/ __| |/ _\ | __/ _ \| '__|
░░░░░░█████▓░▒▒▓████████░░░░░░ | |__| | __/ __/ |_) | |____ | | | | (_| | | | \__ \ | (_| | || (_) | |
░░░░░░▓████▓▒▒█████████▓░░░░░░ |_____/ \___|\___| .__/|______| |_|_| \__,_|_| |_|___/_|\__,_|\__\___/|_|
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░ | |
░░░░░░░░░░░░▒▓████▒░░░░░░░░░░░ |_|
░░░░░░░░░░░░░░░▒▓█░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
`
headerTextMedium = `
░░░░░░░░░░░░▒▓██▓▒░░░░░░░░░░░░
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░
░░░░░░▓█████▓▓█████████▓░░░░░░ _______ _ _
░░░░░░█████▓░░▓█████████░░░░░░ |__ __| | | | |
░░░░░░██████▓█▓▓▓▓▒▓████░░░░░░ | |_ __ __ _ _ __ ___| | __ _| |_ ___ _ __
░░░░░░██████████▓▒░▒████░░░░░░ | | '__/ _\ | '_ \/ __| |/ _\ | __/ _ \| '__|
░░░░░░█████▓░▒▒▓████████░░░░░░ | | | | (_| | | | \__ \ | (_| | || (_) | |
░░░░░░▓████▓▒▒█████████▓░░░░░░ |_|_| \__,_|_| |_|___/_|\__,_|\__\___/|_|
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░
░░░░░░░░░░░░▒▓████▒░░░░░░░░░░░
░░░░░░░░░░░░░░░▒▓█░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
`
headerTextSmall = `
░░░░░░░░░░░░▒▓██▓▒░░░░░░░░░░░░
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░
░░░░░░▓█████▓▓█████████▓░░░░░░
░░░░░░█████▓░░▓█████████░░░░░░
░░░░░░██████▓█▓▓▓▓▒▓████░░░░░░
░░░░░░██████████▓▒░▒████░░░░░░
░░░░░░█████▓░▒▒▓████████░░░░░░
░░░░░░▓████▓▒▒█████████▓░░░░░░
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░
░░░░░░░░░░░░▒▓████▒░░░░░░░░░░░
░░░░░░░░░░░░░░░▒▓█░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
`
)

var debug bool = false

type ui struct {
tview.Application

header tview.Primitive
grid *tview.Grid

header *tview.TextView
footer *tview.TextView

inputTextArea *tview.TextArea
Expand All @@ -25,13 75,11 @@ func newUI() *ui {
Application: *tview.NewApplication(),
}

ui.setupHeader()

ui.sourceLangDropDown = tview.NewDropDown().
SetLabel("Select source language: ")
ui.header = tview.NewTextView().
SetTextAlign(tview.AlignLeft)

ui.targetLangDropDown = tview.NewDropDown().
SetLabel("Select target language: ")
ui.sourceLangDropDown = tview.NewDropDown()
ui.targetLangDropDown = tview.NewDropDown()

ui.inputTextArea = tview.NewTextArea().
SetPlaceholder("Type to translate.")
Expand All @@ -41,7 89,8 @@ func newUI() *ui {
ui.Draw()
})

ui.footer = tview.NewTextView()
ui.footer = tview.NewTextView().
SetTextAlign(tview.AlignRight)

grid := tview.NewGrid()

Expand All @@ -54,38 103,84 @@ func newUI() *ui {
AddItem(ui.footer, 3, 0, 1, 2, 0, 0, false)

grid.
SetRows(12, 1, 0, 1).
SetColumns(0, 0).
SetBorders(true)

ui.SetRoot(grid, true)
ui.grid = grid

ui.SetRoot(ui.grid, true)

ui.registerKeybindings()

ui.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
w, h := screen.Size()
return ui.adjustToScreenSize(w, h)
})

return ui
}

func (ui *ui) setupHeader() {
var text string
func (ui *ui) registerKeybindings() {
ui.Application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Modifiers() == tcell.ModAlt {
switch event.Rune() {
case 's':
ui.SetFocus(ui.sourceLangDropDown)
return nil
case 't':
ui.SetFocus(ui.targetLangDropDown)
return nil
case 'i':
ui.SetFocus(ui.inputTextArea)
return nil
}
}
return event
})
}

text = `
░░░░░░░░░░░░▒▓██▓▒░░░░░░░░░░░░
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░
░░░░░░▓█████▓▓█████████▓░░░░░░ _____ _ _______ _ _
░░░░░░█████▓░░▓█████████░░░░░░ | __ \ | | |__ __| | | | |
░░░░░░██████▓█▓▓▓▓▒▓████░░░░░░ | | | | ___ ___ _ __ | | | |_ __ __ _ _ __ ___| | __ _| |_ ___ _ __
░░░░░░██████████▓▒░▒████░░░░░░ | | | |/ _ \/ _ \ '_ \| | | | '__/ _\ | '_ \/ __| |/ _\ | __/ _ \| '__|
░░░░░░█████▓░▒▒▓████████░░░░░░ | |__| | __/ __/ |_) | |____ | | | | (_| | | | \__ \ | (_| | || (_) | |
░░░░░░▓████▓▒▒█████████▓░░░░░░ |_____/ \___|\___| .__/|______| |_|_| \__,_|_| |_|___/_|\__,_|\__\___/|_|
░░░░░░░░▒▒▓████████▓▒▒░░░░░░░░ | |
░░░░░░░░░░░░▒▓████▒░░░░░░░░░░░ |_|
░░░░░░░░░░░░░░░▒▓█░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
`
func (ui *ui) adjustToScreenSize(width int, height int) bool {
var (
headerText string
headerHeight int

sourceLangLabel string
targetLangLabel string
)

if width > 112 && height > 30 {
headerText = headerTextLarge
headerHeight = 12
} else if width > 80 && height > 30 {
headerText = headerTextMedium
headerHeight = 12
} else if width > 31 && height > 30 {
headerText = headerTextSmall
headerHeight = 12
} else {
headerText = "DeepL Translator"
headerHeight = 1
}

ui.header = tview.NewTextView().
SetTextAlign(tview.AlignLeft).
SetText(strings.TrimSpace(text))
ui.grid.SetRows(headerHeight, 1, 0, 1)
ui.header.SetText(strings.TrimPrefix(headerText, "\n"))

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

ui.sourceLangDropDown.SetLabel(sourceLangLabel)
ui.targetLangDropDown.SetLabel(targetLangLabel)

if debug {
ui.footer.SetText(fmt.Sprintf("(%d,%d)", width, height))
}

return false
}

func (ui *ui) SetFooter(text string) {
Expand All @@ -111,21 206,3 @@ func (ui *ui) SetSourceLangOptions(opts []string, selected func(string, int)) {
func (ui *ui) SetTargetLangOptions(opts []string, selected func(string, int)) {
ui.targetLangDropDown.SetOptions(opts, selected)
}
func (ui *ui) registerKeybindings() {
ui.Application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Modifiers() == tcell.ModAlt {
switch event.Rune() {
case 's':
ui.SetFocus(ui.sourceLangDropDown)
return nil
case 't':
ui.SetFocus(ui.targetLangDropDown)
return nil
case 'i':
ui.SetFocus(ui.inputTextArea)
return nil
}
}
return event
})
}

0 comments on commit b3c86c8

Please sign in to comment.