Skip to content

Commit

Permalink
Add "State" to Question
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jan 23, 2025
1 parent 3294549 commit bca2183
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/action_init_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func updatePrepare(key, input string, q *wizard.Question) (string, error) {
if cfg, ok := q.Screen.Wizard.Backing.(*types.Config); ok {
service := cfg.Services[key]
bytes, _ := json.Marshal(service)
// q.State = colors.Yellow + string(bytes) + colors.Off
q.State = colors.Yellow + string(bytes) + colors.Off
if service.Enabled {
return "yes", validOk(`question proceeds`, input)
}
Expand Down
6 changes: 5 additions & 1 deletion app/wizard/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Question struct {
Question string
Hint string
Value string
State string
Response string
ErrorStr string
PrepareFn func(string, *Question) (string, error)
Expand Down Expand Up @@ -76,7 +77,7 @@ func (q *Question) Prompt(str, spacer string, pad ...bool) string {
}

var reps = Replacement{Color: colors.Green, Values: []string{
"Question:", "Current:", "Answer:", "Error:", "Hint", "Response",
"Question:", "Current:", "Answer:", "Error:", "Hint", "Response", "State",
}}
return reps.Replace(str)
}
Expand All @@ -88,6 +89,9 @@ func (q *Question) getLines() []string {
if q.Hint != "" {
lines = append(lines, q.Prompt("Hint", "")+q.Hint)
}
if q.State != "" {
lines = append(lines, q.Prompt("State", "")+q.State)
}
if q.Value != "" {
lines = append(lines, q.Prompt("Current", "")+colors.BrightBlue+q.Value+colors.Off)
}
Expand Down
6 changes: 6 additions & 0 deletions app/wizard/question_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestQuestion(t *testing.T) {

assert.Equal(t, "What is your name?", question.Question)
assert.Equal(t, "", question.Value)
assert.Equal(t, "", question.State)
assert.Equal(t, "", question.ErrorStr) // Not initialized until processResponse
assert.NotNil(t, question.PrepareFn)
assert.NotNil(t, question.Validate)
Expand All @@ -44,19 +45,22 @@ func TestQuestion(t *testing.T) {
err := question.processResponse("download")
assert.NoError(t, err)
assert.Equal(t, "download", question.Value)
assert.Equal(t, "", question.State)
assert.Equal(t, "", question.ErrorStr)

// Invalid input
err = question.processResponse("invalid")
assert.Error(t, err)
assert.Equal(t, "invalid", question.Value)
assert.Equal(t, "", question.State)
assert.Contains(t, question.ErrorStr, "value must be either \"download\" or \"scratch\"")

// Empty input (uses current Value)
question.Value = "scratch"
err = question.processResponse("")
assert.NoError(t, err)
assert.Equal(t, "scratch", question.Value)
assert.Equal(t, "", question.State)
assert.Equal(t, "", question.ErrorStr)
}
t.Run("Process Response Test", func(t *testing.T) { processResponseTest() })
Expand All @@ -65,13 +69,15 @@ func TestQuestion(t *testing.T) {
emptyQuestion := &Question{
Question: "",
Value: "",
State: "",
ErrorStr: "",
PrepareFn: nil,
Validate: nil,
}

assert.Equal(t, "", emptyQuestion.Question)
assert.Equal(t, "", emptyQuestion.Value)
assert.Equal(t, "", emptyQuestion.State)
assert.Equal(t, "", emptyQuestion.ErrorStr)
assert.Nil(t, emptyQuestion.PrepareFn)
assert.Nil(t, emptyQuestion.Validate)
Expand Down
2 changes: 2 additions & 0 deletions app/wizard/screen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestProcessResponse(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t, "valid", question.Value)
assert.Equal(t, "", question.State)
assert.Equal(t, "", question.ErrorStr)
}
t.Run("Valid Input Test", func(t *testing.T) { validInputTest() })
Expand All @@ -79,6 +80,7 @@ func TestProcessResponse(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "invalid input", question.ErrorStr)
assert.Equal(t, "", question.Value)
assert.Equal(t, "", question.State)
}
t.Run("Invalid Input Test", func(t *testing.T) { invalidInputTest() })

Expand Down

0 comments on commit bca2183

Please sign in to comment.