Source

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

"▶1
scriptencoding utf-8
execute frawor#Setup('0.1', {'@/resources': '0.0',
            \                       '@/os': '0.2'})
let s:utils={}
"▶1 utils.getcmd :: cmd, args, kwargs, esc → sh
let s:prefexpr='repeat("-", 1+(len(v:val[0])>1)).v:val[0]." ="[len(v:val[0])>1]'
function s:utils.getcmd(cmd, args, kwargs, esc)
    let cmd=a:cmd
    if !empty(a:kwargs)
        let cmd.=' '.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)))))'
                \            ))
    endif
    if !empty(a:args)
        let cmd.=' '.join(map(copy(a:args), 'shellescape(v:val, a:esc)'))
    endif
    return cmd
endfunction
"▶1 utils.run :: sh, hasnulls::0|1|2 → [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.printm :: sh, hasnulls::Bool → + :echom, shell
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
endfunction
"▶1 utils.diffopts :: opts, opts, difftrans → diffopts
function s:utils.diffopts(opts, defaultdiffopts, difftrans)
    let opts=extend(copy(a:defaultdiffopts), a:opts)
    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 post resource
call s:_f.postresource('utils', s:utils)
"▶1
call frawor#Lockvar(s:, '_pluginloaded')
" vim: ft=vim ts=4 sts=4 et fmr=▶,▲
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.