Move more stuff around
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-04-02 13:11:54 -04:00
parent 6255936e35
commit 1cc35c95a7
8 changed files with 76 additions and 74 deletions

View File

@ -1,7 +1,7 @@
package editor package editor
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// !Constants // !General Constants
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const ( const (
@ -9,3 +9,19 @@ const (
KiloTabStop = 4 KiloTabStop = 4
KiloQuitTimes = 3 KiloQuitTimes = 3
) )
// ----------------------------------------------------------------------------
// !Constants representing input keys
// ----------------------------------------------------------------------------
const (
keyUp = "ARROW_UP"
keyDown = "ARROW_DOWN"
keyLeft = "ARROW_LEFT"
keyRight = "ARROW_RIGHT"
keyPageUp = "PAGE_UP"
keyPageDown = "PAGE_DOWN"
keyHome = "HOME"
keyEnd = "END"
keyDelete = "DELETE"
)

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"log" "log"
"os" "os"
"timshome.page/gilo/internal/buffer"
) )
type document struct { type document struct {
@ -83,14 +84,14 @@ func (d *document) appendRow(s string) {
} }
func (d *document) toString() string { func (d *document) toString() string {
buf := newBuffer() buf := buffer.NewBuffer()
for _, row := range d.rows { for _, row := range d.rows {
buf.append(row.toString()) buf.Append(row.toString())
buf.appendRune('\n') buf.AppendRune('\n')
} }
return buf.toString() return buf.ToString()
} }
func (d *document) rowCount() int { func (d *document) rowCount() int {

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"time" "time"
"timshome.page/gilo/internal/buffer"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"
) )
@ -15,20 +16,20 @@ import (
func (e *editor) RefreshScreen() { func (e *editor) RefreshScreen() {
e.scroll() e.scroll()
ab := newBuffer() ab := buffer.NewBuffer()
ab.append(terminal.HideCursor) ab.Append(terminal.HideCursor)
ab.append(terminal.ResetCursor) ab.Append(terminal.ResetCursor)
e.drawRows(ab) e.drawRows(ab)
e.drawStatusBar(ab) e.drawStatusBar(ab)
e.drawMessageBar(ab) e.drawMessageBar(ab)
ab.append(terminal.MoveCursor(e.renderX-e.offset.x, e.cursor.y-e.offset.y)) ab.Append(terminal.MoveCursor(e.renderX-e.offset.x, e.cursor.y-e.offset.y))
ab.append(terminal.ShowCursor) ab.Append(terminal.ShowCursor)
terminal.Write(ab.toString()) terminal.Write(ab.ToString())
} }
func (e *editor) scroll() { func (e *editor) scroll() {
@ -55,7 +56,7 @@ func (e *editor) scroll() {
} }
} }
func (e *editor) drawRows(ab *buffer) { func (e *editor) drawRows(ab *buffer.Buffer) {
for y := 0; y < e.screen.Rows; y++ { for y := 0; y < e.screen.Rows; y++ {
fileRow := y + e.offset.y fileRow := y + e.offset.y
@ -67,21 +68,21 @@ func (e *editor) drawRows(ab *buffer) {
// If the column offset is greater than the length of the row, // If the column offset is greater than the length of the row,
// just display an empty row // just display an empty row
if e.offset.x > rawRow.size() { if e.offset.x > rawRow.size() {
ab.append("") ab.Append("")
continue continue
} }
rowLen := e.document.rows[fileRow].rSize() - e.offset.x rowLen := e.document.rows[fileRow].rSize() - e.offset.x
outputRow := truncate(string(e.document.rows[fileRow].render[e.offset.x:]), rowLen) outputRow := truncate(string(e.document.rows[fileRow].render[e.offset.x:]), rowLen)
ab.append(outputRow) ab.Append(outputRow)
} }
ab.appendLn(terminal.ClearLine) ab.AppendLn(terminal.ClearLine)
} }
} }
func (e *editor) drawPlaceholderRow(y int, ab *buffer) { func (e *editor) drawPlaceholderRow(y int, ab *buffer.Buffer) {
if e.document.rowCount() == 0 && y == e.screen.Rows/3 { if e.document.rowCount() == 0 && y == e.screen.Rows/3 {
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion) welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
if len(welcome) > e.screen.Cols { if len(welcome) > e.screen.Cols {
@ -90,25 +91,25 @@ func (e *editor) drawPlaceholderRow(y int, ab *buffer) {
padding := (e.screen.Cols - len(welcome)) / 2 padding := (e.screen.Cols - len(welcome)) / 2
if padding > 0 { if padding > 0 {
ab.appendRune('~') ab.AppendRune('~')
padding-- padding--
} }
for padding > 0 { for padding > 0 {
padding-- padding--
ab.appendRune(' ') ab.AppendRune(' ')
} }
ab.append(welcome) ab.Append(welcome)
} else { } else {
ab.appendRune('~') ab.AppendRune('~')
} }
} }
func (e *editor) drawStatusBar(ab *buffer) { func (e *editor) drawStatusBar(ab *buffer.Buffer) {
cols := e.screen.Cols cols := e.screen.Cols
ab.append(terminal.InvertColor) ab.Append(terminal.InvertColor)
fileName := "[No Name]" fileName := "[No Name]"
if e.document.filename != "" { if e.document.filename != "" {
@ -124,8 +125,8 @@ func (e *editor) drawStatusBar(ab *buffer) {
if length > cols { if length > cols {
leftStatus = truncate(leftStatus, cols) leftStatus = truncate(leftStatus, cols)
ab.append(leftStatus) ab.Append(leftStatus)
ab.append(terminal.ResetColor) ab.Append(terminal.ResetColor)
return return
} }
@ -138,30 +139,30 @@ func (e *editor) drawStatusBar(ab *buffer) {
paddingLength := cols - statusLength paddingLength := cols - statusLength
padding := strings.Repeat(" ", paddingLength) padding := strings.Repeat(" ", paddingLength)
ab.append(leftStatus) ab.Append(leftStatus)
ab.append(padding) ab.Append(padding)
ab.append(rightStatus) ab.Append(rightStatus)
ab.append(terminal.ResetColor) ab.Append(terminal.ResetColor)
return return
} }
ab.append(leftStatus) ab.Append(leftStatus)
// Pad the rest of the status line // Pad the rest of the status line
padding := strings.Repeat(" ", cols-length) padding := strings.Repeat(" ", cols-length)
ab.append(padding) ab.Append(padding)
ab.append(terminal.ResetColor) ab.Append(terminal.ResetColor)
} }
func (e *editor) drawMessageBar(ab *buffer) { func (e *editor) drawMessageBar(ab *buffer.Buffer) {
ab.append("\r\n") ab.Append("\r\n")
ab.append(terminal.ClearLine) ab.Append(terminal.ClearLine)
msg := truncate(e.status.message, e.screen.Cols) msg := truncate(e.status.message, e.screen.Cols)
if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 { if len(msg) > 0 && time.Since(e.status.created).Seconds() < 5.0 {
ab.append(msg) ab.Append(msg)
} }
} }

View File

@ -1,3 +1,4 @@
// The main interface/implementation of the editor object
package editor package editor
import ( import (

View File

@ -12,9 +12,9 @@ type moveCursor struct {
} }
var cursorTests = []moveCursor{ var cursorTests = []moveCursor{
{[]string{"\x1b"}, false, &point{0, 0}}, {[]string{string(rune(key.Esc))}, false, &point{0, 0}},
{[]string{keyRight}, true, &point{1, 0}}, {[]string{keyRight}, true, &point{1, 0}},
{[]string{keyEnd}, true, &point{14, 0}}, {[]string{keyDown, keyEnd}, true, &point{14, 1}},
{[]string{keyEnd, keyHome}, true, &point{0, 0}}, {[]string{keyEnd, keyHome}, true, &point{0, 0}},
{[]string{keyRight, keyLeft}, true, &point{0, 0}}, {[]string{keyRight, keyLeft}, true, &point{0, 0}},
{[]string{keyDown, keyUp}, true, &point{0, 0}}, {[]string{keyDown, keyUp}, true, &point{0, 0}},
@ -29,8 +29,8 @@ func TestMoveCursor(t *testing.T) {
e.Open("editor.go") e.Open("editor.go")
} }
for _, key := range test.keys { for _, k := range test.keys {
e.moveCursor(key) e.moveCursor(k)
} }
want := test.cursor want := test.cursor
got := e.cursor got := e.cursor

View File

@ -1,17 +0,0 @@
package editor
// ----------------------------------------------------------------------------
// !Constants representing input keys
// ----------------------------------------------------------------------------
const (
keyUp = "ARROW_UP"
keyDown = "ARROW_DOWN"
keyLeft = "ARROW_LEFT"
keyRight = "ARROW_RIGHT"
keyPageUp = "PAGE_UP"
keyPageDown = "PAGE_DOWN"
keyHome = "HOME"
keyEnd = "END"
keyDelete = "DELETE"
)

View File

@ -1,4 +1,4 @@
package editor package buffer
import ( import (
"fmt" "fmt"
@ -9,38 +9,38 @@ import (
// !Output Buffer // !Output Buffer
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
type buffer struct { type Buffer struct {
buf *strings.Builder buf *strings.Builder
} }
func newBuffer() *buffer { func NewBuffer() *Buffer {
var buf strings.Builder var buf strings.Builder
b := new(buffer) b := new(Buffer)
b.buf = &buf b.buf = &buf
return b return b
} }
func (b *buffer) appendRune(r rune) int { func (b *Buffer) AppendRune(r rune) int {
size, _ := b.buf.WriteRune(r) size, _ := b.buf.WriteRune(r)
return size return size
} }
func (b *buffer) append(s string) int { func (b *Buffer) Append(s string) int {
size, _ := b.buf.WriteString(s) size, _ := b.buf.WriteString(s)
return size return size
} }
func (b *buffer) appendLn(s string) int { func (b *Buffer) AppendLn(s string) int {
str := fmt.Sprintf("%s\r\n", s) str := fmt.Sprintf("%s\r\n", s)
size, _ := b.buf.WriteString(str) size, _ := b.buf.WriteString(str)
return size return size
} }
func (b *buffer) toString() string { func (b *Buffer) ToString() string {
return b.buf.String() return b.buf.String()
} }

View File

@ -1,11 +1,11 @@
package editor package buffer
import "testing" import "testing"
func TestBufferAppendRune(t *testing.T) { func TestBufferAppendRune(t *testing.T) {
b := newBuffer() b := NewBuffer()
got := b.appendRune('a') got := b.AppendRune('a')
want := 1 want := 1
if got != want { if got != want {
@ -14,9 +14,9 @@ func TestBufferAppendRune(t *testing.T) {
} }
func TestBufferAppend(t *testing.T) { func TestBufferAppend(t *testing.T) {
b := newBuffer() b := NewBuffer()
got := b.append("123") got := b.Append("123")
want := 3 want := 3
if got != want { if got != want {
@ -25,9 +25,9 @@ func TestBufferAppend(t *testing.T) {
} }
func TestBufferAppendLn(t *testing.T) { func TestBufferAppendLn(t *testing.T) {
b := newBuffer() b := NewBuffer()
got := b.appendLn("123") got := b.AppendLn("123")
// Adds carriage return and line feed // Adds carriage return and line feed
want := 5 want := 5
@ -38,10 +38,10 @@ func TestBufferAppendLn(t *testing.T) {
} }
func TestBufferToString(t *testing.T) { func TestBufferToString(t *testing.T) {
b := newBuffer() b := NewBuffer()
b.append("foo") b.Append("foo")
got := b.toString() got := b.ToString()
want := "foo" want := "foo"
if got != want { if got != want {