1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 06:50:05 +08:00

Add drawing_box api

This commit is contained in:
wsdjeg 2017-04-27 23:31:04 +08:00
parent 0c9cb6d445
commit f6fc48f991
2 changed files with 94 additions and 0 deletions

View File

@ -26,6 +26,31 @@ endfunction
let s:file['fill'] = function('s:fill') let s:file['fill'] = function('s:fill')
function! s:fill_middle(str, length) abort
if strwidth(a:str) <= a:length
"return a:str . repeat(' ', a:length - strwidth(a:str))
let n = a:length - strwidth(a:str)
if n % 2 == 0
return repeat(' ', (a:length - strwidth(a:str))/2) . a:str . repeat(' ', (a:length - strwidth(a:str))/2)
else
return repeat(' ', (a:length - strwidth(a:str))/2) . a:str . repeat(' ', (a:length + 1 - strwidth(a:str))/2)
endif
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_middle'] = function('s:fill_middle')
function! s:trim_start(str) abort function! s:trim_start(str) abort
return substitute(a:str, '^\s*', '', 'g') return substitute(a:str, '^\s*', '', 'g')
endfunction endfunction

View File

@ -76,6 +76,75 @@ endfunction
let s:box['drawing_table'] = function('s:drawing_table') let s:box['drawing_table'] = function('s:drawing_table')
function! s:drawing_box(data, h, w, bw) abort
if &encoding ==# 'utf-8'
let top_left_corner = '╭'
let top_right_corner = '╮'
let bottom_left_corner = '╰'
let bottom_right_corner = '╯'
let side = '│'
let top_bottom_side = '─'
let middle = '┼'
let top_middle = '┬'
let left_middle = '├'
let right_middle = '┤'
let bottom_middle = '┴'
else
let top_left_corner = '*'
let top_right_corner = '*'
let bottom_left_corner = '*'
let bottom_right_corner = '*'
let side = '|'
let top_bottom_side = '-'
let middle = '*'
let top_middle = '*'
let left_middle = '*'
let right_middle = '*'
let bottom_middle = '*'
endif
let box = []
let top_line = top_left_corner
\ . repeat(repeat(top_bottom_side, a:bw) . top_middle, a:w - 1)
\ . repeat(top_bottom_side, a:bw)
\ . top_right_corner
let middle_line = left_middle
\ . repeat(repeat(top_bottom_side, a:bw) . middle, a:w - 1)
\ . repeat(top_bottom_side, a:bw)
\ . right_middle
let bottom_line = bottom_left_corner
\ . repeat(repeat(top_bottom_side, a:bw) . bottom_middle, a:w - 1)
\ . repeat(top_bottom_side, a:bw)
\ . bottom_right_corner
let empty_line = side
\ . repeat(repeat(' ', a:bw) . side, a:w)
call add(box, top_line)
let i = 0
let ls = 1
let line = side
for sel in a:data
let line .=s:string.fill_middle(sel, a:bw) . side
let i += 1
if i == a:w
call add(box, line)
call add(box, middle_line)
let ls += 1
let line = side
let i = 0
endif
endfor
if ls < a:h
for j in range(a:h - ls)
call add(box, empty_line)
call add(box, middle)
endfor
else
let box[-1] = bottom_line
endif
return join(box, "\n")
endfunction
let s:box['drawing_box'] = function('s:drawing_box')
function! SpaceVim#api#unicode#box#get() abort function! SpaceVim#api#unicode#box#get() abort
return deepcopy(s:box) return deepcopy(s:box)
endfunction endfunction