forked from tildetown/town
add color and email prompt
parent
0884ba2ff6
commit
880511a79a
|
@ -5,19 +5,59 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/mattn/go-tty"
|
"github.com/mattn/go-tty"
|
||||||
)
|
)
|
||||||
|
|
||||||
func numberPrompt(tty *tty.TTY, prompt string, opts []string) (int, error) {
|
// TODO consider local-only help command for renaming and adding emails to account
|
||||||
|
|
||||||
|
type colorScheme struct {
|
||||||
|
Header func(string) string
|
||||||
|
Subtitle func(string) string
|
||||||
|
Prompt func(string) string
|
||||||
|
Email func(string) string
|
||||||
|
Option func(string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newColorScheme() colorScheme {
|
||||||
|
s2r := func(s lipgloss.Style) func(string) string {
|
||||||
|
return s.Render
|
||||||
|
}
|
||||||
|
c := func(s string) lipgloss.Color {
|
||||||
|
return lipgloss.Color(s)
|
||||||
|
}
|
||||||
|
s := lipgloss.NewStyle
|
||||||
|
return colorScheme{
|
||||||
|
Header: s2r(s().Bold(true).Foreground(c("#E0B0FF"))),
|
||||||
|
Subtitle: s2r(s().Italic(true).Foreground(c("gray"))),
|
||||||
|
Email: s2r(s().Bold(true).Underline(true)),
|
||||||
|
Prompt: s2r(s().Bold(true).Foreground(c("#00752d"))),
|
||||||
|
Option: s2r(s().Bold(true).Foreground(c("#38747a"))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringPrompt(cs colorScheme, tty *tty.TTY, prompt string) (string, error) {
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println(cs.Prompt(prompt))
|
||||||
|
fmt.Println(cs.Subtitle("(type your answer below and press enter to submit)"))
|
||||||
|
s, err := tty.ReadString()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("couldn't collect input: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func numberPrompt(cs colorScheme, tty *tty.TTY, prompt string, opts []string) (int, error) {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println(prompt)
|
fmt.Println(cs.Prompt(prompt))
|
||||||
fmt.Println("(pick an option using the corresponding number)")
|
fmt.Println(cs.Subtitle("(pick an option using the corresponding number)"))
|
||||||
|
|
||||||
chosen := -1
|
chosen := -1
|
||||||
for chosen < 0 {
|
for chosen < 0 {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
for ix, o := range opts {
|
for ix, o := range opts {
|
||||||
fmt.Printf("%d: %s\n", ix+1, o)
|
fmt.Printf("%s: %s\n", cs.Option(fmt.Sprintf("%d", ix+1)), o)
|
||||||
}
|
}
|
||||||
r, err := tty.ReadRune()
|
r, err := tty.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -37,13 +77,17 @@ func numberPrompt(tty *tty.TTY, prompt string, opts []string) (int, error) {
|
||||||
chosen = c - 1
|
chosen = c - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
return chosen, nil
|
return chosen, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main() error {
|
func _main() error {
|
||||||
fmt.Println("Hi, you have reached the tilde town help desk.")
|
cs := newColorScheme()
|
||||||
|
fmt.Println(cs.Header("Hi, you have reached the tilde town help desk."))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Please check out the options below. If none of them apply to you, you can send an e-mail to root@tilde.town .")
|
fmt.Println("Please check out the options below.")
|
||||||
|
fmt.Printf("If none of them apply to your case, you can email %s. \n", cs.Email("root@tilde.town"))
|
||||||
tty, err := tty.Open()
|
tty, err := tty.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not open tty: %w", err)
|
return fmt.Errorf("could not open tty: %w", err)
|
||||||
|
@ -51,20 +95,48 @@ func _main() error {
|
||||||
defer tty.Close()
|
defer tty.Close()
|
||||||
|
|
||||||
options := []string{
|
options := []string{
|
||||||
"I lost access to my tilde.town account and want to upload a new SSH key",
|
"I need to request that a new SSH key be added to my account.",
|
||||||
"I have a code from my e-mail to redeem",
|
"I have a code from my e-mail to redeem for a new SSH key",
|
||||||
"I just want outta here",
|
"I just want out of here",
|
||||||
}
|
}
|
||||||
c, err := numberPrompt(tty, "What do you need help with?", options)
|
c, err := numberPrompt(cs, tty, "What do you need help with?", options)
|
||||||
|
|
||||||
if c == 2 {
|
switch c {
|
||||||
|
case 0:
|
||||||
|
return collectEmail(cs, tty)
|
||||||
|
case 1:
|
||||||
|
return redeemCode(tty)
|
||||||
|
case 2:
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("bye~")
|
fmt.Println(cs.Header("bye~"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO collect email
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func collectEmail(cs colorScheme, tty *tty.TTY) error {
|
||||||
|
fmt.Println(cs.Header("We can send a reset code to an email associated with your town account."))
|
||||||
|
email, err := stringPrompt(cs, tty, "email to send reset code to?")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(email)
|
||||||
|
// TODO confirm email
|
||||||
|
// TODO generate reset code
|
||||||
|
// TODO send email
|
||||||
|
// TODO report success
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func redeemCode(tty *tty.TTY) error {
|
||||||
|
// TODO deserialize
|
||||||
|
// TODO verify code
|
||||||
|
// TODO accept key
|
||||||
|
// TODO verify key
|
||||||
|
// TODO add key to .authorized_keys2
|
||||||
|
// TODO mark used
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue