1
0
Fork 0

Move more stuff around
timw4mail/gilo/pipeline/head This commit looks good Details

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
// ----------------------------------------------------------------------------
// !Constants
// !General Constants
// ----------------------------------------------------------------------------
const (
@ -9,3 +9,19 @@ const (
KiloTabStop = 4
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"
"log"
"os"
"timshome.page/gilo/internal/buffer"
)
type document struct {
@ -83,14 +84,14 @@ func (d *document) appendRow(s string) {
}
func (d *document) toString() string {
buf := newBuffer()
buf := buffer.NewBuffer()
for _, row := range d.rows {
buf.append(row.toString())
buf.appendRune('\n')
buf.Append(row.toString())
buf.AppendRune('\n')
}
return buf.toString()
return buf.ToString()
}
func (d *document) rowCount() int {

View File

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"time"
"timshome.page/gilo/internal/buffer"
"timshome.page/gilo/terminal"
)
@ -15,20 +16,20 @@ import (
func (e *editor) RefreshScreen() {
e.scroll()
ab := newBuffer()
ab := buffer.NewBuffer()
ab.append(terminal.HideCursor)
ab.append(terminal.ResetCursor)
ab.Append(terminal.HideCursor)
ab.Append(terminal.ResetCursor)
e.drawRows(ab)
e.drawStatusBar(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() {
@ -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++ {
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,
// just display an empty row
if e.offset.x > rawRow.size() {
ab.append("")
ab.Append("")
continue
}
rowLen := e.document.rows[fileRow].rSize() - e.offset.x
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 {
welcome := fmt.Sprintf("Gilo editor -- version %s", KiloVersion)
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
if padding > 0 {
ab.appendRune('~')
ab.AppendRune('~')
padding--
}
for padding > 0 {
padding--
ab.appendRune(' ')
ab.AppendRune(' ')
}
ab.append(welcome)
ab.Append(welcome)
} else {
ab.appendRune('~')
ab.AppendRune('~')
}
}
func (e *editor) drawStatusBar(ab *buffer) {
func (e *editor) drawStatusBar(ab *buffer.Buffer) {
cols := e.screen.Cols
ab.append(terminal.InvertColor)
ab.Append(terminal.InvertColor)
fileName := "[No Name]"
if e.document.filename != "" {
@ -124,8 +125,8 @@ func (e *editor) drawStatusBar(ab *buffer) {
if length > cols {
leftStatus = truncate(leftStatus, cols)
ab.append(leftStatus)
ab.append(terminal.ResetColor)
ab.Append(leftStatus)
ab.Append(terminal.ResetColor)
return
}
@ -138,30 +139,30 @@ func (e *editor) drawStatusBar(ab *buffer) {
paddingLength := cols - statusLength
padding := strings.Repeat(" ", paddingLength)
ab.append(leftStatus)
ab.append(padding)
ab.append(rightStatus)
ab.Append(leftStatus)
ab.Append(padding)
ab.Append(rightStatus)
ab.append(terminal.ResetColor)
ab.Append(terminal.ResetColor)
return
}
ab.append(leftStatus)
ab.Append(leftStatus)
// Pad the rest of the status line
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) {
ab.append("\r\n")
ab.append(terminal.ClearLine)
func (e *editor) drawMessageBar(ab *buffer.Buffer) {
ab.Append("\r\n")
ab.Append(terminal.ClearLine)
msg := truncate(e.status.message, e.screen.Cols)
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
import (

View File

@ -12,9 +12,9 @@ type moveCursor struct {
}
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{keyEnd}, true, &point{14, 0}},
{[]string{keyDown, keyEnd}, true, &point{14, 1}},
{[]string{keyEnd, keyHome}, true, &point{0, 0}},
{[]string{keyRight, keyLeft}, true, &point{0, 0}},
{[]string{keyDown, keyUp}, true, &point{0, 0}},
@ -29,8 +29,8 @@ func TestMoveCursor(t *testing.T) {
e.Open("editor.go")
}
for _, key := range test.keys {
e.moveCursor(key)
for _, k := range test.keys {
e.moveCursor(k)
}
want := test.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 (
"fmt"
@ -9,38 +9,38 @@ import (
// !Output Buffer
// ----------------------------------------------------------------------------
type buffer struct {
type Buffer struct {
buf *strings.Builder
}
func newBuffer() *buffer {
func NewBuffer() *Buffer {
var buf strings.Builder
b := new(buffer)
b := new(Buffer)
b.buf = &buf
return b
}
func (b *buffer) appendRune(r rune) int {
func (b *Buffer) AppendRune(r rune) int {
size, _ := b.buf.WriteRune(r)
return size
}
func (b *buffer) append(s string) int {
func (b *Buffer) Append(s string) int {
size, _ := b.buf.WriteString(s)
return size
}
func (b *buffer) appendLn(s string) int {
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 {
func (b *Buffer) ToString() string {
return b.buf.String()
}

View File

@ -1,11 +1,11 @@
package editor
package buffer
import "testing"
func TestBufferAppendRune(t *testing.T) {
b := newBuffer()
b := NewBuffer()
got := b.appendRune('a')
got := b.AppendRune('a')
want := 1
if got != want {
@ -14,9 +14,9 @@ func TestBufferAppendRune(t *testing.T) {
}
func TestBufferAppend(t *testing.T) {
b := newBuffer()
b := NewBuffer()
got := b.append("123")
got := b.Append("123")
want := 3
if got != want {
@ -25,9 +25,9 @@ func TestBufferAppend(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
want := 5
@ -38,10 +38,10 @@ func TestBufferAppendLn(t *testing.T) {
}
func TestBufferToString(t *testing.T) {
b := newBuffer()
b.append("foo")
b := NewBuffer()
b.Append("foo")
got := b.toString()
got := b.ToString()
want := "foo"
if got != want {