From 663c4304f0b013c17d7c97e873855deda825a551 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 26 Mar 2021 16:18:03 -0400 Subject: [PATCH] Reorganize a little bit --- editor/buffer.go | 46 ------------------- editor/draw.go | 99 ++++++++++++++++++++++++++++++++++++++++ editor/editor.go | 115 +++++++++++++++++------------------------------ terminal/io.go | 23 +++------- 4 files changed, 147 insertions(+), 136 deletions(-) delete mode 100644 editor/buffer.go create mode 100644 editor/draw.go diff --git a/editor/buffer.go b/editor/buffer.go deleted file mode 100644 index 32f5b91..0000000 --- a/editor/buffer.go +++ /dev/null @@ -1,46 +0,0 @@ -package editor - -import ( - "fmt" - "strings" -) - -// ---------------------------------------------------------------------------- -// !buffer -// ---------------------------------------------------------------------------- - -type buffer struct { - buf *strings.Builder -} - -func newBuffer() *buffer { - var buf strings.Builder - - b := new(buffer) - b.buf = &buf - - return b -} - -func (b *buffer) appendRune(r rune) int { - size, _ := b.buf.WriteRune(r) - - return size -} - -func (b *buffer) append(s string) int { - size, _ := b.buf.WriteString(s) - - return size -} - -func (b *buffer) appendLn(s string) int { - str := fmt.Sprintf("%s\r\n", s) - size, _ := b.buf.WriteString(str) - - return size -} - -func (b *buffer) toString() string { - return b.buf.String() -} \ No newline at end of file diff --git a/editor/draw.go b/editor/draw.go new file mode 100644 index 0000000..2d1e926 --- /dev/null +++ b/editor/draw.go @@ -0,0 +1,99 @@ +// Editor methods involved in drawing to the console +package editor + +import ( + "fmt" + "strings" + "timshome.page/gilo/fn" + "timshome.page/gilo/terminal" +) + +// ---------------------------------------------------------------------------- +// !Editor Methods +// ---------------------------------------------------------------------------- + +func (e *editor) RefreshScreen() { + ab := newBuffer() + + ab.append(terminal.HideCursor) + ab.append(terminal.ResetCursor) + + e.drawRows(ab) + ab.append(terminal.MoveCursor(e.cursor.x, e.cursor.y)) + + ab.append(terminal.ShowCursor) + + terminal.Write(ab.toString()) +} + +func (e *editor) drawRows(ab *buffer) { + for y :=0; y < e.screen.Rows; y += 1 { + if y == e.screen.Rows / 3 { + welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) + if len(welcome) > e.screen.Cols { + welcome = fn.TruncateString(welcome, e.screen.Cols) + } + + padding := (e.screen.Cols - len(welcome)) / 2 + if padding > 0 { + ab.appendRune('~') + padding-- + } + + for padding > 0 { + padding-- + ab.appendRune(' ') + } + + ab.append(welcome) + } else { + ab.appendRune('~') + } + + ab.append(terminal.ClearLine) + + if y < (e.screen.Rows - 1) { + ab.append("\r\n") + } + } +} + +// ---------------------------------------------------------------------------- +// !Output Buffer +// ---------------------------------------------------------------------------- + +type buffer struct { + buf *strings.Builder +} + +func newBuffer() *buffer { + var buf strings.Builder + + b := new(buffer) + b.buf = &buf + + return b +} + +func (b *buffer) appendRune(r rune) int { + size, _ := b.buf.WriteRune(r) + + return size +} + +func (b *buffer) append(s string) int { + size, _ := b.buf.WriteString(s) + + return size +} + +func (b *buffer) appendLn(s string) int { + str := fmt.Sprintf("%s\r\n", s) + size, _ := b.buf.WriteString(str) + + return size +} + +func (b *buffer) toString() string { + return b.buf.String() +} \ No newline at end of file diff --git a/editor/editor.go b/editor/editor.go index ccfba84..a7b2646 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,8 +1,6 @@ package editor import ( - "fmt" - "strings" "timshome.page/gilo/fn" "timshome.page/gilo/terminal" ) @@ -28,84 +26,53 @@ func New() *editor { return &editor{screen, cursor } } -func (e *editor) RefreshScreen() { - ab := newBuffer() - - ab.append(terminal.HideCursor) - ab.append(terminal.ResetCursor) - - e.drawRows(ab) - ab.append(terminal.MoveCursor(e.cursor.x, e.cursor.y)) - - ab.append(terminal.ShowCursor) - - terminal.Write(ab.toString()) -} - -func (e *editor) drawRows(ab *buffer) { - for y :=0; y < e.screen.Rows; y += 1 { - if y == e.screen.Rows / 3 { - welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) - if len(welcome) > e.screen.Cols { - welcome = fn.TruncateString(welcome, e.screen.Cols) - } - - padding := (e.screen.Cols - len(welcome)) / 2 - if padding > 0 { - ab.appendRune('~') - padding-- - } - - for padding > 0 { - padding-- - ab.appendRune(' ') - } - - ab.append(welcome) - } else { - ab.appendRune('~') - } - - ab.append(terminal.ClearLine) - - if y < (e.screen.Rows - 1) { - ab.append("\r\n") - } - } -} - func (e *editor) ProcessKeypress() bool { - // Handle simplest inputs first - ch, _ := terminal.ReadKey() - if ch == fn.Ctrl('q') { - // Clean up on exit - terminal.Write(terminal.ClearScreen + terminal.ResetCursor) + ch, size := terminal.ReadKey() + var runes []rune - return false - } else { - // Back up so that the input can be read as a string - terminal.Write(terminal.ClearScreen + terminal.ResetCursor) - fmt.Printf("%v\r\n", keyMap) - return false - // terminal.UnReadKey() - } + for ; size != 0; ch, size = terminal.ReadKey() { + if ch == fn.Ctrl('q') { + // Clean up on exit + terminal.Write(terminal.ClearScreen + terminal.ResetCursor) - str := terminal.Read() - - //// Escape sequences can be less fun... - if strings.Contains(str, terminal.EscPrefix) { - code := strings.TrimPrefix(str, terminal.EscPrefix) - - switch code { - case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: - e.moveCursor(code) - return true - default: - // Do something later + return false } + + runes = append(runes, ch) + + print(runes) } - return true + terminal.Write("%v", runes) + + return false + + //str = terminal.Read() + // + //// Handle simplest inputs first + //ch, _ := terminal.ReadKey() + + + //runes := terminal.Read() + //str := string(runes) + // + //terminal.Write(terminal.ClearScreen + terminal.ResetCursor) + //terminal.WriteLn(fmt.Sprintf("%v\r\r", runes)) + // + ////// Escape sequences can be less fun... + //if strings.Contains(str, terminal.EscPrefix) { + // code := strings.TrimPrefix(str, terminal.EscPrefix) + // + // switch code { + // case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: + // e.moveCursor(code) + // return true + // default: + // // Do something later + // } + //} + // + //return true } //func (e *editor) processEscapeCode() bool { diff --git a/terminal/io.go b/terminal/io.go index 9dcc283..b329aca 100644 --- a/terminal/io.go +++ b/terminal/io.go @@ -17,22 +17,13 @@ func ReadKey() (rune, int) { return ch, size } -func Read() string { - scanner := bufio.NewScanner(os.Stdin) - if scanner.Scan() { - return scanner.Text() - } - - err := scanner.Err() - - if err != nil { - panic(fmt.Sprintf("Failed to read string from stdin %v", err)) - } - - return "(╯°□°)╯︵ ┻━┻" +// Print string to stdout +func Write(s string, a ...interface{}) { + fmt.Printf(s, a...) } -// Print string to stdout -func Write(s string) { - fmt.Print(s) +func WriteLn(s string, a ...interface{}) { + str := fmt.Sprintf(s, a...) + + Write("%s\r\n", str) } \ No newline at end of file