Skip to content

Instantly share code, notes, and snippets.

@khoahuynhdev
Last active March 30, 2020 13:17
Show Gist options
  • Save khoahuynhdev/1d4f17382de38338019e3f3d11ad646f to your computer and use it in GitHub Desktop.
Save khoahuynhdev/1d4f17382de38338019e3f3d11ad646f to your computer and use it in GitHub Desktop.

Revisions

  1. khoahuynhdev revised this gist Mar 30, 2020. No changes.
  2. khoahuynhdev revised this gist Mar 30, 2020. 1 changed file with 37 additions and 0 deletions.
    37 changes: 37 additions & 0 deletions corona.vim
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,40 @@
    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
  3. khoahuynhdev created this gist Mar 30, 2020.
    9 changes: 9 additions & 0 deletions corona.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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()