Source

aurum / autoload / aurum / drivers / common / utils.vim

"▶1
scriptencoding utf-8
execute frawor#Setup('0.4', {'@/resources': '0.0',
            \                       '@/os': '0.2'})
let s:utils={}
"▶1 utils.kwargstostr :: kwargs → str
let s:prefexpr='repeat("-", 1+(len(v:val[0])>1)).v:val[0]." ="[len(v:val[0])>1]'
function s:utils.kwargstostr(kwargs, esc)
    return join(map(filter(items(a:kwargs), 'v:val[1] isnot 0'),
                \   '((v:val[1] is 1)?'.
                \     '(repeat("-", 1+(len(v:val[0])>1)).v:val[0]):'.
                \   '((v:val[1] is 0)?'.
                \     '(""):'.
                \   '((type(v:val[1])=='.type([]).')?'.
                \     '(join(map(copy(v:val[1]), '.
                \               '"\"".'.s:prefexpr.'."\".'.
                \                     'shellescape(v:val,    a:esc)"))):'.
                \     '('.s:prefexpr.'.shellescape(v:val[1], a:esc)))))'
                \  ))
endfunction
"▶1 utils.getcmd :: cmd, args, kwargs, esc → sh
function s:utils.getcmd(cmd, args, kwargs, esc)
    let cmd=a:cmd
    if !empty(a:kwargs)
        let cmd.=' '.s:utils.kwargstostr(a:kwargs, a:esc)
    endif
    if !empty(a:args)
        let cmd.=' '.join(map(copy(a:args), 'shellescape(v:val, a:esc)'))
    endif
    return cmd
endfunction
"▶1 utils.run :: cmd, hasnulls::0|1|2, path → [String] + shell
function s:utils.run(cmd, hasnulls, cdpath)
    if a:hasnulls==2 && !empty(&shellredir)
        return call(s:_r.os.readsystem, [a:cmd]+(empty(a:cdpath)?
                    \                               ([]):
                    \                               ([a:cdpath])), {})
    elseif a:hasnulls
        let savedlazyredraw=&lazyredraw
        let savedeventignore=&eventignore
        set eventignore=all
        set lazyredraw
        try
            try
                tabnew
            catch /^Vim(tabnew):E523:/
                let r=s:utils.run(a:cmd, 2, a:cdpath)
                if empty(r[-1])
                    call remove(r, -1)
                endif
                return r
            endtry
            setlocal buftype=nofile modifiable noreadonly
            if !empty(a:cdpath)
                execute 'lcd' fnameescape(a:cdpath)
            endif
            " XXX this is not able to distinguish between output with and 
            " without trailing newline, and also is “smart” about lineendings
            silent execute '%!'.a:cmd
            let r=getline(1, '$')
            bwipeout!
        finally
            let &lazyredraw=savedlazyredraw
            let &eventignore=savedeventignore
        endtry
    else
        let cmd=a:cmd
        if !empty(a:cdpath)
            let cmd='cd '.shellescape(a:cdpath).' && '.cmd
        endif
        let r=split(system(cmd), "\n", 1)
    endif
    return r
endfunction
"▶1 utils.diffopts :: opts, opts, difftrans → diffopts
function s:utils.diffopts(opts, defaultdiffopts, difftrans)
    let opts=extend(copy(a:defaultdiffopts), a:opts)
    if has_key(opts, 'numlines')
        let opts.numlines.=''
    endif
    let r={}
    call map(filter(copy(a:difftrans), 'has_key(opts, v:key)'),
            \'extend(r, {v:val : opts[v:key]})')
    if has_key(opts, 'dates') && has_key(a:difftrans, 'dates')
        let r[a:difftrans.dates]=!opts.dates
    endif
    return r
endfunction
"▶1 utils.addfiles :: repo, files + status → + add, forget
function s:utils.addfiles(repo, files)
    let status=a:repo.functions.status(a:repo, 0, 0, a:files)
    for file in status.unknown
        call a:repo.functions.add(a:repo, file)
    endfor
    for file in status.deleted
        call a:repo.functions.forget(a:repo, file)
    endfor
endfunction
"▶1 utils.usefile :: repo, message, kw, kw, func, args, kwargs, emes
function s:utils.usefile(repo, message, kwfile, kwmes, Func, args, kwargs, ...)
    if a:message=~#'\v[\r\n]'
        let tmpfile=tempname()
        call writefile(split(a:message, "\n", 1), tmpfile, 'b')
        let a:kwargs[a:kwfile]=tmpfile
        let usingfile=1
    else
        let a:kwargs[a:kwmes]=a:message
        let usingfile=0
    endif
    try
        return call(a:Func, [a:repo, 'commit', a:args, a:kwargs]+a:000, {})
    finally
        if usingfile && filereadable(tmpfile)
            call delete(tmpfile)
        endif
    endtry
endfunction
"▶1 utils.printm :: String → + :echo | :echom
if s:_f.require('autoload/ansi_esc_echo', [0, 0], 0)
    function s:utils.printm(str, ...)
        return call(s:_r.ansi_esc.echo, [a:str]+a:000, {})
    endfunction
    let s:utils.using_ansi_esc_echo=1
else
    function s:utils.printm(m, ...)
        let prevempty=0
        for line in a:m
            if empty(line)
                let prevempty+=1
            else
                if prevempty
                    while prevempty
                        echom ' '
                        let prevempty-=1
                    endwhile
                endif
                echom line
            endif
        endfor
        return 0
    endfunction
    let s:utils.using_ansi_esc_echo=0
endif
"▶1 utils.emptystatdct
let s:utils.emptystatdct={
            \'modified': [],
            \   'added': [],
            \ 'removed': [],
            \ 'deleted': [],
            \ 'unknown': [],
            \ 'ignored': [],
            \   'clean': [],
        \}
"▶1 nullnl :: [String] → [String]
" Convert between lines (NL separated strings with NULLs represented as NLs) and 
" NULL separated strings with NLs represented by NLs.
function s:utils.nullnl(text)
    let r=['']
    for nlsplit in map(copy(a:text), 'split(v:val, "\n", 1)')
        let r[-1].="\n".nlsplit[0]
        call extend(r, nlsplit[1:])
    endfor
    if empty(r[0])
        call remove(r, 0)
    else
        let r[0]=r[0][1:]
    endif
    return r
endfunction
"▶1 post resource
call s:_f.postresource('utils', s:utils)
"▶1
call frawor#Lockvar(s:, '')
" vim: ft=vim ts=4 sts=4 et fmr=▶,▲