mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-02 22:20:06 +08:00
Add box and tree api (#3820)
This commit is contained in:
parent
6b6db3f524
commit
e2d5865a17
@ -6,15 +6,26 @@
|
||||
" License: GPLv3
|
||||
"=============================================================================
|
||||
|
||||
let s:box = {}
|
||||
let s:json = SpaceVim#api#import('data#json')
|
||||
let s:string = SpaceVim#api#import('data#string')
|
||||
scriptencoding utf-8
|
||||
|
||||
""
|
||||
" @section unicode#box, api-unicode-box
|
||||
" @parentsection api
|
||||
" provides some functions to draw box and table.
|
||||
"
|
||||
" drawing_table({json}[, {keys}])
|
||||
"
|
||||
" drawing table with json data.
|
||||
"
|
||||
|
||||
let s:self = {}
|
||||
let s:self._json = SpaceVim#api#import('data#json')
|
||||
let s:self._string = SpaceVim#api#import('data#string')
|
||||
" 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
|
||||
function! s:self.drawing_table(json, ...) abort
|
||||
if empty(a:json)
|
||||
return []
|
||||
endif
|
||||
@ -44,7 +55,7 @@ function! s:drawing_table(json, ...) abort
|
||||
let bottom_middle = '*'
|
||||
endif
|
||||
let table = []
|
||||
let items = s:json.json_decode(a:json)
|
||||
let items = self._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)
|
||||
@ -66,14 +77,14 @@ function! s:drawing_table(json, ...) abort
|
||||
let keys = a:1
|
||||
endif
|
||||
for key in keys
|
||||
let tytle .= s:string.fill(key , 15) . side
|
||||
let tytle .= self._string.fill(key , 15) . side
|
||||
endfor
|
||||
call add(table, tytle)
|
||||
call add(table, middle_line)
|
||||
for item in items
|
||||
let value_line = side
|
||||
for key in keys
|
||||
let value_line .= s:string.fill(item[key], 15) . side
|
||||
let value_line .= self._string.fill(item[key], 15) . side
|
||||
endfor
|
||||
call add(table, value_line)
|
||||
call add(table, middle_line)
|
||||
@ -82,10 +93,8 @@ function! s:drawing_table(json, ...) abort
|
||||
return table
|
||||
endfunction
|
||||
|
||||
let s:box['drawing_table'] = function('s:drawing_table')
|
||||
|
||||
" @vimlint(EVL102, 1, l:j)
|
||||
function! s:drawing_box(data, h, w, bw) abort
|
||||
function! s:self.drawing_box(data, h, w, bw) abort
|
||||
if &encoding ==# 'utf-8'
|
||||
let top_left_corner = '╭'
|
||||
let top_right_corner = '╮'
|
||||
@ -131,7 +140,7 @@ function! s:drawing_box(data, h, w, bw) abort
|
||||
let ls = 1
|
||||
let line = side
|
||||
for sel in a:data
|
||||
let line .=s:string.fill_middle(sel, a:bw) . side
|
||||
let line .=self._string.fill_middle(sel, a:bw) . side
|
||||
let i += 1
|
||||
if i == a:w
|
||||
call add(box, line)
|
||||
@ -153,10 +162,8 @@ function! s:drawing_box(data, h, w, bw) abort
|
||||
endfunction
|
||||
" @vimlint(EVL102, 0, l:j)
|
||||
|
||||
let s:box['drawing_box'] = function('s:drawing_box')
|
||||
|
||||
function! SpaceVim#api#unicode#box#get() abort
|
||||
return deepcopy(s:box)
|
||||
return deepcopy(s:self)
|
||||
endfunction
|
||||
|
||||
" vim:set et sw=2:
|
||||
|
70
autoload/SpaceVim/api/unicode/tree.vim
Normal file
70
autoload/SpaceVim/api/unicode/tree.vim
Normal file
@ -0,0 +1,70 @@
|
||||
scriptencoding utf-8
|
||||
let s:self = {}
|
||||
let s:self._json = SpaceVim#api#import('data#json')
|
||||
let s:self._string = SpaceVim#api#import('data#string')
|
||||
let s:self._vim = SpaceVim#api#import('vim')
|
||||
let s:self.bottom_left_corner = '╰'
|
||||
let s:self.side = '│'
|
||||
let s:self.top_bottom_side = '─'
|
||||
let s:self.left_middle = '├'
|
||||
|
||||
|
||||
" ╰test
|
||||
" ├hello
|
||||
" │ ├one
|
||||
" │ ╰two
|
||||
" ╰world
|
||||
"
|
||||
" echo line should be prefix . extra . value
|
||||
|
||||
function! s:self.drawing_tree(tree, ...) abort
|
||||
let tree = []
|
||||
let prefix = get(a:000, 0, '')
|
||||
let extra = get(a:000, 1, '')
|
||||
if self._vim.is_string(a:tree)
|
||||
call add(tree, prefix . extra . a:tree)
|
||||
elseif self._vim.is_list(a:tree)
|
||||
let i = 1
|
||||
for item in a:tree
|
||||
if i < len(a:tree)
|
||||
let sidebar = self.side
|
||||
let extra = self.left_middle
|
||||
else
|
||||
let extra = self.bottom_left_corner
|
||||
if i ==# 1
|
||||
let sidebar = self.side
|
||||
else
|
||||
let sidebar = ' '
|
||||
endif
|
||||
endif
|
||||
call extend(tree, self.drawing_tree(item, prefix . extra, ' '))
|
||||
let i += 1
|
||||
endfor
|
||||
elseif self._vim.is_dict(a:tree)
|
||||
let i = 1
|
||||
for key in keys(a:tree)
|
||||
if i < len(a:tree)
|
||||
let sidebar = self.side
|
||||
let extra = self.left_middle
|
||||
else
|
||||
let extra = self.bottom_left_corner
|
||||
if i ==# 1
|
||||
let sidebar = self.side
|
||||
else
|
||||
let sidebar = ' '
|
||||
endif
|
||||
endif
|
||||
call add(tree, prefix . extra . key)
|
||||
call extend(tree, self.drawing_tree(get(a:tree, key, []), prefix . sidebar , ' '))
|
||||
let i += 1
|
||||
endfor
|
||||
endif
|
||||
return tree
|
||||
endfunction
|
||||
|
||||
function! SpaceVim#api#unicode#tree#get() abort
|
||||
|
||||
return deepcopy(s:self)
|
||||
|
||||
endfunction
|
||||
|
@ -1864,11 +1864,12 @@ CONTENTS *SpaceVim-contents*
|
||||
9. prompt..........................................|SpaceVim-api-prompt|
|
||||
10. sid...........................................|SpaceVim-api-vim-sid|
|
||||
11. system.........................................|SpaceVim-api-system|
|
||||
12. vim#buffer.................................|SpaceVim-api-vim-buffer|
|
||||
13. vim#buffer.................................|SpaceVim-api-vim-window|
|
||||
14. vim#command...............................|SpaceVim-api-vim-command|
|
||||
15. vim#compatible.........................|SpaceVim-api-vim-compatible|
|
||||
16. vim#message...............................|SpaceVim-api-vim-message|
|
||||
12. unicode#box...............................|SpaceVim-api-unicode-box|
|
||||
13. vim#buffer.................................|SpaceVim-api-vim-buffer|
|
||||
14. vim#buffer.................................|SpaceVim-api-vim-window|
|
||||
15. vim#command...............................|SpaceVim-api-vim-command|
|
||||
16. vim#compatible.........................|SpaceVim-api-vim-compatible|
|
||||
17. vim#message...............................|SpaceVim-api-vim-message|
|
||||
9. FAQ........................................................|SpaceVim-faq|
|
||||
10. Changelog...........................................|SpaceVim-changelog|
|
||||
|
||||
@ -5452,6 +5453,16 @@ name()
|
||||
Return the name of current os, availibel value is: linux, cygwin, windows and
|
||||
mac.
|
||||
|
||||
==============================================================================
|
||||
UNICODE#BOX *SpaceVim-api-unicode-box*
|
||||
|
||||
provides some functions to draw box and table.
|
||||
|
||||
drawing_table({json}[, {keys}])
|
||||
|
||||
drawing table with json data.
|
||||
|
||||
|
||||
==============================================================================
|
||||
VIM#BUFFER *SpaceVim-api-vim-buffer*
|
||||
|
||||
|
30
docs/api/unicode/box.md
Normal file
30
docs/api/unicode/box.md
Normal file
@ -0,0 +1,30 @@
|
||||
---
|
||||
title: "unicode#box API"
|
||||
description: "unicode#box API provides some basic functions for drawing box."
|
||||
---
|
||||
|
||||
# [Available APIs](../../) >> unicode#box
|
||||
|
||||
<!-- vim-markdown-toc GFM -->
|
||||
|
||||
- [Intro](#intro)
|
||||
- [Functions](#functions)
|
||||
|
||||
<!-- vim-markdown-toc -->
|
||||
|
||||
## Intro
|
||||
|
||||
`unicode#box` API provides some basic functions for drawing box and table.
|
||||
|
||||
```vim
|
||||
let s:SPI = SpaceVim#api#import('unicode#box')
|
||||
call s:SPI.apply('dot1', 'g:dotstr')
|
||||
set statusline+=%{g:dotstr}
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
| function name | description |
|
||||
| ------------------------ | ------------------------------ |
|
||||
| `apply(name, time, var)` | start a job, return the job id |
|
||||
|
@ -1,6 +1,9 @@
|
||||
Execute ( SpaceVim api: unicode#box ):
|
||||
new
|
||||
let &encoding = 'utf-8'
|
||||
Execute ( SpaceVim api: unicode#tree ):
|
||||
let box = SpaceVim#api#import('unicode#box')
|
||||
AssertEqual box.drawing_box([1,2,3] , 1, 3, 5),
|
||||
\ ['╭─────┬─────┬─────╮', '│ 1 │ 2 │ 3 │', '╰─────┴─────┴─────╯']
|
||||
\ [
|
||||
\ '╭─────┬─────┬─────╮',
|
||||
\ '│ 1 │ 2 │ 3 │',
|
||||
\ '╰─────┴─────┴─────╯'
|
||||
\ ]
|
||||
|
||||
|
11
test/api/unicode/tree.vader
Normal file
11
test/api/unicode/tree.vader
Normal file
@ -0,0 +1,11 @@
|
||||
Execute ( SpaceVim api: unicode#tree ):
|
||||
let box = SpaceVim#api#import('unicode#tree')
|
||||
AssertEqual box.drawing_tree({'test' : [{'hello' : ['one', 'two']},'world']}),
|
||||
\ [
|
||||
\ 'test',
|
||||
\ ' ├hello',
|
||||
\ ' │ ├one',
|
||||
\ ' │ ╰two',
|
||||
\ ' ╰world',
|
||||
\ ]
|
||||
|
Loading…
Reference in New Issue
Block a user