Skip to content

Commit

Permalink
Removed header option. Added README
Browse files Browse the repository at this point in the history
  • Loading branch information
Nexidian committed Feb 2, 2022
1 parent 22987df commit 0a49e48
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
# go-cli-select
Lightweight menu selection library for CLI choices
# Golang CLI Select
Lightweight interactive CLI selection library


## Import the package
```go
import "github.com/nexidian/gocliselect"
```

## Usage
Create a new menu, supplying the question as a parameter

```go
menu := gocliselect.NewMenu("Chose a colour")
```

Add any number of options by calling `AddItem()` supplying the display text of the option
as well as the id
```go
menu.AddItem("Red", "red")
menu.AddItem("Blue", "blue")
menu.AddItem("Green", "green")
menu.AddItem("Yellow", "yellow")
menu.AddItem("Cyan", "cyan")
```

To display the menu and away the user choice call `Display()`

```go
choice := menu.Display()
```

## Example
```go
package main

import (
"fmt"
"github.com/nexidian/gocliselect"
)

func main() {
menu := gocliselect.NewMenu("Chose a colour")

menu.AddItem("Red", "red")
menu.AddItem("Blue", "blue")
menu.AddItem("Green", "green")
menu.AddItem("Yellow", "yellow")
menu.AddItem("Cyan", "cyan")

choice := menu.Display()

fmt.Printf("Choice: %s\n", choice)
}
```
21 changes: 13 additions & 8 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package main

import "github.com/nexidian/gocliselect"
import (
"fmt"
"github.com/nexidian/gocliselect"
)

func main() {
menu := gocliselect.NewMenu("", "Select an option")
menu := gocliselect.NewMenu("Chose a colour")

menu.AddItem("Option 1", "option1")
menu.AddItem("Option 2", "option2")
menu.AddItem("Option 3", "option3")
menu.AddItem("Option 4", "option4")
menu.AddItem("Option 5", "option5")
menu.AddItem("Red", "red")
menu.AddItem("Blue", "blue")
menu.AddItem("Green", "green")
menu.AddItem("Yellow", "yellow")
menu.AddItem("Cyan", "cyan")

menu.Display()
choice := menu.Display()

fmt.Printf("Choice: %s\n", choice)
}
14 changes: 3 additions & 11 deletions gocliselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var keys = map[byte]bool {
}

type Menu struct {
Heading string
Prompt string
CursorPos int
MenuItems []*MenuItem
Expand All @@ -30,9 +29,8 @@ type MenuItem struct {
SubMenu *Menu
}

func NewMenu(heading string, prompt string) *Menu {
func NewMenu(prompt string) *Menu {
return &Menu{
Heading: heading,
Prompt: prompt,
MenuItems: make([]*MenuItem, 0),
}
Expand Down Expand Up @@ -84,15 +82,10 @@ func (m *Menu) renderMenuItems(redraw bool) {
// It returns the users selected choice
func (m *Menu) Display() string {
defer func() {
// Show cursor.
// Show cursor again.
fmt.Printf("\033[?25h")
}()

if m.Heading != "" {
fmt.Println(m.Heading)
fmt.Println("another message")
}

fmt.Printf("%s\n", goterm.Color(goterm.Bold(m.Prompt) + ":", goterm.CYAN))

m.renderMenuItems(false)
Expand All @@ -102,13 +95,12 @@ func (m *Menu) Display() string {

for {
keyCode := getInput()
//keyCode := escape
if keyCode == escape {
return ""
} else if keyCode == enter {
menuItem := m.MenuItems[m.CursorPos]
fmt.Println("\r")
return string(menuItem.ID)
return menuItem.ID
} else if keyCode == up {
m.CursorPos = (m.CursorPos + len(m.MenuItems) - 1) % len(m.MenuItems)
m.renderMenuItems(true)
Expand Down

0 comments on commit 0a49e48

Please sign in to comment.