Skip to content

Commit

Permalink
add left and right arrow support for username and password fields
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-arrow committed May 30, 2024
1 parent 13714cf commit ea5335b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
33 changes: 23 additions & 10 deletions tui/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type field interface {
type input struct {
prompt string
contents string
location int
masked bool
}

Expand All @@ -25,20 +26,32 @@ func (self *input) draw(boxSize int) (string, int) {
} else {
contents = self.contents
}
return self.prompt + ": " + contents, len(self.prompt) + len(contents) + 3
return self.prompt + ": " + contents, len(self.prompt) + self.location + 3
}

func (self *input) onInput(tui *Tui, symbol []int) {
if len(symbol) == 1 {
// Backspace
if symbol[0] == 127 {
if len(self.contents) > 0 {
self.contents = self.contents[:len(self.contents)-1]
}
// Other characters
} else {
self.contents = self.contents + string(rune(symbol[0]))
// Backspace
if symbol[0] == 127 {
if self.location > 0 {
self.contents = self.contents[:self.location-1] + self.contents[self.location:]
self.location -= 1
}
// Right Arrow
} else if reflect.DeepEqual(symbol, []int{27, 91, 67}) {
self.location += 1
if self.location >= len(self.contents) {
self.location = len(self.contents)
}
// Left Arrow
} else if reflect.DeepEqual(symbol, []int{27, 91, 68}) {
self.location -= 1
if self.location < 0 {
self.location = 0
}
} else if len(symbol) == 1 {
// Other characters
self.location += 1
self.contents = self.contents[:self.location-1] + string(rune(symbol[0])) + self.contents[self.location-1:]
}
}

Expand Down
10 changes: 3 additions & 7 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func New(config config.Config, resetTo term.State) (Tui, error) {
return Tui{}, err
}

if err != nil {
return Tui{}, err
}

self := Tui{
TermSize: TermSize{
Lines: lines,
Expand Down Expand Up @@ -110,6 +106,7 @@ func (self *Tui) getFields() []field {
}
if self.config.LastSession != nil {
userInput.contents = self.config.LastSession.User
userInput.location = len(self.config.LastSession.User)
self.position = 2
}

Expand Down Expand Up @@ -194,12 +191,11 @@ func (self *Tui) login() {
term.Restore(int(os.Stdin.Fd()), &self.termState)

set_message := func(message string) {
_, _ = term.MakeRaw(int(os.Stdin.Fd()))
oldState, _ := term.MakeRaw(int(os.Stdin.Fd()))
defer term.Restore(int(os.Stdin.Fd()), oldState)

self.message = message
self.draw()

term.Restore(int(os.Stdin.Fd()), &self.termState)
}

err := login.Authenticate(username, password, session, set_message)
Expand Down

0 comments on commit ea5335b

Please sign in to comment.