Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
bug fix - compatibility issue with ubuntu's termianal and password ma…
Browse files Browse the repository at this point in the history
…sking (#137)

* fixed `#131`

* refactored & fixed `#131`

* accommodated changes for windows

* fixes cursor starting 1 line abovebottom of screen &
cleanup

* bug fix - comparability issue with ubuntu's termianal

* edge case handling

* fixed typo
  • Loading branch information
stefanvassilev authored and AlecAivazis committed Apr 10, 2018
1 parent 64f26f7 commit e752db4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
9 changes: 9 additions & 0 deletions terminal/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func CursorRestore() {
fmt.Print("\x1b8")
}

// for comparability purposes between windows
// in unix we need to print out a new line on some terminals
func CursorMoveNextLine(cur *Coord, terminalSize *Coord) {
if cur.Y == terminalSize.Y {
Println()
}
CursorNextLine(1)
}

// CursorLocation returns the current location of the cursor in the terminal
func CursorLocation() (*Coord, error) {
// print the escape sequence to receive the position in our stdin
Expand Down
6 changes: 6 additions & 0 deletions terminal/cursor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func CursorPreviousLine(n int) {
CursorHorizontalAbsolute(0)
}

// for comparability purposes between windows
// in windows we don't have to print out a new line
func CursorMoveNextLine(cur Coord, terminalSize *Coord) {
CursorNextLine(1)
}

func CursorHorizontalAbsolute(x int) {
handle := syscall.Handle(os.Stdout.Fd())

Expand Down
36 changes: 20 additions & 16 deletions terminal/runereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func NewRuneReader(input *os.File) *RuneReader {
}
}

func printChar(char rune, mask rune) {
// if we don't need to mask the input
if mask == 0 {
// just print the character the user pressed
Printf("%c", char)
} else {
// otherwise print the mask we were given
Printf("%c", mask)
}
}

func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
line := []rune{}
// we only care about horizontal displacements from the origin so start counting at 0
Expand Down Expand Up @@ -51,7 +62,8 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
index--
}
// move the cursor the a new line
CursorNextLine(1)
CursorMoveNextLine(cursorCurrent, terminalSize)

// we're done processing the input
return line, nil
}
Expand Down Expand Up @@ -101,7 +113,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
//Erase symbols which are left over from older print
EraseLine(ERASE_LINE_END)
// print characters to the new line appropriately
Printf("%c", char)
printChar(char, mask)

}
// erase what's left over from last print
Expand Down Expand Up @@ -219,7 +231,8 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
// print the updated line
for _, char := range line[index:] {
EraseLine(ERASE_LINE_END)
Printf("%c", char)
// print out the character
printChar(char, mask)
}
// erase what's left on last line
if cursorCurrent.Y < terminalSize.Y {
Expand Down Expand Up @@ -249,14 +262,8 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
line = append(line, r)
// save the location of the cursor
index++
// if we don't need to mask the input
if mask == 0 {
// just print the character the user pressed
Printf("%c", r)
} else {
// otherwise print the mask we were given
Printf("%c", mask)
}
// print out the character
printChar(r, mask)
} else {
// we are in the middle of the word so we need to insert the character the user pressed
line = append(line[:index], append([]rune{r}, line[index:]...)...)
Expand All @@ -269,11 +276,8 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
// print the updated line
for _, char := range line[index:] {
EraseLine(ERASE_LINE_END)
if mask == 0 {
Printf("%c", char)
} else {
Printf("%c", mask)
}
// print out the character
printChar(char, mask)
cursorCurrent.X++
}
// if we are at the last line, we want to visually insert a new line and append to it.
Expand Down

0 comments on commit e752db4

Please sign in to comment.