aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar BanceDev 2024-09-09 11:08:34 -0400
committerGravatar BanceDev 2024-09-09 11:08:34 -0400
commit73d9858203e29816d60a3a7f2768f76cf260235c (patch)
tree189f1f2bff6f43908aff225ea95073996a9e3e90 /src
parentMerge pull request #1 from Makaze/patch-1 (diff)
added stripping of escape sequences from prompt size to allow for coloring
Diffstat (limited to '')
-rw-r--r--src/lush.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/lush.c b/src/lush.c
index 8e5bb6f..e5571f2 100644
--- a/src/lush.c
+++ b/src/lush.c
@@ -260,12 +260,33 @@ static char *get_prompt() {
free(cwd);
return prompt;
}
+size_t get_stripped_length(const char *str) {
+ size_t len = 0;
+ size_t i = 0;
+
+ while (str[i] != '\0') {
+ if (str[i] == '\033' && str[i + 1] == '[') {
+ // Skip over the escape sequence
+ while (str[i] != 'm' && str[i] != '\0') {
+ i++;
+ }
+ // Include 'm' character to end the escape sequence
+ if (str[i] == 'm') {
+ i++;
+ }
+ } else {
+ len++;
+ i++;
+ }
+ }
+ return len;
+}
static void reprint_buffer(char *buffer, int *last_lines, int *pos,
int history_pos) {
char *prompt = get_prompt();
int width = get_terminal_width();
-
+ size_t prompt_length = get_stripped_length(prompt);
// handle history before doing calculations
if (history_pos >= 0) {
char *history_line = lush_get_past_command(history_pos);
@@ -278,14 +299,14 @@ static void reprint_buffer(char *buffer, int *last_lines, int *pos,
}
}
- int num_lines = ((strlen(buffer) + strlen(prompt) + 1) / width) + 1;
- int cursor_pos = (strlen(prompt) + *pos + 1) % width;
- int cursor_line = (strlen(prompt) + *pos + 1) / width + 1;
+ int num_lines = ((strlen(buffer) + prompt_length + 1) / width) + 1;
+ int cursor_pos = (prompt_length + *pos + 1) % width;
+ int cursor_line = (prompt_length + *pos + 1) / width + 1;
// move cursor down if it is up a number of lines first
if (num_lines - cursor_line > 0) {
printf("\033[%dB", num_lines - cursor_line);
// compensate for if the we have just filled a line
- if ((strlen(buffer) + strlen(prompt) + 1) % width == 0) {
+ if ((strlen(buffer) + prompt_length + 1) % width == 0) {
printf("\033[A");
}
}