mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
refactor(prompts): fold Asker into Prompter and share delete confirmations
FormPrompter is the only production prompter and implements the rich prompt API, so Ask, AskText and ConfirmDefault become the Prompter interface and the plain Input/Confirm fallbacks go away. Confirmations that default to no derive their non-terminal wording from the prompt, and single deletes share deleteConfirmed for the prompt, the request and the done line. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
31 files changed
+131
-253
No files matched your search
@@ -5,6 +5,7 @@ package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -153,17 +154,34 @@ type scriptedPrompter struct {
|
||||
asked []string
|
||||
secrets []string
|
||||
confirmations []string
|
||||
texts []string
|
||||
defaults []bool
|
||||
}
|
||||
|
||||
func (p *scriptedPrompter) Input(_ context.Context, field string) (string, error) {
|
||||
func (p *scriptedPrompter) Ask(_ context.Context, field string, validate func(string) error) (string, error) {
|
||||
p.asked = append(p.asked, field)
|
||||
if value, ok := p.inputs[field]; ok {
|
||||
value, ok := p.inputs[field]
|
||||
if !ok {
|
||||
if p.input == "" {
|
||||
return "", errors.New("unexpected input prompt " + field)
|
||||
}
|
||||
value = p.input
|
||||
}
|
||||
// A form re-prompts until the answer is valid; the script has no other answer, so it gives up.
|
||||
if validate != nil && validate(value) != nil {
|
||||
return "", terminal.ErrCanceled
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// AskText records the title and initial text, and answers like Ask, else with "typed body".
|
||||
func (p *scriptedPrompter) AskText(_ context.Context, title, initial string) (string, error) {
|
||||
p.asked = append(p.asked, title)
|
||||
p.texts = append(p.texts, title+" "+initial)
|
||||
if value, ok := p.inputs[title]; ok {
|
||||
return value, nil
|
||||
}
|
||||
if p.input == "" {
|
||||
return "", errors.New("unexpected input prompt " + field)
|
||||
}
|
||||
return p.input, nil
|
||||
return cmp.Or(p.input, "typed body"), nil
|
||||
}
|
||||
|
||||
func (p *scriptedPrompter) SecretInput(_ context.Context, title string) (string, error) {
|
||||
@@ -174,8 +192,9 @@ func (p *scriptedPrompter) SecretInput(_ context.Context, title string) (string,
|
||||
return p.secret, nil
|
||||
}
|
||||
|
||||
func (p *scriptedPrompter) Confirm(_ context.Context, description string) (bool, error) {
|
||||
func (p *scriptedPrompter) ConfirmDefault(_ context.Context, description string, defaultYes bool) (bool, error) {
|
||||
p.confirmations = append(p.confirmations, description)
|
||||
p.defaults = append(p.defaults, defaultYes)
|
||||
return p.accept, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user