Skip to content

Commit d2448ea

Browse files
committed
feat: home, end and delete
1 parent 455d713 commit d2448ea

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

lib/src/embedded_cli.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,29 @@ static void onEscapedInput(EmbeddedCli *cli, char c) {
760760
impl->cursorPos++;
761761
writeToOutput(cli, escSeqCursorLeft);
762762
}
763+
764+
// Home
765+
if (c == 'H' || (c == '~' && (impl->lastChar == '1' || impl->lastChar == '7'))) {
766+
if (impl->cursorPos < impl->cmdSize) {
767+
moveCursor(cli, impl->cmdSize - impl->cursorPos, CURSOR_DIRECTION_BACKWARD);
768+
impl->cursorPos = impl->cmdSize;
769+
}
770+
}
771+
// End
772+
if (c == 'F' || (c == '~' && (impl->lastChar == '4' || impl->lastChar == '8'))) {
773+
if (impl->cursorPos > 0) {
774+
moveCursor(cli, impl->cursorPos, CURSOR_DIRECTION_FORWARD);
775+
impl->cursorPos = 0;
776+
}
777+
}
778+
// Delete
779+
if (c == '~' && impl->lastChar == '3' && impl->cursorPos != 0) {
780+
size_t insertPos = strlen(impl->cmdBuffer) - impl->cursorPos;
781+
memmove(&impl->cmdBuffer[insertPos], &impl->cmdBuffer[insertPos + 1], impl->cursorPos);
782+
--impl->cmdSize;
783+
--impl->cursorPos;
784+
writeToOutput(cli, escSeqDeleteChar);
785+
}
763786
}
764787
}
765788

tests/cli/BaseTest.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,89 @@ TEST_CASE("CLI. Base tests", "[cli]") {
157157
REQUIRE(displayed.cursorColumn == 7);
158158
}
159159

160+
SECTION("Move cursor to start") {
161+
cli.send(" both\x1B[Hget");
162+
cli.process();
163+
auto displayed = cli.getDisplay();
164+
REQUIRE(displayed.lines.size() == 1);
165+
REQUIRE(displayed.lines[0] == "> get both");
166+
REQUIRE(displayed.cursorColumn == 5);
167+
}
168+
169+
SECTION("Move cursor to start with 1~ alternative sequence") {
170+
cli.send(" both\x1B[1~get");
171+
cli.process();
172+
auto displayed = cli.getDisplay();
173+
REQUIRE(displayed.lines.size() == 1);
174+
REQUIRE(displayed.lines[0] == "> get both");
175+
REQUIRE(displayed.cursorColumn == 5);
176+
}
177+
178+
SECTION("Move cursor to start with 7~ alternative sequence") {
179+
cli.send(" both\x1B[7~get");
180+
cli.process();
181+
auto displayed = cli.getDisplay();
182+
REQUIRE(displayed.lines.size() == 1);
183+
REQUIRE(displayed.lines[0] == "> get both");
184+
REQUIRE(displayed.cursorColumn == 5);
185+
}
186+
187+
SECTION("Move cursor to start then end") {
188+
cli.send("both\x1B[Hget \x1B[F");
189+
cli.process();
190+
auto displayed = cli.getDisplay();
191+
REQUIRE(displayed.lines.size() == 1);
192+
REQUIRE(displayed.lines[0] == "> get both");
193+
REQUIRE(displayed.cursorColumn == 10);
194+
}
195+
196+
SECTION("Move cursor to start then end with 4~ alternative sequence") {
197+
cli.send("both\x1B[Hget \x1B[4~");
198+
cli.process();
199+
auto displayed = cli.getDisplay();
200+
REQUIRE(displayed.lines.size() == 1);
201+
REQUIRE(displayed.lines[0] == "> get both");
202+
REQUIRE(displayed.cursorColumn == 10);
203+
}
204+
205+
206+
SECTION("Move cursor to start then end with 8~ alternative sequence") {
207+
cli.send("both\x1B[Hget \x1B[8~");
208+
cli.process();
209+
auto displayed = cli.getDisplay();
210+
REQUIRE(displayed.lines.size() == 1);
211+
REQUIRE(displayed.lines[0] == "> get both");
212+
REQUIRE(displayed.cursorColumn == 10);
213+
}
214+
215+
SECTION("Move cursor back and perform Delete") {
216+
cli.send("get baoth\x1B[D\x1B[D\x1B[D\x1B\x1B[D\x1B[3~");
217+
cli.process();
218+
auto displayed = cli.getDisplay();
219+
REQUIRE(displayed.lines.size() == 1);
220+
REQUIRE(displayed.lines[0] == "> get both");
221+
REQUIRE(displayed.cursorColumn == 7);
222+
}
223+
224+
SECTION("Delete at end of section") {
225+
cli.sendLine("set example\x1B[3~");
226+
cli.process();
227+
228+
auto lines = cli.getDisplay().lines;
229+
230+
REQUIRE(lines[0] == "> set example");
231+
232+
}
233+
234+
SECTION("Delete at start of line should work") {
235+
cli.sendLine("nset\x1B[D\x1B[D\x1B[D\x1B[D\x1B[3~");
236+
cli.process();
237+
238+
auto lines = cli.getDisplay().lines;
239+
240+
REQUIRE(lines[0] == "> set");
241+
}
242+
160243
SECTION("Command that is too long") {
161244
size_t cmdMax = embeddedCliDefaultConfig()->cmdBufferSize;
162245
std::string cmdMaxTest = std::string(cmdMax/2, 'x');

0 commit comments

Comments
 (0)