commit 381ee7cf29042a9e629a1e7161f9f6a605774b68
parent 8c1bb48b9d7939d8e4552928ac958b3749512b47
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Tue, 18 Apr 2023 21:42:17 +0200
Relative numbers and current line left align
Diffstat:
M | kilo.c | | | 27 | ++++++++++++++++++++------- |
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/kilo.c b/kilo.c
@@ -52,6 +52,9 @@ int numberOfDigits(int n) {
#define KILO_TAP_SOFT 1
#define KILO_QUIT_TIMES 3
+#define KILO_NUM_RELATIVE 1
+#define KILO_MARGIN_LEFT 1
+
#define CTRL_KEY(k) ((k)&0x1f)
enum editorKey {
@@ -1012,7 +1015,8 @@ void editorScroll() {
}
void editorDrawRows(struct abuf *ab) {
- char num_buf[E.row_digits + 1];
+ int margin_size = E.row_digits + KILO_MARGIN_LEFT;
+ char margin_buf[margin_size + 1];
char color_buf[16];
for (int y = 0; y < E.screenrows; y++) {
@@ -1034,13 +1038,22 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1);
}
} else {
- int padding = sprintf(num_buf, "%*d ", E.row_digits, filerow + 1);
- int clen =
- snprintf(color_buf, sizeof(color_buf), "\x1b[%dm",
- editorSyntaxToColor(E.cy == y ? HL_LINEN_CRNT : HL_LINEN));
+ int clen, padding;
+
+ if (E.cy - E.rowoff == y) {
+ padding = sprintf(margin_buf, "%d%*s", filerow + 1,
+ margin_size - numberOfDigits(y + 1) + 1, "");
+ clen = snprintf(color_buf, sizeof(color_buf), "\x1b[%dm",
+ editorSyntaxToColor(HL_LINEN_CRNT));
+ } else {
+ int row = KILO_NUM_RELATIVE ? abs(y - (E.cy - E.rowoff)) : filerow + 1;
+ padding = sprintf(margin_buf, "%*d ", margin_size, row);
+ clen = snprintf(color_buf, sizeof(color_buf), "\x1b[%dm",
+ editorSyntaxToColor(HL_LINEN));
+ }
abAppend(ab, color_buf, clen);
- abAppend(ab, num_buf, padding);
+ abAppend(ab, margin_buf, padding);
abAppend(ab, "\x1b[39m", 5);
int len = E.row[filerow].rsize - E.coloff;
@@ -1132,7 +1145,7 @@ void editorRefreshScreen() {
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1,
- (E.rx - E.coloff) + E.row_digits + 2);
+ (E.rx - E.coloff) + E.row_digits + 2 + KILO_MARGIN_LEFT);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6); // show cursor