Skip to content

Commit

Permalink
Dynamic resize based on terminal size
Browse files Browse the repository at this point in the history
  • Loading branch information
bcongdon committed Nov 28, 2020
1 parent e61f2b1 commit 46c3511
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ require (
github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591
github.com/rivo/tview v0.0.0-20201118063654-f007e9ad3893
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392 h1:xYJJ3S178yv++9zXV/hnr29plCAGO9vAFG9dorqaFQc=
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 h1:XtNJkfEjb4zR3q20BBBcYUykVOEMgZeIUOpBPfNYgxg=
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
Expand Down
33 changes: 25 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
ordering "github.com/bcongdon/emoji-ordering"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"golang.org/x/crypto/ssh/terminal"
)

const numCols = 10

var (
outputFlag = flag.String("output", "clipboard", "The output of ep. Choices: clipboard, stdout")
noninteractiveFlag = flag.Bool("noninteractive", false, "If set, doesn't display emoji picker -- instead just outputting the first selection for the provided query.")
Expand Down Expand Up @@ -69,7 +68,7 @@ func filterEmojis(emojis map[string][]Emoji, query string) []string {
return justEmojis
}

func drawEmojis(table *tview.Table, emojis map[string][]Emoji, query string) {
func drawEmojis(table *tview.Table, emojis map[string][]Emoji, query string, numCols int) {
filteredEmojis := filterEmojis(emojis, query)
used := make(map[string]bool)
gridIdx := 0
Expand Down Expand Up @@ -124,8 +123,8 @@ func main() {
app := tview.NewApplication()
table := tview.NewTable().
SetBorders(false).
SetSelectable(true, true)
// SetFixed(0, 0)
SetSelectable(true, true).
SetFixed(0, 0)

initialQuery := strings.Join(flag.Args(), " ")
inputField := tview.NewInputField().
Expand All @@ -142,23 +141,29 @@ func main() {
return event
})

width, height, err := terminal.GetSize(0)
if err != nil {
log.Fatalf("Unable to get terminal size: %v", err)
}
height = clamp(height, 20, 40)
numCols := (width - 2) / 3

grid := tview.NewGrid().
SetRows(1, 1).
SetColumns(1, 1).
AddItem(inputField, 0, 0, 1, 3, 0, 0, true).
AddItem(table, 2, 0, 1, 3, 0, 0, false)
grid.SetBorder(true).SetTitle("Emoji Picker").SetRect(0, 0, 60, 25)
grid.SetBorder(true).SetTitle("Emoji Picker").SetRect(0, 0, width, height)

emojis, _ := getEmojis()
if *noninteractiveFlag {
runNoninterativeMode(emojis, initialQuery)
return
}
drawEmojis(table, emojis, initialQuery)

inputField.SetChangedFunc(func(text string) {
table.Clear()
drawEmojis(table, emojis, text)
drawEmojis(table, emojis, text, numCols)
})

table.SetDoneFunc(func(key tcell.Key) {
Expand All @@ -185,7 +190,19 @@ func main() {
return event
})

drawEmojis(table, emojis, initialQuery, numCols)
if err := app.SetRoot(grid, false).Run(); err != nil {
panic(err)
}
}

func clamp(val, low, high int) int {
switch {
case val < low:
return low
case val > high:
return high
default:
return val
}
}

0 comments on commit 46c3511

Please sign in to comment.