mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-04-15 08:09:11 +08:00
Update box api
This commit is contained in:
parent
74ac6ab4b6
commit
96e2c38d93
22
autoload/SpaceVim/api/data/json.vim
Normal file
22
autoload/SpaceVim/api/data/json.vim
Normal file
@ -0,0 +1,22 @@
|
||||
let s:json = {}
|
||||
|
||||
function! s:json_encode(expr) abort
|
||||
" TODO
|
||||
" support old vim version
|
||||
return json_encode(a:expr)
|
||||
endfunction
|
||||
|
||||
let s:json['json_encode'] = function('s:json_encode')
|
||||
|
||||
function! s:json_decode(json) abort
|
||||
" TODO
|
||||
" support old vim version
|
||||
return json_decode(a:json)
|
||||
endfunction
|
||||
|
||||
let s:json['json_decode'] = function('s:json_decode')
|
||||
|
||||
|
||||
function! SpaceVim#api#data#json#get() abort
|
||||
return deepcopy(s:json)
|
||||
endfunction
|
@ -7,6 +7,25 @@ endfunction
|
||||
|
||||
let s:file['trim'] = function('s:trim')
|
||||
|
||||
function! s:fill(str, length) abort
|
||||
if strwidth(a:str) <= a:length
|
||||
return a:str . repeat(' ', a:length - strwidth(a:str))
|
||||
else
|
||||
let l = 0
|
||||
for i in range(strchars(a:str) - 1)
|
||||
if strwidth(strcharpart(a:str, 0, i)) > a:length
|
||||
break
|
||||
else
|
||||
let l = i
|
||||
endif
|
||||
endfor
|
||||
let str = strcharpart(a:str, 0, l)
|
||||
return str . repeat(' ', a:length - strwidth(str))
|
||||
endif
|
||||
endfunction
|
||||
|
||||
let s:file['fill'] = function('s:fill')
|
||||
|
||||
function! s:trim_start(str) abort
|
||||
return substitute(a:str, '^\s*', '', 'g')
|
||||
endfunction
|
||||
|
@ -1,6 +1,9 @@
|
||||
let s:box = {}
|
||||
let s:json = SpaceVim#api#import('data#json')
|
||||
let s:string = SpaceVim#api#import('data#string')
|
||||
scriptencoding utf-8
|
||||
" http://jrgraphix.net/r/Unicode/2500-257F
|
||||
" http://www.alanflavell.org.uk/unicode/unidata.html
|
||||
|
||||
" json should be a list of items which have same keys
|
||||
function! s:drawing_table(json) abort
|
||||
@ -33,13 +36,41 @@ function! s:drawing_table(json) abort
|
||||
let bottom_middle = '*'
|
||||
endif
|
||||
let table = []
|
||||
let col = len(keys(a:json[0]))
|
||||
|
||||
|
||||
|
||||
|
||||
let items = s:json.json_decode(a:json)
|
||||
let col = len(keys(items[0]))
|
||||
let top_line = top_left_corner
|
||||
\ . repeat(repeat(top_bottom_side, 15) . top_middle, col - 1)
|
||||
\ . repeat(top_bottom_side, 15)
|
||||
\ . top_right_corner
|
||||
let middle_line = left_middle
|
||||
\ . repeat(repeat(top_bottom_side, 15) . middle, col - 1)
|
||||
\ . repeat(top_bottom_side, 15)
|
||||
\ . right_middle
|
||||
let bottom_line = bottom_left_corner
|
||||
\ . repeat(repeat(top_bottom_side, 15) . bottom_middle, col - 1)
|
||||
\ . repeat(top_bottom_side, 15)
|
||||
\ . bottom_right_corner
|
||||
call add(table, top_line)
|
||||
let tytle = side
|
||||
let keys = keys(items[0])
|
||||
for key in keys
|
||||
let tytle .= s:string.fill(items[0][key], 15) . side
|
||||
endfor
|
||||
call add(table, tytle)
|
||||
for item in items
|
||||
let value_line = side
|
||||
for key in keys
|
||||
let value_line .= s:string.fill(item[key], 15) . side
|
||||
endfor
|
||||
call add(table, value_line)
|
||||
call add(table, middle_line)
|
||||
endfor
|
||||
let table[-1] = bottom_line
|
||||
return table
|
||||
endfunction
|
||||
|
||||
function! SpaceVim#api#file#get() abort
|
||||
let s:box['drawing_table'] = function('s:drawing_table')
|
||||
|
||||
function! SpaceVim#api#unicode#box#get() abort
|
||||
return deepcopy(s:box)
|
||||
endfunction
|
||||
|
Loading…
x
Reference in New Issue
Block a user