ZyX_I avatar ZyX_I committed 438f71c

@%aurum/drivers/common/utils, @%aurum/drivers/mercurial:
Added optional ansi_esc_echo support. Fixes #42

Comments (0)

Files changed (4)

autoload/aurum/drivers/common/utils.vim

 "▶1
 scriptencoding utf-8
-execute frawor#Setup('0.1', {'@/resources': '0.0',
+execute frawor#Setup('0.2', {'@/resources': '0.0',
             \                       '@/os': '0.2'})
 let s:utils={}
 "▶1 utils.getcmd :: cmd, args, kwargs, esc → sh
     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)
         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 post resource
 call s:_f.postresource('utils', s:utils)
 "▶1

autoload/aurum/drivers/mercurial.vim

     catch
         " s:usepythondriver stays equal to 0, errors are ignored
     endtry
-    " FIXME Does not work in python3. Not very problematic as mercurial does not 
-    " do this either, but it will be necessary to review these lines after 
-    " python3 support in mercurial will be finished.
     if s:usepythondriver
+        " FIXME Does not work in python3. Not very problematic as mercurial does 
+        "       not do this either, but it will be necessary to review these 
+        "       lines after python3 support in mercurial will be finished.
         execute s:py 'reload(aurum)'
+        if s:_r.utils.using_ansi_esc_echo
+            if exists('*pyeval')
+                execute s:py 'aurum.register_ansi_esc_echo_func('.
+                            \       'vim.bindeval("s:_r.utils.printm"))'
+            endif
+        endif
     endif
 endif
 let s:nullrev=repeat('0', 40)
     0.1: Added dict.hlines key (see version 0.4 of @aurum).
 @%aurum/drivers/common/utils:
     0.1: _r.utils.getcmd() now accepts kwargs key values 0 and [...]
+    0.2: Added _r.utils.using_ansi_esc_echo
 
 vim: ft=help:tw=78
 import os
 import json
 import re
+import sys
 
 def outermethod(func):
     """
                 r+='"'+str(obj).replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')+'"'
     return r
 
+def pyecho(o, error=False):
+    try:
+        return (sys.stderr if error else sys.stdout).write(str(o))
+    except UnicodeDecodeError:
+        if error:
+            vim.command('echohl ErrorMsg')
+        for line in str(o).split("\n"):
+            if not line:
+                line=' '
+            vim.command('echomsg '+utf_dumps(line))
+        if error:
+            vim.command('echohl None')
+
+if hasattr(vim, 'bindeval'):
+    ansi_esc_echo_func=None
+    def register_ansi_esc_echo_func(func):
+        ansi_esc_echo_func=func
+        global echoe
+        echoe=ansi_esc_echo
+
+    def ansi_esc_echo(o, colinfo):
+        if colinfo is None:
+            return ansi_esc_echo_func(str(o))
+        else:
+            return ansi_esc_echo_func(str(o), colinfo)
+
+echoe=lambda o, colinfo: pyecho(o, True )
+echom=lambda o, colinfo: pyecho(o, False)
+
 def utf_dumps(obj):
     return json.dumps(obj, encoding='utf8')
 
         return '"'+str(obj).replace('\\', '\\\\').replace('"', '\\"')+'"'
 
 class PrintUI(ui.ui):
-    @staticmethod
-    def _write(f, o, warning=False):
-        try:
-            return f.write(str(o))
-        except UnicodeDecodeError:
-            for line in str(o).split("\n"):
-                if line == '':
-                    line=' '
-                if warning:
-                    vim.command('echohl ErrorMsg')
-                vim.command('echomsg '+utf_dumps(line))
-                if warning:
-                    vim.command('echohl None')
-
     # ui.ui for some reason does not support outputting unicode
     def write(self, *args, **kwargs):
         if self._buffers:
             self._buffers[-1].extend([str(a) for a in args])
         else:
+            colinfo=None
             for a in args:
-                self._write(self.fout, a)
+                colinfo=echom(a, colinfo)
 
     def write_err(self, *args, **kwargs):
+        colinfo=None
         for a in args:
-            self._write(self.ferr, a, True)
+            colinfo=echoe(a, colinfo)
 
 class CaptureUI(PrintUI):
     def __init__(self):
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.