diff --git a/terminal/cursor.go b/terminal/cursor.go index 37b149ca..8932cfaf 100644 --- a/terminal/cursor.go +++ b/terminal/cursor.go @@ -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 diff --git a/terminal/cursor_windows.go b/terminal/cursor_windows.go index 85919284..24f51ece 100644 --- a/terminal/cursor_windows.go +++ b/terminal/cursor_windows.go @@ -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()) diff --git a/terminal/runereader.go b/terminal/runereader.go index d4650f48..46798c6e 100644 --- a/terminal/runereader.go +++ b/terminal/runereader.go @@ -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 @@ -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 } @@ -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 @@ -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 { @@ -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:]...)...) @@ -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.