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
for ; size != 0; ch, size = terminal.ReadKey() {
if ch == fn.Ctrl('q') { if ch == fn.Ctrl('q') {
// Clean up on exit // Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor) terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
return false return false
} else { }
// Back up so that the input can be read as a string
terminal.Write(terminal.ClearScreen + terminal.ResetCursor) runes = append(runes, ch)
fmt.Printf("%v\r\n", keyMap)
print(runes)
}
terminal.Write("%v", runes)
return false return false
// terminal.UnReadKey()
}
str := terminal.Read() //str = terminal.Read()
//
//// Handle simplest inputs first
//ch, _ := terminal.ReadKey()
//// Escape sequences can be less fun...
if strings.Contains(str, terminal.EscPrefix) {
code := strings.TrimPrefix(str, terminal.EscPrefix)
switch code { //runes := terminal.Read()
case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: //str := string(runes)
e.moveCursor(code) //
return true //terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
default: //terminal.WriteLn(fmt.Sprintf("%v\r\r", runes))
// Do something later //
} ////// Escape sequences can be less fun...
} //if strings.Contains(str, terminal.EscPrefix) {
// code := strings.TrimPrefix(str, terminal.EscPrefix)
return true //
// 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 {
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
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 // Print string to stdout
func Write(s string) { func Write(s string, a ...interface{}) {
fmt.Print(s) fmt.Printf(s, a...)
}
func WriteLn(s string, a ...interface{}) {
str := fmt.Sprintf(s, a...)
Write("%s\r\n", str)
} }