This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
cairde/input.go

163 lines
3.3 KiB
Go

package main
import (
"strings"
tea "github.com/charmbracelet/bubbletea"
)
const cmdHelp = `/acn on | Turn on the Tor ACN
/acn off | Turn off the Tor ACN
/clear | Clear the screen
/help | Show this help
/profile create <name> <password> <password> | Create a new profile
/profile info | Show profile information
/profile unlock <password> | Unlock profile(s)
/start | Show getting started guide
/quit | Quit the program`
func isHelpCmd(cmds []string) bool {
if cmds[0] == "help" {
return true
}
return false
}
func isQuitCmd(cmds []string) bool {
if cmds[0] == "quit" {
return true
}
return false
}
func isStartCmd(cmds []string) bool {
if cmds[0] == "start" {
return true
}
return false
}
func isProfileCreateCmd(cmds []string) bool {
if cmds[0] == "profile" && cmds[1] == "create" {
return true
}
return false
}
func isProfileUnlockCmd(cmds []string) bool {
if cmds[0] == "profile" && cmds[1] == "unlock" {
return true
}
return false
}
func isProfileInfoCmd(cmds []string) bool {
if cmds[0] == "profile" && cmds[1] == "info" {
return true
}
return false
}
func isAcnOnCmd(cmds []string) bool {
if cmds[0] == "acn" && cmds[1] == "on" {
return true
}
return false
}
func isAcnOffCmd(cmds []string) bool {
if cmds[0] == "acn" && cmds[1] == "off" {
return true
}
return false
}
func isClearCmd(cmds []string) bool {
if cmds[0] == "clear" {
return true
}
return false
}
func handleCommand(cmd, hiddenInput string) tea.Msg {
cmds := strings.Split(cmd, " ")
switch {
case isHelpCmd(cmds):
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
case isQuitCmd(cmds):
return turnAcnOffMsg{quitProgram: true}
case isStartCmd(cmds):
return showGettingStartedGuideMsg{}
case isProfileCreateCmd(cmds):
if len(cmds) != 5 {
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
}
passwords := strings.Split(strings.TrimSpace(hiddenInput), " ")
if passwords[0] != passwords[1] {
return cmdMsg{output: []string{"profile create: passwords do not match?"}}
}
return createProfileMsg{
name: cmds[2],
password: passwords[1],
}
case isProfileUnlockCmd(cmds):
if len(cmds) != 3 {
return cmdMsg{output: strings.Split(cmdHelp, "\n")}
}
return profileUnlockMsg{
password: strings.TrimSpace(hiddenInput),
}
case isProfileInfoCmd(cmds):
return profileInfoMsg{}
case isAcnOnCmd(cmds):
return turnAcnOnMsg{}
case isAcnOffCmd(cmds):
return turnAcnOffMsg{}
case isClearCmd(cmds):
return clearScreenMsg{}
default:
return cmdMsg{output: []string{"unknown command"}}
}
}
func hidePasswordInput(m *model) {
val := strings.TrimSpace(m.input.Value())
if len(val) == 0 {
return
}
if string(val[0]) != "/" {
return
}
cmds := strings.Split(val[1:], " ")
if !(cmds[0] == "profile" && len(cmds) >= 2) {
return
}
if cmds[1] == "create" && len(cmds) > 3 ||
cmds[1] == "unlock" && len(cmds) > 2 {
lastChar := string(val[len(val)-1])
if lastChar != " " {
m.hiddenInput += lastChar
newCmd := []rune(val)
newCmd[len(val)-1] = '*'
m.input.SetValue(string(newCmd))
}
}
}