Use buffer for creating output

This commit is contained in:
Timothy Warren 2021-03-24 15:09:28 -04:00
parent 63cad6d012
commit bb6f7764a1
3 changed files with 83 additions and 49 deletions

10
internal/ansi/ansi.go Normal file
View File

@ -0,0 +1,10 @@
package ansi
import "fmt"
const ClearScreen = "\x1b[2J"
const ResetCursor = "\x1b[H"
func Code (s string) string {
return fmt.Sprintf("\x1b[%s", s)
}

View File

@ -1,32 +1,39 @@
package editor package editor
import ( import (
"fmt"
"strings"
"timshome.page/gilo/internal/ansi"
"timshome.page/gilo/internal/char" "timshome.page/gilo/internal/char"
"timshome.page/gilo/internal/terminal" "timshome.page/gilo/internal/terminal"
) )
// ----------------------------------------------------------------------------
// !Editor
// ----------------------------------------------------------------------------
type editor struct { type editor struct {
rows int rows int
cols int cols int
} }
func New() *editor { func New() *editor {
cols, rows := terminal.Size() rows, cols := terminal.Size()
e := new(editor) return &editor{rows, cols}
e.cols = cols
e.rows = rows
return e
} }
func (e *editor) RefreshScreen() { func (e *editor) RefreshScreen() {
terminal.ANSICode(terminal.ClearScreen) ab := newBuffer()
terminal.ANSICode(terminal.ResetCursor)
e.drawRows() ab.append(ansi.ClearScreen)
ab.append(ansi.ResetCursor)
terminal.ANSICode(terminal.ResetCursor) e.drawRows(ab)
ab.append(ansi.ResetCursor)
terminal.Write(ab.toString())
} }
func (e *editor) ProcessKeypress() bool { func (e *editor) ProcessKeypress() bool {
@ -34,29 +41,61 @@ func (e *editor) ProcessKeypress() bool {
// Clean up on exit // Clean up on exit
if ch == char.Ctrl('q') { if ch == char.Ctrl('q') {
terminal.ANSICode(terminal.ClearScreen) terminal.Write(ansi.ClearScreen)
terminal.ANSICode(terminal.ResetCursor) terminal.Write(ansi.ResetCursor)
return false return false
} }
// Ugliest syntax structure ever?
switch {
case char.IsCtrl(ch):
terminal.WriteLn("%d", ch)
default:
terminal.WriteLn("%d ('%c')", ch, ch)
}
return true return true
} }
func (e *editor) drawRows() { func (e *editor) drawRows(ab *buffer) {
for y :=0; y < e.rows; y += 1 { for y :=0; y < e.rows; y += 1 {
terminal.Write("~") ab.appendRune('~')
if y < (e.rows - 1) { if y < (e.rows - 1) {
terminal.Write("\r\n") 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()
}

View File

@ -8,9 +8,6 @@ import (
"golang.org/x/term" "golang.org/x/term"
) )
const ClearScreen = "2J"
const ResetCursor = "H"
var reader = bufio.NewReader(os.Stdin) var reader = bufio.NewReader(os.Stdin)
func ReadKey() (rune, int) { func ReadKey() (rune, int) {
@ -49,10 +46,6 @@ func RawOff(oldState *term.State) {
} }
} }
func ANSICode (code string) {
fmt.Printf("\x1b[%s", code)
}
func sizeTrick () (rows int, cols int) { func sizeTrick () (rows int, cols int) {
// Move cursor to location further than likely screen size // Move cursor to location further than likely screen size
// The cursor will move to maximum available position // The cursor will move to maximum available position
@ -84,32 +77,24 @@ func sizeTrick () (rows int, cols int) {
return rows, cols return rows, cols
} }
func Size () (width int, height int) { // Get the size of the terminal in rows and columns
width = 80 func Size () (rows int, cols int) {
height = 24 cols = 80
rows = 24
// Try the syscall first // Try the syscall first
width, height, err := term.GetSize(int(os.Stdin.Fd())) cols, rows, err := term.GetSize(int(os.Stdin.Fd()))
if err == nil { if err == nil {
return width, height return rows, cols
} }
// Figure out the size the hard way // Figure out the size the hard way
height, width = sizeTrick() rows, cols = sizeTrick()
WriteLn("%d Rows, %d Cols", height, width) return rows, cols
return width, height
} }
// Print string to stdout
// Print a formatted string to stdout, with CRLF line endings for proper terminal formatting func Write(s string) {
func WriteLn(format string, a ...interface{}) { fmt.Print(s)
formatted := fmt.Sprintf(format, a...)
fmt.Printf("%s\r\n", formatted)
}
func Write(format string, a ...interface{}) {
fmt.Printf(format, a...)
} }