Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Change icons to more simple ones (Issue 93) (#150)
Browse files Browse the repository at this point in the history
* Replace icons with simpler versions to increase compatibility

* Add method to enable the previos fancy icons

* Fix tests and replace hardcoded icons by dynamic string format
  • Loading branch information
MarkusFreitag authored and AlecAivazis committed Aug 16, 2018
1 parent 1769a6d commit f30c5d1
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 77 deletions.
19 changes: 12 additions & 7 deletions confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package survey

import (
"bytes"
"fmt"
"io"
"os"
"testing"
Expand Down Expand Up @@ -29,32 +30,31 @@ func TestConfirmRender(t *testing.T) {
"Test Confirm question output with default true",
Confirm{Message: "Is pizza your favorite food?", Default: true},
ConfirmTemplateData{},
`? Is pizza your favorite food? (Y/n) `,
fmt.Sprintf("%s Is pizza your favorite food? (Y/n) ", core.QuestionIcon),
},
{
"Test Confirm question output with default false",
Confirm{Message: "Is pizza your favorite food?", Default: false},
ConfirmTemplateData{},
`? Is pizza your favorite food? (y/N) `,
fmt.Sprintf("%s Is pizza your favorite food? (y/N) ", core.QuestionIcon),
},
{
"Test Confirm answer output",
Confirm{Message: "Is pizza your favorite food?"},
ConfirmTemplateData{Answer: "Yes"},
"? Is pizza your favorite food? Yes\n",
fmt.Sprintf("%s Is pizza your favorite food? Yes\n", core.QuestionIcon),
},
{
"Test Confirm with help but help message is hidden",
Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
ConfirmTemplateData{},
"? Is pizza your favorite food? [? for help] (y/N) ",
fmt.Sprintf("%s Is pizza your favorite food? [%s for help] (y/N) ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Confirm help output with help message shown",
Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
ConfirmTemplateData{ShowHelp: true},
`ⓘ This is helpful
? Is pizza your favorite food? (y/N) `,
fmt.Sprintf("%s This is helpful\n%s Is pizza your favorite food? (y/N) ", core.HelpIcon, core.QuestionIcon),
},
}

Expand Down Expand Up @@ -125,7 +125,12 @@ func TestConfirmPrompt(t *testing.T) {
Help: "It probably is",
},
func(c *expect.Console) {
c.ExpectString("Is pizza your favorite food? [? for help] (y/N)")
c.ExpectString(
fmt.Sprintf(
"Is pizza your favorite food? [%s for help] (y/N)",
string(core.HelpInputRune),
),
)
c.SendLine("?")
c.ExpectString("It probably is")
c.SendLine("Y")
Expand Down
35 changes: 31 additions & 4 deletions core/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,44 @@ import (
var DisableColor = false

var (
// HelpInputRune is the rune which the user should enter to trigger
// more detailed question help
HelpInputRune = '?'

ErrorIcon = "✘"
HelpIcon = "ⓘ"
// ErrorIcon will be be shown before an error
ErrorIcon = "X"

// HelpIcon will be shown before more detailed question help
HelpIcon = "????"
// QuestionIcon will be shown before a question Message
QuestionIcon = "?"

MarkedOptionIcon = "◉"
// MarkedOptionIcon will be prepended before a selected multiselect option
MarkedOptionIcon = "[x]"
// UnmarkedOptionIcon will be prepended before an unselected multiselect option
UnmarkedOptionIcon = "[ ]"

// SelectFocusIcon is prepended to an option to signify the user is
// currently focusing that option
SelectFocusIcon = ">"
)

/*
SetFancyIcons changes the err, help, marked, and focus input icons to their
fancier forms. These forms may not be compatible with most terminals.
This function will not touch the QuestionIcon as its fancy and non fancy form
are the same.
*/
func SetFancyIcons() {
ErrorIcon = "✘"
HelpIcon = "ⓘ"
// QuestionIcon fancy and non-fancy form are the same

MarkedOptionIcon = "◉"
UnmarkedOptionIcon = "◯"

SelectFocusIcon = "❯"
)
}

var TemplateFuncs = map[string]interface{}{
// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
Expand Down
26 changes: 15 additions & 11 deletions editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package survey

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -30,51 +31,49 @@ func TestEditorRender(t *testing.T) {
"Test Editor question output without default",
Editor{Message: "What is your favorite month:"},
EditorTemplateData{},
"? What is your favorite month: [Enter to launch editor] ",
fmt.Sprintf("%s What is your favorite month: [Enter to launch editor] ", core.QuestionIcon),
},
{
"Test Editor question output with default",
Editor{Message: "What is your favorite month:", Default: "April"},
EditorTemplateData{},
"? What is your favorite month: (April) [Enter to launch editor] ",
fmt.Sprintf("%s What is your favorite month: (April) [Enter to launch editor] ", core.QuestionIcon),
},
{
"Test Editor question output with HideDefault",
Editor{Message: "What is your favorite month:", Default: "April", HideDefault: true},
EditorTemplateData{},
"? What is your favorite month: [Enter to launch editor] ",
fmt.Sprintf("%s What is your favorite month: [Enter to launch editor] ", core.QuestionIcon),
},
{
"Test Editor answer output",
Editor{Message: "What is your favorite month:"},
EditorTemplateData{Answer: "October", ShowAnswer: true},
"? What is your favorite month: October\n",
fmt.Sprintf("%s What is your favorite month: October\n", core.QuestionIcon),
},
{
"Test Editor question output without default but with help hidden",
Editor{Message: "What is your favorite month:", Help: "This is helpful"},
EditorTemplateData{},
"? What is your favorite month: [? for help] [Enter to launch editor] ",
fmt.Sprintf("%s What is your favorite month: [%s for help] [Enter to launch editor] ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Editor question output with default and with help hidden",
Editor{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
EditorTemplateData{},
"? What is your favorite month: [? for help] (April) [Enter to launch editor] ",
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) [Enter to launch editor] ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Editor question output without default but with help shown",
Editor{Message: "What is your favorite month:", Help: "This is helpful"},
EditorTemplateData{ShowHelp: true},
`ⓘ This is helpful
? What is your favorite month: [Enter to launch editor] `,
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: [Enter to launch editor] ", core.HelpIcon, core.QuestionIcon),
},
{
"Test Editor question output with default and with help shown",
Editor{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
EditorTemplateData{ShowHelp: true},
`ⓘ This is helpful
? What is your favorite month: (April) [Enter to launch editor] `,
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) [Enter to launch editor] ", core.HelpIcon, core.QuestionIcon),
},
}

Expand Down Expand Up @@ -178,7 +177,12 @@ func TestEditorPrompt(t *testing.T) {
Help: "Describe your git commit",
},
func(c *expect.Console) {
c.ExpectString("Edit git commit message [? for help] [Enter to launch editor]")
c.ExpectString(
fmt.Sprintf(
"Edit git commit message [%s for help] [Enter to launch editor]",
string(core.HelpInputRune),
),
)
c.SendLine("?")
c.ExpectString("Describe your git commit")
c.SendLine("")
Expand Down
17 changes: 8 additions & 9 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package survey

import (
"bytes"
"fmt"
"io"
"os"
"testing"
Expand Down Expand Up @@ -29,45 +30,43 @@ func TestInputRender(t *testing.T) {
"Test Input question output without default",
Input{Message: "What is your favorite month:"},
InputTemplateData{},
"? What is your favorite month: ",
fmt.Sprintf("%s What is your favorite month: ", core.QuestionIcon),
},
{
"Test Input question output with default",
Input{Message: "What is your favorite month:", Default: "April"},
InputTemplateData{},
"? What is your favorite month: (April) ",
fmt.Sprintf("%s What is your favorite month: (April) ", core.QuestionIcon),
},
{
"Test Input answer output",
Input{Message: "What is your favorite month:"},
InputTemplateData{Answer: "October", ShowAnswer: true},
"? What is your favorite month: October\n",
fmt.Sprintf("%s What is your favorite month: October\n", core.QuestionIcon),
},
{
"Test Input question output without default but with help hidden",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{},
"? What is your favorite month: [? for help] ",
fmt.Sprintf("%s What is your favorite month: [%s for help] ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Input question output with default and with help hidden",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{},
"? What is your favorite month: [? for help] (April) ",
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Input question output without default but with help shown",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
`ⓘ This is helpful
? What is your favorite month: `,
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: ", core.HelpIcon, core.QuestionIcon),
},
{
"Test Input question output with default and with help shown",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
`ⓘ This is helpful
? What is your favorite month: (April) `,
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) ", core.HelpIcon, core.QuestionIcon),
},
}

Expand Down
54 changes: 34 additions & 20 deletions multiselect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package survey

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"

expect "github.com/Netflix/go-expect"
Expand Down Expand Up @@ -42,12 +44,16 @@ func TestMultiSelectRender(t *testing.T) {
PageEntries: prompt.Options,
Checked: map[string]bool{"bar": true, "buz": true},
},
`? Pick your words: [Use arrows to move, type to filter]
◯ foo
◉ bar
❯ ◯ baz
◉ buz
`,
strings.Join(
[]string{
fmt.Sprintf("%s Pick your words: [Use arrows to move, type to filter]", core.QuestionIcon),
fmt.Sprintf(" %s foo", core.UnmarkedOptionIcon),
fmt.Sprintf(" %s bar", core.MarkedOptionIcon),
fmt.Sprintf("%s %s baz", core.SelectFocusIcon, core.UnmarkedOptionIcon),
fmt.Sprintf(" %s buz\n", core.MarkedOptionIcon),
},
"\n",
),
},
{
"Test MultiSelect answer output",
Expand All @@ -56,7 +62,7 @@ func TestMultiSelectRender(t *testing.T) {
Answer: "foo, buz",
ShowAnswer: true,
},
"? Pick your words: foo, buz\n",
fmt.Sprintf("%s Pick your words: foo, buz\n", core.QuestionIcon),
},
{
"Test MultiSelect question output with help hidden",
Expand All @@ -66,12 +72,16 @@ func TestMultiSelectRender(t *testing.T) {
PageEntries: prompt.Options,
Checked: map[string]bool{"bar": true, "buz": true},
},
`? Pick your words: [Use arrows to move, type to filter, ? for more help]
◯ foo
◉ bar
❯ ◯ baz
◉ buz
`,
strings.Join(
[]string{
fmt.Sprintf("%s Pick your words: [Use arrows to move, type to filter, %s for more help]", core.QuestionIcon, string(core.HelpInputRune)),
fmt.Sprintf(" %s foo", core.UnmarkedOptionIcon),
fmt.Sprintf(" %s bar", core.MarkedOptionIcon),
fmt.Sprintf("%s %s baz", core.SelectFocusIcon, core.UnmarkedOptionIcon),
fmt.Sprintf(" %s buz\n", core.MarkedOptionIcon),
},
"\n",
),
},
{
"Test MultiSelect question output with help shown",
Expand All @@ -82,13 +92,17 @@ func TestMultiSelectRender(t *testing.T) {
Checked: map[string]bool{"bar": true, "buz": true},
ShowHelp: true,
},
`ⓘ This is helpful
? Pick your words: [Use arrows to move, type to filter]
◯ foo
◉ bar
❯ ◯ baz
◉ buz
`,
strings.Join(
[]string{
fmt.Sprintf("%s This is helpful", core.HelpIcon),
fmt.Sprintf("%s Pick your words: [Use arrows to move, type to filter]", core.QuestionIcon),
fmt.Sprintf(" %s foo", core.UnmarkedOptionIcon),
fmt.Sprintf(" %s bar", core.MarkedOptionIcon),
fmt.Sprintf("%s %s baz", core.SelectFocusIcon, core.UnmarkedOptionIcon),
fmt.Sprintf(" %s buz\n", core.MarkedOptionIcon),
},
"\n",
),
},
}

Expand Down
8 changes: 4 additions & 4 deletions password_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package survey

import (
"fmt"
"testing"

expect "github.com/Netflix/go-expect"
Expand All @@ -25,20 +26,19 @@ func TestPasswordRender(t *testing.T) {
"Test Password question output",
Password{Message: "Tell me your secret:"},
PasswordTemplateData{},
"? Tell me your secret: ",
fmt.Sprintf("%s Tell me your secret: ", core.QuestionIcon),
},
{
"Test Password question output with help hidden",
Password{Message: "Tell me your secret:", Help: "This is helpful"},
PasswordTemplateData{},
"? Tell me your secret: [? for help] ",
fmt.Sprintf("%s Tell me your secret: [%s for help] ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Password question output with help shown",
Password{Message: "Tell me your secret:", Help: "This is helpful"},
PasswordTemplateData{ShowHelp: true},
`ⓘ This is helpful
? Tell me your secret: `,
fmt.Sprintf("%s This is helpful\n%s Tell me your secret: ", core.HelpIcon, core.QuestionIcon),
},
}

Expand Down

0 comments on commit f30c5d1

Please sign in to comment.