summaryrefslogtreecommitdiffstats
path: root/layman/utils.py
diff options
context:
space:
mode:
authorBrian Dolbec <brian.dolbec@gmail.com>2011-01-16 22:52:52 -0800
committerBrian Dolbec <brian.dolbec@gmail.com>2011-02-11 02:49:14 -0800
commitc551ad652c54eaccc974c1bb87e8c7582adacbae (patch)
tree29c6a99d1d3377ae14e05784d24e91919df47590 /layman/utils.py
parentd7510f1f7ee5c5bec7bdba68a310c1ce17444821 (diff)
downloadlayman-c551ad652c54eaccc974c1bb87e8c7582adacbae.tar.gz
layman-c551ad652c54eaccc974c1bb87e8c7582adacbae.tar.bz2
layman-c551ad652c54eaccc974c1bb87e8c7582adacbae.zip
move utility functions out of the Overlay class
Diffstat (limited to 'layman/utils.py')
-rw-r--r--layman/utils.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/layman/utils.py b/layman/utils.py
index 18bf442..79b3c18 100644
--- a/layman/utils.py
+++ b/layman/utils.py
@@ -30,14 +30,69 @@ __version__ = '$Id: utils.py 236 2006-09-05 20:39:37Z wrobel $'
#-------------------------------------------------------------------------------
import types, re, os
+import sys
+import locale
+import codecs
+
from layman.debug import OUT
+
#===============================================================================
#
# Helper functions
#
#-------------------------------------------------------------------------------
+def decode_selection(selection):
+ '''utility function to decode a list of strings
+ accoring to the filesystem encoding
+ '''
+ enc = sys.getfilesystemencoding()
+ if enc:
+ return [i.decode(enc) for i in selection]
+ return selection
+
+
+def encoder(unicode_text, _encoding_):
+ return codecs.encode(unicode_text, _encoding_, 'replace')
+
+
+def get_encoding(output):
+ if hasattr(output, 'encoding') \
+ and output.encoding != None:
+ return output.encoding
+ else:
+ return locale.getpreferredencoding()
+
+
+def pad(string, length):
+ '''Pad a string with spaces.'''
+ if len(string) <= length:
+ return string + ' ' * (length - len(string))
+ else:
+ return string[:length - 3] + '...'
+
+
+def terminal_width():
+ '''Determine width of terminal window.'''
+ try:
+ width = int(os.environ['COLUMNS'])
+ if width > 0:
+ return width
+ except:
+ pass
+ try:
+ import struct, fcntl, termios
+ query = struct.pack('HHHH', 0, 0, 0, 0)
+ response = fcntl.ioctl(1, termios.TIOCGWINSZ, query)
+ width = struct.unpack('HHHH', response)[1]
+ if width > 0:
+ return width
+ except:
+ pass
+ return 80
+
+
def ensure_unicode(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):