Created
December 4, 2013 15:41
-
-
Save cohama/7789728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s:V = vital#of('vital') | |
function! s:truncate(text, max_width, ellipsis) | |
let ellipsis_width = strdisplaywidth(a:ellipsis) | |
let text_width = strdisplaywidth(a:text) | |
if text_width > a:max_width | |
let truncated = s:V.strwidthpart(a:text, a:max_width - ellipsis_width) | |
" for double width string | |
if strdisplaywidth(truncated) != a:max_width - ellipsis_width | |
let truncated .= ' ' | |
endif | |
return truncated . a:ellipsis | |
else | |
return a:text | |
endif | |
endfunction | |
echo repeat('|', 20) . '$' | |
echo s:truncate('ほげhogefuga', 20, '… 続きを読む') . '$' | |
echo s:truncate('ほげhogefugohogefugo', 20, '… 続きを読む') . '$' | |
echo s:truncate('ほげhogefugohogefugohogefugo', 20, '… 続きを読む') . '$' | |
echo s:truncate('ほげほげhogefugohogefugohogefugohogefugo', 20, '… 続きを読む') . '$' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment