Skip to content

Instantly share code, notes, and snippets.

@khoahuynhdev
Last active March 30, 2020 13:17
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 khoahuynhdev/1d4f17382de38338019e3f3d11ad646f to your computer and use it in GitHub Desktop.
Save khoahuynhdev/1d4f17382de38338019e3f3d11ad646f to your computer and use it in GitHub Desktop.
Check corona update in Vim
function! s:corona_stats() abort
let l:lines = []
let l:keys = [
\ ['country', 'Quốc gia'],
\ ['cases', 'Số ca'],
\ ['todayCases', 'Số ca (hôm nay)'],
\ ['deaths', 'Tử vong'],
\ ['todayDeaths', 'Tử vong (hôm nay)'],
\ ['recovered', 'Hồi phục'],
\]
call add(l:lines, map(deepcopy(l:keys), 'v:val[1]'))
let l:contents = system('curl -s "https://corona-stats.online?format=json"')
let l:resp = json_decode(l:contents)
for l:row in l:resp.data + [l:resp.worldStats]
call add(l:lines, map(deepcopy(l:keys), 'l:row[v:val[0]]'))
endfor
let l:h = range(len(l:lines[0]))
for l:c in range(len(l:lines[0]))
let l:m = 0
let l:w = range(len(l:lines))
for l:r in range(len(l:w))
let l:w[l:r] = strdisplaywidth(l:lines[l:r][l:c])
let l:m = max([l:m, l:w[l:r]])
endfor
for l:r in range(len(l:w))
if l:c > 0
let l:lines[l:r][l:c] = repeat(' ', l:m - l:w[l:r]) . l:lines[l:r][l:c]
else
let l:lines[l:r][l:c] = l:lines[l:r][l:c] . repeat(' ', l:m - l:w[l:r])
endif
endfor
let l:h[l:c] = repeat('-', strdisplaywidth(l:lines[0][l:c]))
endfor
for l:n in range(len(l:lines))
let l:lines[l:n] = '|' . join(l:lines[l:n], '|') . '|'
endfor
call insert(l:lines, '|' . join(l:h, '|') . '|', 1)
call insert(l:lines, '|' . join(l:h, '|') . '|', len(l:lines)-1)
silent new
file __CORONA_STATS__
setlocal buftype=nofile nolist nonumber bufhidden=wipe noswapfile buflisted filetype=
silent call append(0, l:lines)
normal! gg
endfunction
command! CoronaStats call s:corona_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment