@@ -23,10 +23,9 @@ import (
2323 "github.com/peterh/liner"
2424)
2525
26- // TerminalPrompter holds the stdin line reader (also using stdout for printing
27- // prompts). Only this reader may be used for input because it keeps an internal
28- // buffer.
29- var TerminalPrompter = newTerminalPrompter ()
26+ // Stdin holds the stdin line reader (also using stdout for printing prompts).
27+ // Only this reader may be used for input because it keeps an internal buffer.
28+ var Stdin = newTerminalPrompter ()
3029
3130// UserPrompter defines the methods needed by the console to promt the user for
3231// various types of inputs.
@@ -44,9 +43,13 @@ type UserPrompter interface {
4443 // choice to be made, returning that choice.
4544 PromptConfirm (prompt string ) (bool , error )
4645
47- // SetScrollHistory sets the the input scrollback history that the prompter will
48- // allow the user to scoll back to.
49- SetScrollHistory (history []string )
46+ // SetHistory sets the the input scrollback history that the prompter will allow
47+ // the user to scoll back to.
48+ SetHistory (history []string )
49+
50+ // AppendHistory appends an entry to the scrollback history. It should be called
51+ // if and only if the prompt to append was a valid command.
52+ AppendHistory (command string )
5053
5154 // SetWordCompleter sets the completion function that the prompter will call to
5255 // fetch completion candidates when the user presses tab.
@@ -74,34 +77,34 @@ type terminalPrompter struct {
7477// newTerminalPrompter creates a liner based user input prompter working off the
7578// standard input and output streams.
7679func newTerminalPrompter () * terminalPrompter {
77- r := new (terminalPrompter )
80+ p := new (terminalPrompter )
7881 // Get the original mode before calling NewLiner.
7982 // This is usually regular "cooked" mode where characters echo.
8083 normalMode , _ := liner .TerminalMode ()
8184 // Turn on liner. It switches to raw mode.
82- r .State = liner .NewLiner ()
85+ p .State = liner .NewLiner ()
8386 rawMode , err := liner .TerminalMode ()
8487 if err != nil || ! liner .TerminalSupported () {
85- r .supported = false
88+ p .supported = false
8689 } else {
87- r .supported = true
88- r .normalMode = normalMode
89- r .rawMode = rawMode
90+ p .supported = true
91+ p .normalMode = normalMode
92+ p .rawMode = rawMode
9093 // Switch back to normal mode while we're not prompting.
9194 normalMode .ApplyMode ()
9295 }
93- r .SetCtrlCAborts (true )
94- r .SetTabCompletionStyle (liner .TabPrints )
96+ p .SetCtrlCAborts (true )
97+ p .SetTabCompletionStyle (liner .TabPrints )
9598
96- return r
99+ return p
97100}
98101
99102// PromptInput displays the given prompt to the user and requests some textual
100103// data to be entered, returning the input of the user.
101- func (r * terminalPrompter ) PromptInput (prompt string ) (string , error ) {
102- if r .supported {
103- r .rawMode .ApplyMode ()
104- defer r .normalMode .ApplyMode ()
104+ func (p * terminalPrompter ) PromptInput (prompt string ) (string , error ) {
105+ if p .supported {
106+ p .rawMode .ApplyMode ()
107+ defer p .normalMode .ApplyMode ()
105108 } else {
106109 // liner tries to be smart about printing the prompt
107110 // and doesn't print anything if input is redirected.
@@ -110,47 +113,53 @@ func (r *terminalPrompter) PromptInput(prompt string) (string, error) {
110113 prompt = ""
111114 defer fmt .Println ()
112115 }
113- return r .State .Prompt (prompt )
116+ return p .State .Prompt (prompt )
114117}
115118
116119// PromptPassword displays the given prompt to the user and requests some textual
117120// data to be entered, but one which must not be echoed out into the terminal.
118121// The method returns the input provided by the user.
119- func (r * terminalPrompter ) PromptPassword (prompt string ) (passwd string , err error ) {
120- if r .supported {
121- r .rawMode .ApplyMode ()
122- defer r .normalMode .ApplyMode ()
123- return r .State .PasswordPrompt (prompt )
122+ func (p * terminalPrompter ) PromptPassword (prompt string ) (passwd string , err error ) {
123+ if p .supported {
124+ p .rawMode .ApplyMode ()
125+ defer p .normalMode .ApplyMode ()
126+ return p .State .PasswordPrompt (prompt )
124127 }
125- if ! r .warned {
128+ if ! p .warned {
126129 fmt .Println ("!! Unsupported terminal, password will be echoed." )
127- r .warned = true
130+ p .warned = true
128131 }
129132 // Just as in Prompt, handle printing the prompt here instead of relying on liner.
130133 fmt .Print (prompt )
131- passwd , err = r .State .Prompt ("" )
134+ passwd , err = p .State .Prompt ("" )
132135 fmt .Println ()
133136 return passwd , err
134137}
135138
136139// PromptConfirm displays the given prompt to the user and requests a boolean
137140// choice to be made, returning that choice.
138- func (r * terminalPrompter ) PromptConfirm (prompt string ) (bool , error ) {
139- input , err := r .Prompt (prompt + " [y/N] " )
141+ func (p * terminalPrompter ) PromptConfirm (prompt string ) (bool , error ) {
142+ input , err := p .Prompt (prompt + " [y/N] " )
140143 if len (input ) > 0 && strings .ToUpper (input [:1 ]) == "Y" {
141144 return true , nil
142145 }
143146 return false , err
144147}
145148
146- // SetScrollHistory sets the the input scrollback history that the prompter will
147- // allow the user to scoll back to.
148- func (r * terminalPrompter ) SetScrollHistory (history []string ) {
149- r .State .ReadHistory (strings .NewReader (strings .Join (history , "\n " )))
149+ // SetHistory sets the the input scrollback history that the prompter will allow
150+ // the user to scoll back to.
151+ func (p * terminalPrompter ) SetHistory (history []string ) {
152+ p .State .ReadHistory (strings .NewReader (strings .Join (history , "\n " )))
153+ }
154+
155+ // AppendHistory appends an entry to the scrollback history. It should be called
156+ // if and only if the prompt to append was a valid command.
157+ func (p * terminalPrompter ) AppendHistory (command string ) {
158+ p .State .AppendHistory (command )
150159}
151160
152161// SetWordCompleter sets the completion function that the prompter will call to
153162// fetch completion candidates when the user presses tab.
154- func (r * terminalPrompter ) SetWordCompleter (completer WordCompleter ) {
155- r .State .SetWordCompleter (liner .WordCompleter (completer ))
163+ func (p * terminalPrompter ) SetWordCompleter (completer WordCompleter ) {
164+ p .State .SetWordCompleter (liner .WordCompleter (completer ))
156165}
0 commit comments