From d04b4bec84920be1d1cda80c5888656b0a1148bd Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 24 Mar 2021 16:23:17 -0400 Subject: [PATCH] Fewer folders, more files --- editor/buffer.go | 46 ++++++++++++ {internal/editor => editor}/editor.go | 59 +++------------ {internal/char => fn}/char.go | 2 +- gilo.go | 4 +- {internal/ansi => terminal}/ansi.go | 2 +- terminal/io.go | 23 ++++++ terminal/raw.go | 34 +++++++++ .../terminal/terminal.go => terminal/size.go | 71 ++++--------------- 8 files changed, 129 insertions(+), 112 deletions(-) create mode 100644 editor/buffer.go rename {internal/editor => editor}/editor.go (52%) rename {internal/char => fn}/char.go (96%) rename {internal/ansi => terminal}/ansi.go (93%) create mode 100644 terminal/io.go create mode 100644 terminal/raw.go rename internal/terminal/terminal.go => terminal/size.go (57%) diff --git a/editor/buffer.go b/editor/buffer.go new file mode 100644 index 0000000..32f5b91 --- /dev/null +++ b/editor/buffer.go @@ -0,0 +1,46 @@ +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/internal/editor/editor.go b/editor/editor.go similarity index 52% rename from internal/editor/editor.go rename to editor/editor.go index cc1b46e..7171c23 100644 --- a/internal/editor/editor.go +++ b/editor/editor.go @@ -2,11 +2,8 @@ package editor import ( "fmt" - "strings" "timshome.page/gilo/fn" - "timshome.page/gilo/internal/ansi" - "timshome.page/gilo/internal/char" - "timshome.page/gilo/internal/terminal" + "timshome.page/gilo/terminal" ) const KiloVersion = "0.0.1" @@ -29,13 +26,13 @@ func New() *editor { func (e *editor) RefreshScreen() { ab := newBuffer() - ab.append(ansi.HideCursor) - ab.append(ansi.ResetCursor) + ab.append(terminal.HideCursor) + ab.append(terminal.ResetCursor) e.drawRows(ab) - ab.append(ansi.ResetCursor) - ab.append(ansi.ShowCursor) + ab.append(terminal.ResetCursor) + ab.append(terminal.ShowCursor) terminal.Write(ab.toString()) } @@ -44,8 +41,8 @@ func (e *editor) ProcessKeypress() bool { ch, _ := terminal.ReadKey() // Clean up on exit - if ch == char.Ctrl('q') { - terminal.Write(ansi.ClearScreen + ansi.ResetCursor) + if ch == fn.Ctrl('q') { + terminal.Write(terminal.ClearScreen + terminal.ResetCursor) return false } @@ -77,50 +74,10 @@ func (e *editor) drawRows(ab *buffer) { ab.appendRune('~') } - ab.append(ansi.ClearLine) + ab.append(terminal.ClearLine) if y < (e.rows - 1) { ab.append("\r\n") } } -} - -// ---------------------------------------------------------------------------- -// !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/internal/char/char.go b/fn/char.go similarity index 96% rename from internal/char/char.go rename to fn/char.go index 33e8648..6bf25a5 100644 --- a/internal/char/char.go +++ b/fn/char.go @@ -1,4 +1,4 @@ -package char +package fn func isAscii(char rune) bool { ord := int(char) diff --git a/gilo.go b/gilo.go index 80a2a31..2eea5ae 100644 --- a/gilo.go +++ b/gilo.go @@ -1,8 +1,8 @@ package main import ( - "timshome.page/gilo/internal/editor" - "timshome.page/gilo/internal/terminal" + "timshome.page/gilo/editor" + "timshome.page/gilo/terminal" ) func main() { diff --git a/internal/ansi/ansi.go b/terminal/ansi.go similarity index 93% rename from internal/ansi/ansi.go rename to terminal/ansi.go index 83b8bbf..0a87565 100644 --- a/internal/ansi/ansi.go +++ b/terminal/ansi.go @@ -1,4 +1,4 @@ -package ansi +package terminal import "fmt" diff --git a/terminal/io.go b/terminal/io.go new file mode 100644 index 0000000..ccb92cf --- /dev/null +++ b/terminal/io.go @@ -0,0 +1,23 @@ +package terminal + +import ( + "bufio" + "fmt" + "os" +) + +var reader = bufio.NewReader(os.Stdin) + +func ReadKey() (rune, int) { + ch, size, err := reader.ReadRune() + if err != nil { + panic(err) + } + + return ch, size +} + +// Print string to stdout +func Write(s string) { + fmt.Print(s) +} \ No newline at end of file diff --git a/terminal/raw.go b/terminal/raw.go new file mode 100644 index 0000000..512903d --- /dev/null +++ b/terminal/raw.go @@ -0,0 +1,34 @@ +package terminal + +import ( + "os" + + "golang.org/x/term" +) + +// Put the terminal in raw mode +func RawOn() *term.State { + check() + + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + + return oldState +} + +// Restore the terminal to canonical mode +func RawOff(oldState *term.State) { + err := term.Restore(int(os.Stdin.Fd()), oldState) + if err != nil { + panic(err) + } +} + +// Is this a valid interactive terminal? +func check() { + if !term.IsTerminal(int(os.Stdin.Fd())) { + panic("An interactive terminal is required to use a text editor!") + } +} diff --git a/internal/terminal/terminal.go b/terminal/size.go similarity index 57% rename from internal/terminal/terminal.go rename to terminal/size.go index dafe9c5..89c765a 100644 --- a/internal/terminal/terminal.go +++ b/terminal/size.go @@ -1,49 +1,28 @@ package terminal import ( - "bufio" "fmt" - "os" - "golang.org/x/term" + "os" ) -var reader = bufio.NewReader(os.Stdin) - -func ReadKey() (rune, int) { - ch, size, err := reader.ReadRune() - if err != nil { - panic(err) - } - - return ch, size -} - -// Is this a valid interactive terminal? -func check() { - if !term.IsTerminal(int(os.Stdin.Fd())) { - panic("An interactive terminal is required to use a text editor!") - } -} - -// Put the terminal in raw mode -func RawOn() *term.State { +// Get the size of the terminal in rows and columns +func Size () (rows int, cols int) { check() - oldState, err := term.MakeRaw(int(os.Stdin.Fd())) - if err != nil { - panic(err) + cols = 80 + rows = 24 + + // Try the syscall first + cols, rows, err := term.GetSize(int(os.Stdin.Fd())) + if err == nil { + return rows, cols } - return oldState -} + // Figure out the size the hard way + rows, cols = sizeTrick() -// Restore the terminal to canonical mode -func RawOff(oldState *term.State) { - err := term.Restore(int(os.Stdin.Fd()), oldState) - if err != nil { - panic(err) - } + return rows, cols } func sizeTrick () (rows int, cols int) { @@ -75,26 +54,4 @@ func sizeTrick () (rows int, cols int) { } return rows, cols -} - -// Get the size of the terminal in rows and columns -func Size () (rows int, cols int) { - cols = 80 - rows = 24 - - // Try the syscall first - cols, rows, err := term.GetSize(int(os.Stdin.Fd())) - if err == nil { - return rows, cols - } - - // Figure out the size the hard way - rows, cols = sizeTrick() - - return rows, cols -} - -// Print string to stdout -func Write(s string) { - fmt.Print(s) -} +} \ No newline at end of file