Skip to content

Commit

Permalink
Improve stability of request editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 22, 2023
1 parent 79a70c3 commit cb835ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
25 changes: 10 additions & 15 deletions pkg/cli/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,18 @@ func (c *Content) RemoveSymbol() string {
}

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

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

// Move cursor back to position
for i := 1; i < len(lines); i++ {
output += LineUp
if moveUp != "" {
output += moveUp
output += "\r" + lines[0]
}

output += "\r" + lines[0]

return output
}

Expand Down Expand Up @@ -159,20 +159,19 @@ func (c *Content) InsertSymbol(symbol rune) string {
}

output := ""

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

// Move cursor back to position
for i := 2; i < len(lines); i++ {
output += LineUp
moveUp := ""
for i := 1; i < len(lines)-1; i++ {
moveUp += LineUp
}

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

return output
}
Expand All @@ -189,11 +188,7 @@ func (c *Content) MoveToEnd() string {
}

func (c *Content) GetLinesAfterPosition(pos int) (startOfLine int, lines []string) {
if pos < 0 || pos > len(c.text) {
return 0, []string{}
}

startOfLine = lastIndexOf(c.text, pos, '\t')
startOfLine = lastIndexOf(c.text, pos-1, '\n')
if startOfLine == -1 {
startOfLine = 0
} else {
Expand Down
13 changes: 10 additions & 3 deletions pkg/cli/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestContent_RemoveSymbol(t *testing.T) {
name: "remove newline symbol at the end of the text",
input: "hello\nworld\n",
pos: 12,
output: "\x1b[1A\x1b[2K\rhelloworld\n\x1b[2K\r\n\x1b[2K\r\x1b[1A\x1b[1A\rhello",
output: "\x1b[1A\x1b[2K\rworld\n\x1b[2K\r\x1b[1A\rworld",
contentAfter: "hello\nworld",
},
{
Expand Down Expand Up @@ -398,8 +398,6 @@ func TestContent_InsertSymbol(t *testing.T) {
posAfter: 12,
},
{
// This test is not really correct, but it is how it works now
// TODO: fix this test
name: "insert newline at the begginning",
input: "hello\nworld",
symbol: '\n',
Expand All @@ -408,6 +406,15 @@ func TestContent_InsertSymbol(t *testing.T) {
contentAfter: "\nhello\nworld",
posAfter: 1,
},
{
name: "insert newline in a row",
input: "h\n\nello\nworld",
symbol: '\n',
pos: 2,
output: "\x1b[2K\r\n\x1b[2K\r\n\x1b[2K\rello\n\x1b[2K\rworld\x1b[1A\x1b[1A\r",
contentAfter: "h\n\n\nello\nworld",
posAfter: 3,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit cb835ac

Please sign in to comment.