Skip to content

Commit 7933389

Browse files
committed
strpart breaks multi-byte strings
1 parent 6c06e4a commit 7933389

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

autoload/gist.vim

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ function! s:shellwords(str) abort
119119
return words
120120
endfunction
121121

122+
function! s:truncate(str, num)
123+
let mx_first = '^\(.\)\(.*\)$'
124+
let str = a:str
125+
let ret = ''
126+
let width = 0
127+
while 1
128+
let char = substitute(str, mx_first, '\1', '')
129+
let cells = strdisplaywidth(char)
130+
if cells == 0 || width + cells > a:num
131+
break
132+
endif
133+
let width = width + cells
134+
let ret .= char
135+
let str = substitute(str, mx_first, '\2', '')
136+
endwhile
137+
while width + 1 <= a:num
138+
let ret .= " "
139+
let width = width + 1
140+
endwhile
141+
return ret
142+
endfunction
143+
122144
function! s:format_gist(gist) abort
123145
let files = sort(keys(a:gist.files))
124146
if empty(files)
@@ -142,12 +164,10 @@ function! s:format_gist(gist) abort
142164
redir =>a |exe "sil sign place buffer=".bufnr('')|redir end
143165
let signlist = split(a, '\n')
144166
let width = winwidth(0) - ((&number||&relativenumber) ? &numberwidth : 0) - &foldcolumn - (len(signlist) > 2 ? 2 : 0)
145-
let s:gistlen = 33
146-
if !exists("g:gist_namelength")
147-
let g:gist_namelength=30
148-
endif
149-
let s:desclen = width-(s:gistlen+g:gist_namelength+10)
150-
return printf('gist: %-*s %-*s %-*s', s:gistlen, a:gist.id, g:gist_namelength+1, strpart(name, 0, g:gist_namelength), s:desclen+1, strpart(desc, 0, s:desclen))
167+
let idlen = 33
168+
let namelen = get(g:, 'gist_namelength', 30)
169+
let desclen = width - (idlen + namelen + 10)
170+
return printf('gist: %s %s %s', s:truncate(a:gist.id, idlen), s:truncate(name, namelen), s:truncate(desc, desclen))
151171
endfunction
152172

153173
" Note: A colon in the file name has side effects on Windows due to NTFS Alternate Data Streams; avoid it.

0 commit comments

Comments
 (0)