summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2015-06-22 23:04:02 +0200
committerJan Pokorný <jpokorny@redhat.com>2015-07-02 23:42:58 +0200
commit52d54f461b0a7f6cd7a06b4288d64e807368d4dd (patch)
tree505184633fef5c367911b5ff93c30d4627edc26e
parent70f7ba421725809033d63c67ad8d608ac7808bad (diff)
downloadclufter-52d54f461b0a7f6cd7a06b4288d64e807368d4dd.tar.gz
clufter-52d54f461b0a7f6cd7a06b4288d64e807368d4dd.tar.xz
clufter-52d54f461b0a7f6cd7a06b4288d64e807368d4dd.zip
filters/cmd-wrap: new filter (promoting command format)
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
-rw-r--r--filters/cmd_wrap.py60
-rw-r--r--formats/command.py (renamed from ext-plugins/lib-general/formats/command.py)0
2 files changed, 60 insertions, 0 deletions
diff --git a/filters/cmd_wrap.py b/filters/cmd_wrap.py
new file mode 100644
index 0000000..4ac5d21
--- /dev/null
+++ b/filters/cmd_wrap.py
@@ -0,0 +1,60 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2015 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""cmd-wrap filter"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from ..filter import Filter
+from ..formats.command import command
+
+from os import getenv
+from textwrap import TextWrapper
+
+
+@Filter.deco('string-iter', 'string-iter')
+def cmd_wrap(flt_ctxt, in_obj):
+ """Try to apply a bit smarter wrapping on lines carrying shell commands
+
+ Smarter means:
+ - do not delimit option from its argument
+ - when line is broken vertically, append backslash for the continuation
+ and indent subsequent lines for visual clarity
+ - and as a side-effect: normalize whitespace occurrences
+ """
+ try:
+ tw_system = int(getenv('COLUMNS'))
+ except TypeError:
+ tw_system = 0
+ try:
+ tw = int(flt_ctxt.get('text_width'))
+ if not tw:
+ raise TypeError
+ elif tw < 0:
+ tw = -tw
+ tw = tw if not tw_system or tw < tw_system else tw_system
+ except TypeError:
+ tw = tw_system
+ if not tw:
+ tw = 72
+ cw = TextWrapper(width=tw, subsequent_indent='# ') # wrapper for comments
+
+ ret = []
+ for line in in_obj('stringiter', protect_safe=True):
+ if line.lstrip().startswith('#'):
+ ret.append(cw.wrap(line))
+ continue
+ linecnt, rline, remains = 0, [], tw - 2 # ' \'
+ for itemgroup in command('bytestring', line)('separated'):
+ item = ' '.join(itemgroup)
+ fills = len(item) + (1 if rline else 0)
+ # also deal with text width overflow on the first line
+ if (remains - fills >= 0 or not rline):
+ rline.append(item)
+ remains -= fills
+ else:
+ ret.append((' ' if linecnt else '') + ' '.join(rline) + ' \\')
+ linecnt += 1
+ rline, remains = [item], tw - len(item) - 4 # ' \' & ident
+ ret.append((' ' if linecnt else '') + ' '.join(rline))
+ return ('stringiter', ret)
diff --git a/ext-plugins/lib-general/formats/command.py b/formats/command.py
index 04a49ad..04a49ad 100644
--- a/ext-plugins/lib-general/formats/command.py
+++ b/formats/command.py