Skip to content

Commit

Permalink
Small cleanings
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jan 21, 2025
1 parent b510b6a commit 4aebce6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 19 deletions.
4 changes: 1 addition & 3 deletions app/action_init_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ You may use $HOME or ~/ in your paths to refer to your home directory.`
}
}

var generalScreen = wizard.Screen{
return wizard.Screen{
Title: generalTitle,
Subtitle: generalSubtitle,
Instructions: generalInstructions,
Expand All @@ -53,8 +53,6 @@ You may use $HOME or ~/ in your paths to refer to your home directory.`
Questions: generalQuestions,
Style: wizard.NewStyle(),
}

return generalScreen
}

// --------------------------------------------------------
Expand Down
20 changes: 15 additions & 5 deletions app/wizard/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func displayScreen(w *Wizard, screenIndex int) error {
} else if errors.Is(err, ErrSkipQuestion) {
i++
} else if errors.Is(err, ErrValidateWarn) {
msg := question.Prompt("Response") + err.Error()
fmt.Println(colors.Yellow + msg + colors.Off)
msg := question.Prompt("Response") + colors.BrightBlue + err.Error() + colors.Off
fmt.Println(msg)
if os.Getenv("NO_CLEAR") != "true" {
time.Sleep(3000 * time.Millisecond)
}
} else if errors.Is(err, ErrValidateMsg) {
msg := question.Prompt("Response") + err.Error()
fmt.Println(colors.Yellow + msg + colors.Off)
msg := question.Prompt("Response") + colors.BrightBlue + err.Error() + colors.Off
fmt.Println(msg)
if os.Getenv("NO_CLEAR") != "true" {
time.Sleep(1250 * time.Millisecond)
}
Expand All @@ -66,14 +66,24 @@ func displayScreen(w *Wizard, screenIndex int) error {
} else if !errors.Is(err, ErrUserBack) || i == 0 {
return err
} else {
i -= 2
prev := &curScreen.Questions[i-1]
skip := prev.Prepare(curScreen)
if skip {
curScreen.Questions[i-1].ErrorMsg = ""
curScreen.Questions[i-2].ErrorMsg = ""
i -= 3
} else {
curScreen.Questions[i-1].ErrorMsg = ""
i -= 2
}
}
}
}
} else {
nSkipped++
}
}

w.Next()
return nil
}
2 changes: 1 addition & 1 deletion app/wizard/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (q *Question) Prompt(str string, pad ...bool) string {
str = spacer + fmt.Sprintf("%-*s", 10, str+":")
}

var reps = Replacement{Color: colors.Green, Values: []string{"Question:", "Current:", "Answer:", "Error:", "Hint"}}
var reps = Replacement{Color: colors.Green, Values: []string{"Question:", "Current:", "Answer:", "Error:", "Hint", "Response"}}
return reps.Replace(str)
}

Expand Down
55 changes: 46 additions & 9 deletions app/wizard/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,32 @@ func (s *Screen) OpenHelp() {

func (s *Screen) Display() {
topBorder := func(width int, bs Border) []string {
left := string(boxTokens[bs][TopLeft])
middle := strings.Repeat(string(boxTokens[bs][Horizontal]), width-2)
right := string(boxTokens[bs][TopRight])
return []string{
string(boxTokens[bs][TopLeft]) + strings.Repeat(string(boxTokens[bs][Horizontal]), width-2) + string(boxTokens[bs][TopRight]),
left + middle + right,
}
}

bottomBorder := func(width int, bs Border) []string {
left := string(boxTokens[bs][BottomLeft])
middle := strings.Repeat(string(boxTokens[bs][Horizontal]), width-2)
right := string(boxTokens[bs][BottomRight])
return []string{
string(boxTokens[bs][BottomLeft]) + strings.Repeat(string(boxTokens[bs][Horizontal]), width-2) + string(boxTokens[bs][BottomRight]),
left + middle + right,
}
}

// innerBorder := func(width int, bs Border) []string {
// left := string(boxTokens[bs][BottomLeft])
// middle := strings.Repeat(string(boxTokens[bs][Horizontal]), width-2)
// right := string(boxTokens[bs][RightT])
// return []string{
// left + middle + right,
// }
// }

padRow := func(line string, width int, bs Border, just Justification) string {
_ = just // linter
bw := 2
Expand All @@ -92,13 +107,30 @@ func (s *Screen) Display() {
log.Printf("line too long in padRow: %s%s%s [%d,%d,%d,%d]\n", colors.Red, line, colors.Off, width, lineLen, padLeft, padRight)
padRight = 0
}

// switch just {
// case Right:
// padLeft += padRight
// padRight = 0
// case Center:
// extraLeft := padRight / 2
// extraRight := padRight - extraLeft
// padLeft += extraLeft
// padRight = extraRight
// case Left:
// fallthrough
// default:
// // do nothing
// }

return strings.Repeat(" ", padLeft) + line + strings.Repeat(" ", padRight)
}

boxRow := func(s string, width int, bs Border, just Justification) string {
boxRow := func(str string, width int, bs Border, just Justification) string {
body := []string{}
lines := strings.Split(s, "\n")
lines := strings.Split(str, "\n")
for _, line := range lines {
// padded := padRow(line, width, s.Style.Inner, just)
padded := padRow(line, width, bs, just)
l := string(boxTokens[bs][Vertical]) + padded + string(boxTokens[bs][Vertical])
body = append(body, l)
Expand All @@ -119,6 +151,7 @@ func (s *Screen) Display() {
for _, s := range strs {
ret = append(ret, boxRow(s, width, bs, just))
}
// ret = append(ret, innerBorder(width, bs)...)
}

return strings.Join(ret, "\n")
Expand Down Expand Up @@ -149,17 +182,13 @@ func (s *Screen) Display() {
}
return []string{}
}

lines := []string{}
lines = append(lines, titleRows(s.Title, s.Subtitle, &s.Style)...)
lines = append(lines, s.Body)
lines = append(lines, bodyPad(strings.Join(lines, "\n"), 13)...)
lines = append(lines, s.Instructions)
screen := box(lines, screenWidth, s.Style.Outer, Left)
clearScreen := "\033[2J\033[H"
if os.Getenv("NO_CLEAR") == "true" {
clearScreen = ""
}
fmt.Printf("%s%s\n", clearScreen, screen)
}

Expand All @@ -170,3 +199,11 @@ func (s *Screen) GetCaret(caret string, i, skipped int) string {
return caret

}

var clearScreen = "\033[2J\033[H"

func init() {
if os.Getenv("NO_CLEAR") == "true" {
clearScreen = ""
}
}
15 changes: 15 additions & 0 deletions app/wizard/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const (
BottomRight
Horizontal
Vertical
TopT
LeftT
BottomT
RightT
MiddleT
)

var boxTokens = map[Border]map[BorderPos]rune{
Expand All @@ -49,6 +54,11 @@ var boxTokens = map[Border]map[BorderPos]rune{
BottomRight: '┘',
Horizontal: '─',
Vertical: '│',
TopT: '┬',
LeftT: '├',
BottomT: '┴',
RightT: '┤',
MiddleT: '┼',
},
Double: {
TopLeft: '╔',
Expand All @@ -57,5 +67,10 @@ var boxTokens = map[Border]map[BorderPos]rune{
BottomRight: '╝',
Horizontal: '═',
Vertical: '║',
TopT: '╦',
LeftT: '╠',
BottomT: '╩',
RightT: '╣',
MiddleT: '╬',
},
}
2 changes: 1 addition & 1 deletion app/wizard/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (w *Wizard) Run() error {
}
}

fmt.Println("")
fmt.Printf("%s\n", clearScreen)
fmt.Println("Your answers:")
width := 0
for _, screen := range w.screens {
Expand Down

0 comments on commit 4aebce6

Please sign in to comment.