Put Editor behind a constructor function
This commit is contained in:
parent
0902d494c5
commit
63cad6d012
6
gilo.go
6
gilo.go
@ -10,11 +10,13 @@ func main() {
|
|||||||
oldState := terminal.RawOn()
|
oldState := terminal.RawOn()
|
||||||
defer terminal.RawOff(oldState)
|
defer terminal.RawOff(oldState)
|
||||||
|
|
||||||
|
e := editor.New()
|
||||||
|
|
||||||
// The input loop
|
// The input loop
|
||||||
for {
|
for {
|
||||||
editor.RefreshScreen()
|
e.RefreshScreen()
|
||||||
|
|
||||||
if editor.ProcessKeypress() == false {
|
if e.ProcessKeypress() == false {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,29 +5,31 @@ import (
|
|||||||
"timshome.page/gilo/internal/terminal"
|
"timshome.page/gilo/internal/terminal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Editor struct {
|
type editor struct {
|
||||||
rows int
|
rows int
|
||||||
cols int
|
cols int
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawRows() {
|
func New() *editor {
|
||||||
_, rows := terminal.Size()
|
cols, rows := terminal.Size()
|
||||||
|
|
||||||
for y :=0; y < rows; y += 1 {
|
e := new(editor)
|
||||||
terminal.OutLn("~")
|
e.cols = cols
|
||||||
}
|
e.rows = rows
|
||||||
|
|
||||||
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func RefreshScreen() {
|
func (e *editor) RefreshScreen() {
|
||||||
terminal.ANSICode(terminal.ClearScreen)
|
terminal.ANSICode(terminal.ClearScreen)
|
||||||
terminal.ANSICode(terminal.ResetCursor)
|
terminal.ANSICode(terminal.ResetCursor)
|
||||||
|
|
||||||
drawRows()
|
e.drawRows()
|
||||||
|
|
||||||
terminal.ANSICode(terminal.ResetCursor)
|
terminal.ANSICode(terminal.ResetCursor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessKeypress() bool {
|
func (e *editor) ProcessKeypress() bool {
|
||||||
ch, _ := terminal.ReadKey()
|
ch, _ := terminal.ReadKey()
|
||||||
|
|
||||||
// Clean up on exit
|
// Clean up on exit
|
||||||
@ -41,10 +43,20 @@ func ProcessKeypress() bool {
|
|||||||
// Ugliest syntax structure ever?
|
// Ugliest syntax structure ever?
|
||||||
switch {
|
switch {
|
||||||
case char.IsCtrl(ch):
|
case char.IsCtrl(ch):
|
||||||
terminal.OutLn("%d", ch)
|
terminal.WriteLn("%d", ch)
|
||||||
default:
|
default:
|
||||||
terminal.OutLn("%d ('%c')", ch, ch)
|
terminal.WriteLn("%d ('%c')", ch, ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *editor) drawRows() {
|
||||||
|
for y :=0; y < e.rows; y += 1 {
|
||||||
|
terminal.Write("~")
|
||||||
|
|
||||||
|
if y < (e.rows - 1) {
|
||||||
|
terminal.Write("\r\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -97,14 +97,19 @@ func Size () (width int, height int) {
|
|||||||
// Figure out the size the hard way
|
// Figure out the size the hard way
|
||||||
height, width = sizeTrick()
|
height, width = sizeTrick()
|
||||||
|
|
||||||
OutLn("%d Rows, %d Cols", height, width)
|
WriteLn("%d Rows, %d Cols", height, width)
|
||||||
|
|
||||||
return width, height
|
return width, height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Print a formatted string to stdout, with CRLF line endings for proper terminal formatting
|
// Print a formatted string to stdout, with CRLF line endings for proper terminal formatting
|
||||||
func OutLn(format string, a ...interface{}) {
|
func WriteLn(format string, a ...interface{}) {
|
||||||
formatted := fmt.Sprintf(format, a...)
|
formatted := fmt.Sprintf(format, a...)
|
||||||
|
|
||||||
fmt.Printf("%s\r\n", formatted)
|
fmt.Printf("%s\r\n", formatted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Write(format string, a ...interface{}) {
|
||||||
|
fmt.Printf(format, a...)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user