diff --git a/cmd/ttg/main.go b/cmd/ttg/main.go index 9c74e1a..6553a4d 100644 --- a/cmd/ttg/main.go +++ b/cmd/ttg/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "log" + "os" "github.com/ivanilves/ttg/pkg/directory" "github.com/ivanilves/ttg/pkg/file" @@ -10,6 +11,7 @@ import ( "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" @@ -38,7 +40,9 @@ func main() { flag.Parse() if version { - shell.PrintAndExit(appVersion) + println(appVersion) + + os.Exit(0) } matches := flag.Args() @@ -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()) diff --git a/go.mod b/go.mod index 1ea62d8..485146b 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index d6d1956..6001c28 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/menu/menu.go b/pkg/menu/menu.go index 1ec7465..ca2bc13 100644 --- a/pkg/menu/menu.go +++ b/pkg/menu/menu.go @@ -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") } @@ -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, } diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index 61899e1..ec7bcbd 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -9,7 +9,6 @@ import ( ) const defaultShell = "bash" -const defaultLines = 20 func detectShell() string { shell := os.Getenv("SHELL") @@ -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] @@ -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) - } -} diff --git a/pkg/shell/shell_test.go b/pkg/shell/shell_test.go index 3e53559..9f8828d 100644 --- a/pkg/shell/shell_test.go +++ b/pkg/shell/shell_test.go @@ -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) diff --git a/pkg/terminal/terminal.go b/pkg/terminal/terminal.go new file mode 100644 index 0000000..92285d6 --- /dev/null +++ b/pkg/terminal/terminal.go @@ -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 +}