## Standard Python libraries.
import errno as E
import fnmatch as FN
+import locale as LC
import optparse as OP
import os as OS
import re as RX
###--------------------------------------------------------------------------
### Eyecandy progress reports.
+DEFAULT_ENCODING = None
+
def charwidth(s):
"""
Return the width of S, in characters.
None of this handles tab characters in any kind of useful way. Sorry.
"""
- ## If there's no encoding for stdout then we're doing something stupid.
- if SYS.stdout.encoding is None: return len(s)
+ global DEFAULT_ENCODING
+
+ ## Figure out the default encoding.
+ if DEFAULT_ENCODING is None: DEFAULT_ENCODING = LC.getpreferredencoding()
## Turn the string into Unicode so we can hack on it properly. Maybe that
## won't work out, in which case fall back to being stupid.
- try: u = s.decode(SYS.stdout.encoding)
+ try: u = s.decode(DEFAULT_ENCODING)
except UnicodeError: return len(s)
## Our main problem is combining characters, but we should also try to