Skip to content

Commit

Permalink
Move hardcoded to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 22, 2023
1 parent cb835ac commit 229df19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
49 changes: 26 additions & 23 deletions pkg/cli/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
)

const (
LineUp = "\x1b[1A"
LineClear = "\x1b[2K"
LineUp = "\x1b[1A"
LineClear = "\x1b[2K"
NewLine = '\n'
ReturnCarriege = "\r"
Backspace = "\b"
)

type Content struct {
Expand Down Expand Up @@ -43,11 +46,11 @@ func (c *Content) MovePositionLeft() string {
}

c.pos--
if c.text[c.pos] != '\n' {
return "\b"
if c.text[c.pos] != NewLine {
return Backspace
}

startPrevLine := lastIndexOf(c.text, c.pos-1, '\n')
startPrevLine := lastIndexOf(c.text, c.pos-1, NewLine)
if startPrevLine == -1 {
startPrevLine = 0
} else {
Expand All @@ -73,11 +76,11 @@ func (c *Content) Clear() string {
}

output := c.MoveToEnd()
output += LineClear + "\r"
output += LineClear + ReturnCarriege

for i := 0; i < len(c.text); i++ {
if c.text[i] == '\n' {
output += LineUp + LineClear + "\r"
if c.text[i] == NewLine {
output += LineUp + LineClear + ReturnCarriege
}
}

Expand All @@ -102,26 +105,26 @@ func (c *Content) RemoveSymbol() string {

c.text = buffer

if c.pos == len(c.text) && symbol != '\n' {
return "\b \b"
if c.pos == len(c.text) && symbol != NewLine {
return Backspace + " " + Backspace
}

if symbol != '\n' {
if symbol != NewLine {
endCurrentLine := startCurrentLine + len(lines[0])
return LineClear + "\r" + string(c.text[startCurrentLine:endCurrentLine-1]) + "\r" + string(c.text[startCurrentLine:c.pos])
return LineClear + ReturnCarriege + string(c.text[startCurrentLine:endCurrentLine-1]) + ReturnCarriege + string(c.text[startCurrentLine:c.pos])
}

output := LineUp + LineClear + "\r" + lines[0]
output := LineUp + LineClear + ReturnCarriege + lines[0]
moveUp := ""

for i := 1; i < len(lines); i++ {
output += lines[i] + "\n" + LineClear + "\r"
output += lines[i] + string(NewLine) + LineClear + ReturnCarriege
moveUp += LineUp
}

if moveUp != "" {
output += moveUp
output += "\r" + lines[0]
output += ReturnCarriege + lines[0]
}

return output
Expand All @@ -146,23 +149,23 @@ func (c *Content) InsertSymbol(symbol rune) string {
c.pos++
c.text = buffer

if symbol != '\n' && c.text[c.pos] == '\n' {
if symbol != NewLine && c.text[c.pos] == NewLine {
return string(symbol)
}

startCurrentLine, lines := c.GetLinesAfterPosition(c.pos - 1)

if symbol != '\n' {
if symbol != NewLine {
// here probably i have a room for optimization
endCurrentLine := startCurrentLine + len(lines[0])
return LineClear + "\r" + string(c.text[startCurrentLine:endCurrentLine]) + "\r" + string(c.text[startCurrentLine:c.pos])
return LineClear + ReturnCarriege + string(c.text[startCurrentLine:endCurrentLine]) + ReturnCarriege + string(c.text[startCurrentLine:c.pos])
}

output := ""
for i := 0; i < len(lines); i++ {
output += LineClear + "\r" + lines[i]
output += LineClear + ReturnCarriege + lines[i]
if i < len(lines)-1 {
output += "\n"
output += string(NewLine)
}
}

Expand All @@ -171,7 +174,7 @@ func (c *Content) InsertSymbol(symbol rune) string {
moveUp += LineUp
}

output += moveUp + "\r"
output += moveUp + ReturnCarriege

return output
}
Expand All @@ -188,14 +191,14 @@ func (c *Content) MoveToEnd() string {
}

func (c *Content) GetLinesAfterPosition(pos int) (startOfLine int, lines []string) {
startOfLine = lastIndexOf(c.text, pos-1, '\n')
startOfLine = lastIndexOf(c.text, pos-1, NewLine)
if startOfLine == -1 {
startOfLine = 0
} else {
startOfLine++
}

return startOfLine, strings.Split(string(c.text[startOfLine:]), "\n")
return startOfLine, strings.Split(string(c.text[startOfLine:]), string(NewLine))
}

func lastIndexOf(buffer []rune, pos int, search rune) int {
Expand Down
1 change: 0 additions & 1 deletion pkg/cli/content_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// BEGIN: yz9d8f7g6h5j
package cli

import (
Expand Down

0 comments on commit 229df19

Please sign in to comment.