diff --git a/src/wcstringutil.cpp b/src/wcstringutil.cpp index 163b0e8ed..79209c1c5 100644 --- a/src/wcstringutil.cpp +++ b/src/wcstringutil.cpp @@ -29,3 +29,19 @@ wcstring_range wcstring_tok(wcstring& str, const wcstring& needle, wcstring_rang str[next_pos] = L'\0'; return std::make_pair(pos, next_pos - pos); } + +wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype) { + if (input.size() <= max_len) { + return input; + } + + if (etype == ellipsis_type::None) { + return input.substr(0, max_len); + } + if (etype == ellipsis_type::Prettiest) { + return input.substr(0, max_len - wcslen(ellipsis_str)).append(ellipsis_str); + } + wcstring output = input.substr(0, max_len - 1); + output.push_back(ellipsis_char); + return output; +} diff --git a/src/wcstringutil.h b/src/wcstringutil.h index 04a1a4133..878771f25 100644 --- a/src/wcstringutil.h +++ b/src/wcstringutil.h @@ -49,4 +49,15 @@ void split_about(ITER haystack_start, ITER haystack_end, ITER needle_start, ITER // Trailing component, possibly empty. output->push_back(wcstring(haystack_cursor, haystack_end)); } + +enum class ellipsis_type { + None, + //Prefer niceness over minimalness + Prettiest, + //Make every character count ($ instead of ...) + Shortest, +}; + +wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype = ellipsis_type::Prettiest); + #endif