Skip to content

Commit

Permalink
fix: menu size calculation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Oct 15, 2022
1 parent edda820 commit 3572e99
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 69 deletions.
8 changes: 6 additions & 2 deletions cmd/ttg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"flag"
"log"
"os"

"github.com/ivanilves/ttg/pkg/directory"
"github.com/ivanilves/ttg/pkg/file"
"github.com/ivanilves/ttg/pkg/filter"
"github.com/ivanilves/ttg/pkg/menu"
"github.com/ivanilves/ttg/pkg/scm"
"github.com/ivanilves/ttg/pkg/shell"
"github.com/ivanilves/ttg/pkg/terminal"
)

var appVersion = "default"
Expand Down Expand Up @@ -38,7 +40,9 @@ func main() {
flag.Parse()

if version {
shell.PrintAndExit(appVersion)
println(appVersion)

os.Exit(0)
}

matches := flag.Args()
Expand All @@ -59,7 +63,7 @@ func main() {
log.Fatalf("invalid filter: %s", err.Error())
}

selected, err := menu.Build(filter.Apply(entries, matches))
selected, err := menu.Build(filter.Apply(entries, matches), terminal.Height())

if err != nil {
log.Fatalf("failed to build menu: %s", err.Error())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-git/go-git/v5 v5.4.2
github.com/manifoldco/promptui v0.9.0
github.com/stretchr/testify v1.8.0
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9w
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
22 changes: 20 additions & 2 deletions pkg/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@ import (

const label = "Select Terragrunt project to travel"

// Overhead shows how many lines are occupied by menu control elements
const Overhead = 3

// MinSize stands for minimal menu size
const MinSize = 5

func getSize(itemCount int, size int) int {
if itemCount <= size {
return itemCount
}

if size <= MinSize {
return MinSize
}

return size
}

// Build creates an interactive menu to chose Terragrunt project from
func Build(items []string) (selected string, err error) {
func Build(items []string, maxSize int) (selected string, err error) {
if len(items) == 0 {
return "", fmt.Errorf("no items")
}
Expand All @@ -26,7 +44,7 @@ func Build(items []string) (selected string, err error) {
prompt := promptui.Select{
Label: label,
Items: items,
Size: len(items),
Size: getSize(len(items), maxSize-Overhead),
Searcher: searcher,
}

Expand Down
32 changes: 0 additions & 32 deletions pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

const defaultShell = "bash"
const defaultLines = 20

func detectShell() string {
shell := os.Getenv("SHELL")
Expand All @@ -29,28 +28,6 @@ func detectShell() string {
return defaultShell
}

func detectLines() int {
lines := os.Getenv("LINES")

if len(lines) == 0 {
return defaultLines
}

lnum, err := strconv.Atoi(lines)

if err != nil {
return defaultLines
}

return lnum
}

func isMocked() bool {
shell := detectShell()

return shell == "/bin/true" || shell == "/bin/false"
}

// Name returns a called binary name
func Name() string {
return os.Args[0]
Expand Down Expand Up @@ -83,12 +60,3 @@ func Spawn(path string) error {

return cmd.Run()
}

// PrintAndExit prints a string passed and exits after
func PrintAndExit(s string) {
fmt.Printf("%s\n", s)

if !isMocked() {
os.Exit(0)
}
}
33 changes: 0 additions & 33 deletions pkg/shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,6 @@ func TestDetectShell(t *testing.T) {
}
}

func TestDetectLines(t *testing.T) {
assert := assert.New(t)

cases := map[string]int{
"50": 50,
"xyz": defaultLines,
"": defaultLines,
}

for input, expected := range cases {
os.Setenv("LINES", input)

assert.Equal(expected, detectLines())
}
}

func TestIsMocked(t *testing.T) {
assert := assert.New(t)

cases := map[string]bool{
"/bin/bash": false,
"/bin/zsh": false,
"/bin/true": true,
"/bin/false": true,
}

for input, expected := range cases {
os.Setenv("SHELL", input)

assert.Equal(expected, isMocked())
}
}

func TestName(t *testing.T) {
assert := assert.New(t)

Expand Down
18 changes: 18 additions & 0 deletions pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package terminal

import (
"golang.org/x/term"
)

const defautHeight = 20

// Height reveals a number of lines we have in our terminal
func Height() int {
_, h, err := term.GetSize(0)

if err != nil {
return defautHeight
}

return h
}

0 comments on commit 3572e99

Please sign in to comment.