display non-printable characters with carot notation

This commit is contained in:
anon 2024-08-07 19:30:54 +02:00
parent 35ca08fc94
commit 117cfe0431
2 changed files with 37 additions and 1 deletions

29
source/caret_notater.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef CARET_NOTATER
#define CARET_NOTATER
static inline
int is_caret(char c) {
return (0 <= c && c <= 31);
}
static inline
char to_caret_char(char c) {
return c + '@';
}
char * string_to_caret_notation(const char * input, int size, char * output) {
int output_empty_end = 0;
for (int i = 0; i < size; i++) {
if (is_caret(input[i])) {
output[output_empty_end++] = '^';
output[output_empty_end++] = to_caret_char(input[i]);
} else {
output[output_empty_end++] = input[i];
}
}
output[output_empty_end] = '\0';
return output;
}
#endif

View File

@ -4,6 +4,8 @@
#include <ncurses.h>
#include <readline/readline.h>
#include "caret_notater.h"
extern bool do_execute;
size_t entry_lines;
@ -91,6 +93,8 @@ void tui_append_back(const entry_t entry) {
const int TIME_SIZE = 20;
char time_buffer[TIME_SIZE];
strftime(time_buffer, TIME_SIZE, "%Y-%m-%d %H:%M:%S", tm_info);
const size_t entry_len = strlen(entry.command);
char caret_notation_buffer[entry_len*2];
if (entry_line_index == selection_relative) {
wattron(entry_window, A_REVERSE);
@ -99,7 +103,10 @@ void tui_append_back(const entry_t entry) {
"%s %.*s",
time_buffer,
COLS-2-(TIME_SIZE-1)-2, // XXX: this is horrible
entry.command
string_to_caret_notation(entry.command,
entry_len,
caret_notation_buffer
)
);
if (entry_line_index == selection_relative) {
wattroff(entry_window, A_REVERSE);