History

msg: allow to truncate the message to terminal width

Kacper Michajłow committed 2 years ago

Commit bf025cd1 parent 95f00469 files · +106 −15Browse files

Showing the changes to common (2 of 9 files). Show all

Unified · Split

2 files
  1. common/msg.c +33 −4
  2. common/msg.h +2 −0
common/msg.c+33−4
@@ -374,7 +374,7 @@ static bool test_terminal_level(struct mp_log *log, int lev) · 373 unmodified lines
374374 }
375375
376376 static void append_terminal_line(struct mp_log *log, int lev,
377− bstr text, bstr *term_msg, int *line_w)
377+ bstr text, bstr *term_msg, int *line_w, int term_w)
378378 {
379379 struct mp_log_root *root = log->root;
380380
@@ -394,8 +394,35 @@ static void append_terminal_line(struct mp_log *log, int lev, · 13 unmodified lines
394394 }
395395
396396 bstr_xappend(root, term_msg, text);
397+
398+ const unsigned char *cut_pos = NULL;
399+ int width = term_disp_width(bstr_splice(*term_msg, start, term_msg->len),
400+ term_w - 3, &cut_pos);
401+ if (cut_pos) {
402+ int new_len = cut_pos - term_msg->start;
403+ bstr rem = {(unsigned char *)cut_pos, term_msg->len - new_len};
404+ term_msg->len = new_len;
405+
406+ bstr_xappend(root, term_msg, bstr0("..."));
407+
408+ while (rem.len) {
409+ if (bstr_eatstart0(&rem, "\033[")) {
410+ bstr_xappend(root, term_msg, bstr0("\033["));
411+
412+ while (rem.len && !((*rem.start >= '@' && *rem.start <= '~') || *rem.start == 'm')) {
413+ bstr_xappend(root, term_msg, bstr_splice(rem, 0, 1));
414+ rem = bstr_cut(rem, 1);
415+ }
416+ bstr_xappend(root, term_msg, bstr_splice(rem, 0, 1));
417+ }
418+ rem = bstr_cut(rem, 1);
419+ }
420+
421+ bstr_xappend(root, term_msg, bstr0("\n"));
422+ width += 3;
423+ }
397424 *line_w = root->isatty[term_msg_fileno(root, lev)]
398− ? term_disp_width(bstr_splice(*term_msg, start, term_msg->len)) : 0;
425+ ? width : 0;
399426 }
400427
401428 static struct mp_log_buffer_entry *log_buffer_read(struct mp_log_buffer *buffer)
@@ -496,7 +523,8 @@ static void write_term_msg(struct mp_log *log, int lev, bstr text, bstr *out) · 94 unmodified lines
496523
497524 if (print_term) {
498525 int line_w;
499− append_terminal_line(log, lev, line, &root->term_msg_tmp, &line_w);
526+ append_terminal_line(log, lev, line, &root->term_msg_tmp, &line_w,
527+ bstr_eatstart0(&line, TERM_MSG_0) ? term_w : INT_MAX);
500528 term_msg_lines += (!line_w || !term_w)
501529 ? 1 : (line_w + term_w - 1) / term_w;
502530 }
@@ -506,7 +534,8 @@ static void write_term_msg(struct mp_log *log, int lev, bstr text, bstr *out) · 3 unmodified lines
506534 if (lev == MSGL_STATUS) {
507535 int line_w = 0;
508536 if (str.len && print_term)
509− append_terminal_line(log, lev, str, &root->term_msg_tmp, &line_w);
537+ append_terminal_line(log, lev, str, &root->term_msg_tmp, &line_w,
538+ bstr_eatstart0(&str, TERM_MSG_0) ? term_w : INT_MAX);
510539 term_msg_lines += !term_w ? (str.len ? 1 : 0)
511540 : (line_w + term_w - 1) / term_w;
512541 } else if (str.len) {
common/msg.h+2−0
@@ -25,6 +25,8 @@ · 24 unmodified lines
2525
2626 #include "osdep/compiler.h"
2727
28+#define TERM_MSG_0 "\xFC"
29+
2830 struct mp_log;
2931
3032 // A mp_log instance that never outputs anything.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t