Move functions to file under editor instead of their own package

This commit is contained in:
Timothy Warren 2021-03-30 16:22:35 -04:00
parent 842ab0ef49
commit f7157f9c50
5 changed files with 30 additions and 31 deletions

View File

@ -4,7 +4,6 @@ package editor
import (
"fmt"
"strings"
"timshome.page/gilo/fn"
"timshome.page/gilo/terminal"
)
@ -31,7 +30,7 @@ func (e *editor) drawRows(ab *buffer) {
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)
welcome = TruncateString(welcome, e.screen.Cols)
}
padding := (e.screen.Cols - len(welcome)) / 2

View File

@ -1,7 +1,6 @@
package editor
import (
"timshome.page/gilo/fn"
"timshome.page/gilo/terminal"
)
@ -24,7 +23,6 @@ func New() *editor {
screen := terminal.Size()
cursor := &cursor { 0, 0 }
var rows []*row
rows = append(rows, NewRow())
return &editor{screen, cursor, rows }
}
@ -33,7 +31,7 @@ func (e *editor) ProcessKeypress() bool {
var str string
ch, _ := terminal.ReadKey()
if ch == fn.Ctrl('q') {
if ch == Ctrl('q') {
// Clean up on exit
terminal.Write(terminal.ClearScreen + terminal.ResetCursor)
@ -99,6 +97,7 @@ func parseEscapeSequence () string {
runes = append(runes, ch)
if i == 1 && runes[1] >= 'A' {
// \eOH \eOF
if runes[0] == 'O' {
switch runes[1] {
case 'H':
@ -107,6 +106,7 @@ func parseEscapeSequence () string {
return keyEnd
}
}
// \e[A
if runes[0] == '[' {
switch runes[1] {
case 'A':
@ -125,6 +125,7 @@ func parseEscapeSequence () string {
}
}
// \e[1~
if i == 2 && runes[0] == '[' && runes[2] == '~' {
switch runes[1] {
case '1':

View File

@ -1,4 +1,26 @@
package fn
package editor
import "strings"
func TruncateString(s string, length int) string {
if length < 1 {
return ""
}
var buf strings.Builder
count := 0
for _, char := range s {
if count == length {
break
}
buf.WriteRune(char)
count++
}
return buf.String()
}
func isAscii(char rune) bool {
ord := int(char)

View File

@ -14,7 +14,7 @@ const(
)
// ----------------------------------------------------------------------------
// !Map escape sequences to simpler constants
// !Constants representing input keys
// ----------------------------------------------------------------------------
const (

View File

@ -1,23 +0,0 @@
package fn
import "strings"
func TruncateString(s string, length int) string {
if length < 1 {
return ""
}
var buf strings.Builder
count := 0
for _, char := range s {
if count == length {
break
}
buf.WriteRune(char)
count++
}
return buf.String()
}