Squashed some bugs, still working out right arrow and having the cursor start at the end of a seed.
This commit is contained in:
parent
1aa59e8f4b
commit
ab656bd3b3
49
main.go
49
main.go
|
@ -36,6 +36,7 @@ type Buffer struct {
|
|||
maxWidth int
|
||||
offset int
|
||||
cursorStart int
|
||||
prompt string
|
||||
}
|
||||
|
||||
func (lb Buffer) String() string {
|
||||
|
@ -47,10 +48,11 @@ func (lb *Buffer) deleteChar() {
|
|||
return
|
||||
} else if lb.cursor == len(lb.buf)-1 {
|
||||
lb.buf = lb.buf[:len(lb.buf)-1]
|
||||
fmt.Printf("\033[0K")
|
||||
} else {
|
||||
lb.buf = append(lb.buf[:lb.cursor], lb.buf[lb.cursor+1:]...)
|
||||
fmt.Printf("%s\033[0K\033[%dD", string(lb.buf[lb.cursor:]), len(lb.buf)-lb.cursor)
|
||||
}
|
||||
if lb.offset > 0 {
|
||||
lb.offset--
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,17 +66,13 @@ func (lb *Buffer) addChar(c rune, echo bool) {
|
|||
if lb.cursor == len(lb.buf) {
|
||||
lb.buf = append(lb.buf, c)
|
||||
lb.cursor++
|
||||
// if echo {
|
||||
// fmt.Printf("%c", c)
|
||||
// }
|
||||
} else {
|
||||
lb.buf = append(lb.buf[:lb.cursor+1], lb.buf[lb.cursor:]...)
|
||||
lb.buf[lb.cursor] = c
|
||||
lb.cursor++
|
||||
// if echo {
|
||||
// s := string(lb.buf[lb.cursor-1:])
|
||||
// fmt.Printf("%s\033[%dD", s, len(s)-1)
|
||||
// }
|
||||
}
|
||||
if lb.cursor - lb.offset > lb.maxWidth {
|
||||
lb.offset++
|
||||
}
|
||||
if echo {
|
||||
lb.printBuf()
|
||||
|
@ -88,12 +86,16 @@ func min(a, b int) int {
|
|||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (lb Buffer) printBuf() {
|
||||
out := lb.buf[lb.offset:min(len(lb.buf), lb.offset+lb.maxWidth)]
|
||||
if lb.cursor - 1 != 0 {
|
||||
fmt.Printf("\033[%dD", lb.cursor-1)
|
||||
}
|
||||
fmt.Printf("%s\033[%dD\033[%dC", string(out), len(out), lb.cursor)
|
||||
fmt.Printf("\r%s%s\033[0K\r\033[%dC", lb.prompt, string(out), lb.cursor - lb.offset + len(lb.prompt))
|
||||
}
|
||||
|
||||
func (lb *Buffer) controlInput(c rune) {
|
||||
|
@ -103,30 +105,33 @@ func (lb *Buffer) controlInput(c rune) {
|
|||
case BackSpace:
|
||||
if lb.cursor > 0 {
|
||||
lb.cursor--
|
||||
fmt.Print("\033[1D")
|
||||
lb.deleteChar()
|
||||
}
|
||||
case LeftArrow:
|
||||
if lb.offset > 0 && lb.cursor - lb.offset == 0 {
|
||||
lb.offset--
|
||||
}
|
||||
if lb.cursor > 0 {
|
||||
lb.cursor--
|
||||
fmt.Print("\033[1D")
|
||||
}
|
||||
case RightArrow:
|
||||
// FIXME: This still isnt working quite right...
|
||||
for ;lb.cursor - lb.offset >= lb.maxWidth && lb.cursor < len(lb.buf); {
|
||||
lb.offset++
|
||||
}
|
||||
if lb.cursor < len(lb.buf) {
|
||||
lb.cursor++
|
||||
fmt.Print("\033[1C")
|
||||
}
|
||||
case Home:
|
||||
if lb.cursor > 0 {
|
||||
fmt.Printf("\033[%dD", lb.cursor)
|
||||
lb.offset = 0
|
||||
lb.cursor = 0
|
||||
}
|
||||
case End:
|
||||
if lb.cursor < len(lb.buf) {
|
||||
fmt.Printf("\033[%dC", len(lb.buf)-lb.cursor)
|
||||
lb.cursor = len(lb.buf)
|
||||
lb.offset = max(lb.cursor - lb.maxWidth, 0)
|
||||
}
|
||||
}
|
||||
lb.printBuf()
|
||||
}
|
||||
|
||||
func (lb *Buffer) seedContent(s string) {
|
||||
|
@ -144,12 +149,12 @@ func parseCursorPosition(esc string) (int, int, error) {
|
|||
|
||||
func GetText(prompt string, content string) string {
|
||||
cols, _ := termios.GetWindowSize()
|
||||
b := Buffer{make([]rune, 0, (len(content)+1)*2), 0, cols-len(prompt), 0, 0}
|
||||
b := Buffer{make([]rune, 0, (len(content)+1)*2), 0, cols-len(prompt), 0, 0, prompt}
|
||||
b.seedContent(content)
|
||||
|
||||
fmt.Print(prompt)
|
||||
fmt.Print("\033[6n")
|
||||
fmt.Print(b.String())
|
||||
b.printBuf()
|
||||
|
||||
var ch rune
|
||||
var err error
|
||||
|
|
Loading…
Reference in New Issue