Skip to content

Commit

Permalink
Step23&24: Reposition the cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
forfd8960 committed Mar 16, 2024
1 parent 3d7207a commit 4c08d56
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 1 addition & 3 deletions doc/step22.MD
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
## Refresh Sceen

```
Escape sequences always start with an escape character (27) followed by a [ character. Escape sequences instruct the terminal to do various text formatting tasks, such as coloring text, moving the cursor around, and clearing parts of the screen.
```
Escape sequences always start with an escape character `(27)` followed by a `[` character. **Escape sequences instruct the terminal to do various text formatting tasks, such as coloring text, moving the cursor around, and clearing parts of the screen.**

In C, `\x1b[2J` is an escape sequence that is used to clear the screen in a terminal or console window. This escape sequence is a combination of special characters that, when printed to the terminal, instruct the terminal to clear the screen.

13 changes: 13 additions & 0 deletions doc/step23.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Move cursure to top-left position

```
The H command actually takes two arguments: the row number and the column number at which to position the cursor.
```

- `\x1b[H`

The H command actually takes two arguments: the row number and the column number at which to position the cursor.

The default arguments for H both happen to be 1,
so we can leave both arguments out and it will position the cursor at the first row and first column,
as if we had sent the `<esc>[1;1H` command
8 changes: 8 additions & 0 deletions doc/step24.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Clear and reposition the cursor

```sh
tcgetattr: Inappropriate ioctl for device

~ main*
```
5 changes: 5 additions & 0 deletions kilo.c
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ struct termios orig_termios;

void printAndExit(const char *s)
{
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
perror(s);
exit(1);
}
@@ -65,6 +67,7 @@ char editorReadKey(void)
void editorRefreshSceen(void)
{
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
}

/*** input ***/
@@ -76,6 +79,8 @@ void editorProcessKeypress(void)
switch (c)
{
case CTRL_KEY('q'):
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
exit(0);
break;
}

0 comments on commit 4c08d56

Please sign in to comment.