Skip to content

Instantly share code, notes, and snippets.

@cohama
Created December 4, 2013 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cohama/7789728 to your computer and use it in GitHub Desktop.
Save cohama/7789728 to your computer and use it in GitHub Desktop.
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