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 misc (2 of 9 files). Show all

Unified · Split

2 files
  1. misc/codepoint_width.c +27 −9
  2. misc/codepoint_width.h +4 −1
misc/codepoint_width.c+27−9
@@ -662,20 +662,26 @@ static int ucdToCharacterWidth(const int val) · 661 unmodified lines
662662 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
663663 */
664664
665−int term_disp_width(bstr str)
665+#include "common/common.h"
666+
667+int term_disp_width(bstr str, int max_width, const unsigned char **cut_pos)
666668 {
667669 static const int ambiguous_width = 1;
668670
669671 int width = 0;
670672
673+ const unsigned char *prev_pos = str.start;
671674 while (str.len) {
675+ int current_width = 0;
676+
672677 if (bstr_eatstart0(&str, "\033[")) {
673678 while (str.len && !((*str.start >= '@' && *str.start <= '~') || *str.start == 'm'))
674679 str = bstr_cut(str, 1);
675680 str = bstr_cut(str, 1);
676− continue;
681+ goto next;
677682 }
678683
684+ prev_pos = str.start;
679685 int cp = bstr_decode_utf8(str, &str);
680686
681687 // Stop processing on any invalid input
@@ -684,18 +690,17 @@ int term_disp_width(bstr str) · 2 unmodified lines
684690
685691 if (cp == '\r') {
686692 width = 0;
687− continue;
693+ goto next;
688694 }
689695
690696 if (cp < 0x20)
691− continue;
697+ goto next;
692698
693699 if (cp <= 0x7E) {
694− width++;
695− continue;
700+ current_width = 1;
701+ goto next;
696702 }
697703
698− int grapheme_width = 0;
699704 int state = 0;
700705
701706 while (true) {
@@ -711,7 +716,7 @@ int term_disp_width(bstr str) · 9 unmodified lines
711716 if (cp == 0xFE0F)
712717 w = 2;
713718
714− grapheme_width += w;
719+ current_width += w;
715720
716721 if (!str.len)
717722 break;
@@ -730,7 +735,20 @@ int term_disp_width(bstr str) · 12 unmodified lines
730735
731736 str = cluster_end;
732737 }
733− width += grapheme_width > 2 ? 2 : grapheme_width;
738+
739+next:
740+ current_width = MPMIN(current_width, 2);
741+ if (width + current_width > max_width) {
742+ assert(prev_pos < str.start + str.len);
743+ *cut_pos = prev_pos;
744+ break;
745+ }
746+ width += current_width;
747+ if (width == max_width) {
748+ if (str.len)
749+ *cut_pos = str.start;
750+ break;
751+ }
734752 }
735753
736754 return width;
misc/codepoint_width.h+4−1
@@ -6,6 +6,9 @@ · 5 unmodified lines
66 * @brief Determines the number of columns required to display a given string.
77 *
88 * @param str Sequence of UTF-8 chars
9+ * @param max_width Maximum allowed width of string
10+ * @param cut_pos If max_width is exceeded, this will be initialized to last
11+ * full printable character before width limit.
912 * @return int width of the string
1013 */
11−int term_disp_width(bstr str);
14+int term_disp_width(bstr str, int max_width, const unsigned char **cut_pos);

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