Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created October 11, 2011 11:44
Show Gist options
  • Save ynkdir/1277884 to your computer and use it in GitHub Desktop.
Save ynkdir/1277884 to your computer and use it in GitHub Desktop.
vim cui scrollbar
" require: term.vim
" http://www.vim.org/scripts/script.php?script_id=2812
autocmd CursorMoved,CursorMovedI * call s:update_scrollbar_absolute()
" how to get curwin->w_winrow and curwin->w_wincol?
function! s:update_scrollbar_absolute()
let winline = winline()
let wincol = wincol()
let winwidth = &columns
let winheight = &lines
let line = line('.') + 0.0
let lastline = line('$') + 0.0
let percent = float2nr(((line - 1) / lastline) * winheight)
let barheight = float2nr(ceil(winheight / lastline))
call term#save_cursor()
for i in range(winheight)
call term#put(i + 1, winwidth, ' ', (percent <= i && i < percent + barheight) ? 45 : 44)
endfor
call term#attr()
call term#restore_cursor()
call term#out()
endfunction
" buggy
function! s:update_scrollbar_relative()
" XXX: Since cursor is not drawn yet, update cursor.
redraw
let winline = winline()
let wincol = wincol()
let winwidth = winwidth(0)
let winheight = winheight(0)
let line = line('.') + 0.0
let lastline = line('$') + 0.0
let percent = float2nr(((line - 1) / lastline) * winheight)
let barheight = float2nr(ceil(winheight / lastline))
call term#save_cursor()
call term#preceding_line(winline - 1)
for i in range(winheight)
call term#forward(winwidth - 1)
call term#text(' ', (percent <= i && i < percent + barheight) ? 45 : 44)
call term#next_line()
endfor
call term#attr()
call term#restore_cursor()
call term#out()
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment