Skip to content

Commit

Permalink
Fix last and first character deletion for Windows Terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
iilun committed Dec 16, 2023
1 parent 4f5fbae commit 8d51e00
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions terminal/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

var COORDINATE_SYSTEM_BEGIN Short = 1

var CURSOR_STAGNATES_ON_LAST_CHAR = false

var dsrPattern = regexp.MustCompile(`\x1b\[(\d+);(\d+)R$`)

type Cursor struct {
Expand Down
12 changes: 12 additions & 0 deletions terminal/cursor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ package terminal
import (
"bytes"
"fmt"
log "github.com/Iilun/survey/v2/internal"
"os"
"syscall"
"unsafe"
)

var COORDINATE_SYSTEM_BEGIN Short = 0

var CURSOR_STAGNATES_ON_LAST_CHAR = false

func init() {
if windowsTerminalSession := os.Getenv("WT_SESSION"); windowsTerminalSession != "" {
CURSOR_STAGNATES_ON_LAST_CHAR = true
}
CURSOR_STAGNATES_ON_LAST_CHAR = true
log.Printf("Cursor stagnates %t", CURSOR_STAGNATES_ON_LAST_CHAR)
}

// shared variable to save the cursor location from CursorSave()
var cursorLoc Coord

Expand Down
11 changes: 7 additions & 4 deletions terminal/runereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ func (rr *RuneReader) ReadLineWithDefault(mask rune, d []rune, onRunes ...OnRune
// we set the current location of the cursor once
cursorCurrent, _ := cursor.Location(rr.Buffer())
// CHANGE THIS
wroteOnLine := true
wroteOnLine := COORDINATE_SYSTEM_BEGIN == 0
increment := func() {
if !cursorIsInLastPosition && cursorCurrent.CursorIsAtLineEnd(terminalSize) {
if CURSOR_STAGNATES_ON_LAST_CHAR && !cursorIsInLastPosition && cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursorIsInLastPosition = true
} else if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursorCurrent.X = 1
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
if CURSOR_STAGNATES_ON_LAST_CHAR {
cursorCurrent.X++
}
cursorCurrent.Y++
wroteOnLine = true
wroteOnLine = COORDINATE_SYSTEM_BEGIN == 0
cursorIsInLastPosition = false
} else {
cursorCurrent.X++
Expand Down

0 comments on commit 8d51e00

Please sign in to comment.