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

Commit

Permalink
Added customisable hide character for password inputs. (#431)
Browse files Browse the repository at this point in the history
Includes example, but is missing a test

mend

Co-authored-by: Alec Aivazis <alec@aivazis.com>
  • Loading branch information
SirRegion and AlecAivazis committed Sep 22, 2022
1 parent 734e799 commit d889203
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
36 changes: 36 additions & 0 deletions examples/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
)

// the questions to ask
var defaultPasswordCharacterPrompt = &survey.Password{
Message: "What is your password? (Default hide character)",
}
var customPasswordCharacterPrompt = &survey.Password{
Message: "What is your password? (Custom hide character)",
}

func main() {

var defaultPass string
var customPass string

// ask the question
err := survey.AskOne(defaultPasswordCharacterPrompt, &defaultPass)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println()
err = survey.AskOne(customPasswordCharacterPrompt, &customPass, survey.WithHideCharacter('-'))
if err != nil {
fmt.Println(err.Error())
return
}
// print the answers
fmt.Printf("Password 1: %s.\n Password 2: %s\n", defaultPass, customPass)
}
6 changes: 3 additions & 3 deletions password.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *Password) Prompt(config *PromptConfig) (interface{}, error) {

// no help msg? Just return any response
if p.Help == "" {
line, err := rr.ReadLine('*')
line, err := rr.ReadLine(config.HideCharacter)
return string(line), err
}

Expand All @@ -69,7 +69,7 @@ func (p *Password) Prompt(config *PromptConfig) (interface{}, error) {
var line []rune
// process answers looking for help prompt answer
for {
line, err = rr.ReadLine('*')
line, err = rr.ReadLine(config.HideCharacter)
if err != nil {
return string(line), err
}
Expand All @@ -96,7 +96,7 @@ func (p *Password) Prompt(config *PromptConfig) (interface{}, error) {
}

lineStr := string(line)
p.AppendRenderedText(strings.Repeat("*", len(lineStr)))
p.AppendRenderedText(strings.Repeat(string(config.HideCharacter), len(lineStr)))
return lineStr, err
}

Expand Down
13 changes: 13 additions & 0 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func defaultAskOptions() *AskOptions {
ShowCursor: false,
RemoveSelectAll: false,
RemoveSelectNone: false,
HideCharacter: '*',
},
}
}
Expand Down Expand Up @@ -122,6 +123,7 @@ type PromptConfig struct {
ShowCursor bool
RemoveSelectAll bool
RemoveSelectNone bool
HideCharacter rune
}

// Prompt is the primary interface for the objects that can take user input
Expand Down Expand Up @@ -254,6 +256,17 @@ func WithShowCursor(ShowCursor bool) AskOpt {
}
}

// WithHideCharacter sets the default character shown instead of the password for password inputs
func WithHideCharacter(char rune) AskOpt {
return func(options *AskOptions) error {
// set the hide character
options.PromptConfig.HideCharacter = char

// nothing went wrong
return nil
}
}

/*
AskOne performs the prompt for a single prompt and asks for validation if required.
Response types should be something that can be casted from the response type designated
Expand Down

0 comments on commit d889203

Please sign in to comment.