Skip to content

Commit

Permalink
ENH: Improve lh#dict#print_as_tree() rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHermitte committed Dec 13, 2024
1 parent dd94f17 commit 326db2c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions autoload/lh/dict.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
" <URL:http://github.com/LucHermitte/lh-vim-lib>
" License: GPLv3 with exceptions
" <URL:http://github.com/LucHermitte/lh-vim-lib/tree/master/License.md>
" Version: 4.7.1
let s:k_version = '40701'
" Version: 5.4.1
let s:k_version = '50401'
" Created: 26th Nov 2015
" Last Update: 03rd Dec 2019
" Last Update: 13th Dec 2024
"------------------------------------------------------------------------
" Description:
" |Dict| helper functions
Expand Down Expand Up @@ -146,41 +146,43 @@ endfunction
" Function: lh#dict#print_as_tree(dict, [, indent]) {{{3
" @since version 4.7.1
function! lh#dict#print_as_tree(V, ...) abort
let sw = get(g:, 'lh#dict#prettyprint_indent', &sw)
let over_indent = get(g:, 'lh#dict#prettyprint_close_indent', 0) * sw
let indent = get(a:, 1, 0)
if type(a:V) == type([])
let res = ['['] + s:recurse_list(a:V, indent+4) + [repeat(' ', indent+4) . ']']
let res = ['['] + s:recurse_list(a:V, indent+sw, sw) + [repeat(' ', indent+over_indent) . ']']
elseif type(a:V) == type({})
if lh#object#is_an_object(a:V)
let res = [lh#object#to_string(a:V)]
else
let res = ['{'] + s:recurse_dict(a:V, indent+4) + [repeat(' ', indent+4) . '}']
let res = ['{'] + s:recurse_dict(a:V, indent+sw, sw) + [repeat(' ', indent+over_indent) . '}']
endif
else
let res = [string(a:V)]
endif
return indent == 0 ? join(res, "\n") : res
endfunction

function! s:recurse_list(list, indent) abort
function! s:recurse_list(list, indent, sw) abort
let res = []
let lead = repeat(' ', a:indent) . '+- '
let nb_digits = float2nr(ceil(log10(len(a:list))))
for [k,l:V] in map(copy(a:list), {k0,v0 -> [k0,v0]})
let sub_res = lh#dict#print_as_tree(l:V, a:indent+4)
let sub_res = lh#dict#print_as_tree(l:V, a:indent+a:sw)
let sub_res[0] = printf('%s%'.nb_digits.'d -> %s', lead, k, sub_res[0])
let res += sub_res
endfor
return res
endfunction

function! s:recurse_dict(dict, indent) abort
function! s:recurse_dict(dict, indent, sw) abort
let res = []
let lead = repeat(' ', a:indent) . '+- '
let max_length = max(map(keys(a:dict), {k,V -> strdisplaywidth(string(V))}))

for [k,l:V] in items(a:dict)
let start = lead . string(k). repeat(' ', max_length- strdisplaywidth(string(k))+1).'= '
let sub_res = lh#dict#print_as_tree(l:V, a:indent+4)
let sub_res = lh#dict#print_as_tree(l:V, a:indent+a:sw)
let sub_res[0] = start . sub_res[0]
let res += sub_res
endfor
Expand Down

0 comments on commit 326db2c

Please sign in to comment.