Fewer folders, more files

This commit is contained in:
Timothy Warren 2021-03-24 16:23:17 -04:00
parent 1173818135
commit d04b4bec84
8 changed files with 129 additions and 112 deletions

46
editor/buffer.go Normal file
View File

@ -0,0 +1,46 @@
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()
}

View File

@ -2,11 +2,8 @@ package editor
import (
"fmt"
"strings"
"timshome.page/gilo/fn"
"timshome.page/gilo/internal/ansi"
"timshome.page/gilo/internal/char"
"timshome.page/gilo/internal/terminal"
"timshome.page/gilo/terminal"
)
const KiloVersion = "0.0.1"
@ -29,13 +26,13 @@ func New() *editor {
func (e *editor) RefreshScreen() {
ab := newBuffer()
ab.append(ansi.HideCursor)
ab.append(ansi.ResetCursor)
ab.append(terminal.HideCursor)
ab.append(terminal.ResetCursor)
e.drawRows(ab)
ab.append(ansi.ResetCursor)
ab.append(ansi.ShowCursor)
ab.append(terminal.ResetCursor)
ab.append(terminal.ShowCursor)
terminal.Write(ab.toString())
}
@ -44,8 +41,8 @@ func (e *editor) ProcessKeypress() bool {
ch, _ := terminal.ReadKey()
// Clean up on exit
if ch == char.Ctrl('q') {
terminal.Write(ansi.ClearScreen + ansi.ResetCursor)
if ch == fn.Ctrl('q') {
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
return false
}
@ -77,50 +74,10 @@ func (e *editor) drawRows(ab *buffer) {
ab.appendRune('~')
}
ab.append(ansi.ClearLine)
ab.append(terminal.ClearLine)
if y < (e.rows - 1) {
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

@ -1,4 +1,4 @@
package char
package fn
func isAscii(char rune) bool {
ord := int(char)

View File

@ -1,8 +1,8 @@
package main
import (
"timshome.page/gilo/internal/editor"
"timshome.page/gilo/internal/terminal"
"timshome.page/gilo/editor"
"timshome.page/gilo/terminal"
)
func main() {

View File

@ -1,4 +1,4 @@
package ansi
package terminal
import "fmt"

23
terminal/io.go Normal file
View File

@ -0,0 +1,23 @@
package terminal
import (
"bufio"
"fmt"
"os"
)
var reader = bufio.NewReader(os.Stdin)
func ReadKey() (rune, int) {
ch, size, err := reader.ReadRune()
if err != nil {
panic(err)
}
return ch, size
}
// Print string to stdout
func Write(s string) {
fmt.Print(s)
}

34
terminal/raw.go Normal file
View File

@ -0,0 +1,34 @@
package terminal
import (
"os"
"golang.org/x/term"
)
// Put the terminal in raw mode
func RawOn() *term.State {
check()
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
return oldState
}
// Restore the terminal to canonical mode
func RawOff(oldState *term.State) {
err := term.Restore(int(os.Stdin.Fd()), oldState)
if err != nil {
panic(err)
}
}
// Is this a valid interactive terminal?
func check() {
if !term.IsTerminal(int(os.Stdin.Fd())) {
panic("An interactive terminal is required to use a text editor!")
}
}

View File

@ -1,49 +1,28 @@
package terminal
import (
"bufio"
"fmt"
"os"
"golang.org/x/term"
"os"
)
var reader = bufio.NewReader(os.Stdin)
func ReadKey() (rune, int) {
ch, size, err := reader.ReadRune()
if err != nil {
panic(err)
}
return ch, size
}
// Is this a valid interactive terminal?
func check() {
if !term.IsTerminal(int(os.Stdin.Fd())) {
panic("An interactive terminal is required to use a text editor!")
}
}
// Put the terminal in raw mode
func RawOn() *term.State {
// Get the size of the terminal in rows and columns
func Size () (rows int, cols int) {
check()
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
cols = 80
rows = 24
// Try the syscall first
cols, rows, err := term.GetSize(int(os.Stdin.Fd()))
if err == nil {
return rows, cols
}
return oldState
}
// Figure out the size the hard way
rows, cols = sizeTrick()
// Restore the terminal to canonical mode
func RawOff(oldState *term.State) {
err := term.Restore(int(os.Stdin.Fd()), oldState)
if err != nil {
panic(err)
}
return rows, cols
}
func sizeTrick () (rows int, cols int) {
@ -75,26 +54,4 @@ func sizeTrick () (rows int, cols int) {
}
return rows, cols
}
// Get the size of the terminal in rows and columns
func Size () (rows int, cols int) {
cols = 80
rows = 24
// Try the syscall first
cols, rows, err := term.GetSize(int(os.Stdin.Fd()))
if err == nil {
return rows, cols
}
// Figure out the size the hard way
rows, cols = sizeTrick()
return rows, cols
}
// Print string to stdout
func Write(s string) {
fmt.Print(s)
}
}