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
374}374}
375 375
376static void append_terminal_line(struct mp_log *log, int lev,376static 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)
378{378{
379 struct mp_log_root *root = log->root;379 struct mp_log_root *root = log->root;
380 380
@@ -394,8 +394,35 @@ static void append_terminal_line(struct mp_log *log, int lev, · 13 unmodified lines
394 }394 }
395 395
396 bstr_xappend(root, term_msg, text);396 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 }
397 *line_w = root->isatty[term_msg_fileno(root, lev)]424 *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;
399}426}
400 427
401static struct mp_log_buffer_entry *log_buffer_read(struct mp_log_buffer *buffer)428static 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
496 523
497 if (print_term) {524 if (print_term) {
498 int line_w;525 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);
500 term_msg_lines += (!line_w || !term_w)528 term_msg_lines += (!line_w || !term_w)
501 ? 1 : (line_w + term_w - 1) / term_w;529 ? 1 : (line_w + term_w - 1) / term_w;
502 }530 }
@@ -506,7 +534,8 @@ static void write_term_msg(struct mp_log *log, int lev, bstr text, bstr *out) · 3 unmodified lines
506 if (lev == MSGL_STATUS) {534 if (lev == MSGL_STATUS) {
507 int line_w = 0;535 int line_w = 0;
508 if (str.len && print_term)536 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);
510 term_msg_lines += !term_w ? (str.len ? 1 : 0)539 term_msg_lines += !term_w ? (str.len ? 1 : 0)
511 : (line_w + term_w - 1) / term_w;540 : (line_w + term_w - 1) / term_w;
512 } else if (str.len) {541 } else if (str.len) {
common/msg.h+2−0
@@ -25,6 +25,8 @@ · 24 unmodified lines
25 25
26#include "osdep/compiler.h"26#include "osdep/compiler.h"
27 27
28#define TERM_MSG_0 "\xFC"
29
28struct mp_log;30struct mp_log;
29 31
30// A mp_log instance that never outputs anything.32// 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