mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-31 03:51:14 -03:00
Add wcstringutil.h truncate function
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user