Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom style/output #8

Merged
merged 19 commits into from
Oct 20, 2017
23 changes: 23 additions & 0 deletions _examples/confirm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/manifoldco/promptui"
)

func main() {
prompt := promptui.Prompt{
Label: "Delete Resource",
IsConfirm: true,
}

result, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

fmt.Printf("You choose %q\n", result)
}
43 changes: 43 additions & 0 deletions _examples/custom_prompt/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"strconv"

"github.com/manifoldco/promptui"
)

type pepper struct {
Name string
HeatUnit int
Peppers int
}

func main() {
validate := func(input string) error {
_, err := strconv.ParseFloat(input, 64)
return err
}

templates := &promptui.PromptTemplates{
Prompt: "{{ . }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}

prompt := promptui.Prompt{
Label: "Spicy Level",
Templates: templates,
Validate: validate,
}

result, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

fmt.Printf("You answered %s\n", result)
}
50 changes: 50 additions & 0 deletions _examples/custom_select/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"

"github.com/manifoldco/promptui"
)

type pepper struct {
Name string
HeatUnit int
Peppers int
}

func main() {
peppers := []pepper{
{Name: "Bell Pepper", HeatUnit: 0, Peppers: 0},
{Name: "Banana Pepper", HeatUnit: 100, Peppers: 1},
{Name: "Poblano", HeatUnit: 1000, Peppers: 2},
{Name: "Jalapeño", HeatUnit: 3500, Peppers: 3},
{Name: "Aleppo", HeatUnit: 10000, Peppers: 4},
{Name: "Tabasco", HeatUnit: 30000, Peppers: 5},
{Name: "Malagueta", HeatUnit: 50000, Peppers: 6},
{Name: "Habanero", HeatUnit: 100000, Peppers: 7},
{Name: "Red Savina Habanero", HeatUnit: 350000, Peppers: 8},
{Name: "Dragon’s Breath", HeatUnit: 855000, Peppers: 9},
}

templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U0001F525 {{ .Name | bold }} ({{ .HeatUnit | red | italic }})",
Inactive: " {{ .Name | bold }} ({{ .HeatUnit | red | italic }})",
Selected: "\U0001F525 {{ .Name | red | bold }}",
}

prompt := promptui.Select{
Label: "Spicy Level",
Items: peppers,
Templates: templates,
}

i, _, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

fmt.Printf("You choose number %d: %v\n", i+1, peppers[i])
}
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/select/main.go → _examples/select/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func main() {
return
}

fmt.Printf("You choose %q\n", result)
fmt.Printf("You choose %s\n", result)
}
33 changes: 26 additions & 7 deletions codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"fmt"
"strconv"
"strings"
"text/template"
)

const esc = "\033["

type attribute int

// Forground weight/decoration attributes.
// Foreground weight/decoration attributes.
const (
reset attribute = iota

Expand All @@ -20,7 +21,7 @@ const (
FGUnderline
)

// Forground color attributes
// Foreground color attributes
const (
FGBlack attribute = iota + 30
FGRed
Expand All @@ -35,12 +36,29 @@ const (
// ResetCode is the character code used to reset the terminal formatting
var ResetCode = fmt.Sprintf("%s%dm", esc, reset)

var (
const (
hideCursor = esc + "?25l"
showCursor = esc + "?25h"
clearLine = esc + "2K"
)

// FuncMap defines template helpers for the output. It can be extended as a
// regular map.
var FuncMap = template.FuncMap{
"black": Styler(FGBlack),
"red": Styler(FGRed),
"green": Styler(FGGreen),
"yellow": Styler(FGYellow),
"blue": Styler(FGBlue),
"magenta": Styler(FGMagenta),
"cyan": Styler(FGCyan),
"white": Styler(FGWhite),
"bold": Styler(FGBold),
"faint": Styler(FGFaint),
"italic": Styler(FGItalic),
"underline": Styler(FGUnderline),
}

func upLine(n uint) string {
return movementCode(n, 'A')
}
Expand All @@ -55,19 +73,20 @@ func movementCode(n uint, code rune) string {

// Styler returns a func that applies the attributes given in the Styler call
// to the provided string.
func Styler(attrs ...attribute) func(string) string {
func Styler(attrs ...attribute) func(interface{}) string {
attrstrs := make([]string, len(attrs))
for i, v := range attrs {
attrstrs[i] = strconv.Itoa(int(v))
}

seq := strings.Join(attrstrs, ";")

return func(s string) string {
return func(v interface{}) string {
end := ""
if !strings.HasSuffix(s, ResetCode) {
s, ok := v.(string)
if !ok || !strings.HasSuffix(s, ResetCode) {
end = ResetCode
}
return fmt.Sprintf("%s%sm%s%s", esc, seq, s, end)
return fmt.Sprintf("%s%sm%v%s", esc, seq, v, end)
}
}
2 changes: 0 additions & 2 deletions codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestStyler(t *testing.T) {
if boldRed != expected {
t.Errorf("style did not match: %s != %s", boldRed, expected)
}

})

t.Run("should not repeat reset codes for nested styles", func(t *testing.T) {
Expand All @@ -27,6 +26,5 @@ func TestStyler(t *testing.T) {
if boldRed != expected {
t.Errorf("style did not match: %s != %s", boldRed, expected)
}

})
}
Loading