Reorganize a little bit

This commit is contained in:
Timothy Warren 2021-03-26 16:18:03 -04:00
parent f205887294
commit 663c4304f0
4 changed files with 147 additions and 136 deletions

View File

@ -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()
}

99
editor/draw.go Normal file
View File

@ -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()
}

View File

@ -1,8 +1,6 @@
package editor package editor
import ( import (
"fmt"
"strings"
"timshome.page/gilo/fn" "timshome.page/gilo/fn"
"timshome.page/gilo/terminal" "timshome.page/gilo/terminal"
) )
@ -28,84 +26,53 @@ func New() *editor {
return &editor{screen, cursor } 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 { func (e *editor) ProcessKeypress() bool {
// Handle simplest inputs first ch, size := terminal.ReadKey()
ch, _ := terminal.ReadKey() var runes []rune
if ch == fn.Ctrl('q') {
// Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
return false for ; size != 0; ch, size = terminal.ReadKey() {
} else { if ch == fn.Ctrl('q') {
// Back up so that the input can be read as a string // Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor) terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
fmt.Printf("%v\r\n", keyMap)
return false
// terminal.UnReadKey()
}
str := terminal.Read() return false
//// 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
} }
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 { //func (e *editor) processEscapeCode() bool {

View File

@ -17,22 +17,13 @@ func ReadKey() (rune, int) {
return ch, size return ch, size
} }
func Read() string { // Print string to stdout
scanner := bufio.NewScanner(os.Stdin) func Write(s string, a ...interface{}) {
if scanner.Scan() { fmt.Printf(s, a...)
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 WriteLn(s string, a ...interface{}) {
func Write(s string) { str := fmt.Sprintf(s, a...)
fmt.Print(s)
Write("%s\r\n", str)
} }