From bdb98cf210becbdb29086b2e55123199b1e9f56f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 7 Feb 2022 22:37:58 +0530 Subject: [PATCH] Count chars not bytes when truncating menubar title --- kitty/cocoa_window.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index a01b78d3a..1280358c1 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -673,9 +673,10 @@ void cocoa_update_menu_bar_title(PyObject *pytitle) { NSString *title = nil; if (OPT(macos_menubar_title_max_length) > 0 && PyUnicode_GetLength(pytitle) > OPT(macos_menubar_title_max_length)) { - static char buf[2048]; - snprintf(buf, sizeof(buf), "%*.*s…", (int)OPT(macos_menubar_title_max_length), (int)OPT(macos_menubar_title_max_length), PyUnicode_AsUTF8(pytitle)); - title = @(buf); + static char fmt[64]; + snprintf(fmt, sizeof(fmt), "%%%ld.%ldU%%s", OPT(macos_menubar_title_max_length), OPT(macos_menubar_title_max_length)); + DECREF_AFTER_FUNCTION PyObject *st = PyUnicode_FromFormat(fmt, pytitle, "…"); + if (st) title = @(PyUnicode_AsUTF8(st)); } else { title = @(PyUnicode_AsUTF8(pytitle)); }