X Tutup
Skip to content

Commit a5e541b

Browse files
committed
Detect if Unicode box characters are supported in user's locale (fixes 295)
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent d251064 commit a5e541b

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

bpython/cli.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
from bpython import importcompletion
7373

7474
# This for config
75-
from bpython.config import Struct
75+
from bpython.config import Struct, getpreferredencoding
7676

7777
# This for keys
7878
from bpython.keys import cli_key_dispatch as key_dispatch
@@ -98,9 +98,6 @@
9898
# ---
9999

100100

101-
def getpreferredencoding():
102-
return locale.getpreferredencoding() or sys.getdefaultencoding()
103-
104101
def calculate_screen_lines(tokens, width, cursor=0):
105102
"""Given a stream of tokens and a screen width plus an optional
106103
initial cursor position, return the amount of needed lines on the

bpython/config.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from __future__ import with_statement
44
import os
55
import sys
6+
import locale
67
from ConfigParser import ConfigParser
78
from itertools import chain
89
from bpython.keys import cli_key_dispatch as key_dispatch
910
from bpython.autocomplete import SIMPLE as default_completion
10-
1111
import bpython.autocomplete
1212

1313

@@ -16,6 +16,21 @@ class Struct(object):
1616
to and use for various arbitrary things."""
1717

1818

19+
def getpreferredencoding():
20+
"""Get the user's preferred encoding."""
21+
return locale.getpreferredencoding() or sys.getdefaultencoding()
22+
23+
24+
def supports_box_chars():
25+
"""Check if the encoding suppors Unicode box characters."""
26+
try:
27+
for c in (u'│', u'│', u'─', u'─', u'└', u'┘', u'┌', u'┐'):
28+
c.encode(getpreferredencoding())
29+
return True
30+
except UnicodeEncodeError:
31+
return False
32+
33+
1934
def get_config_home():
2035
"""Returns the base directory for bpython's configuration files."""
2136
xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '~/.config')
@@ -256,7 +271,7 @@ def get_key_no_doublebind(command):
256271
struct.autocomplete_mode = default_completion
257272

258273
# set box drawing characters
259-
if config.getboolean('general', 'unicode_box'):
274+
if config.getboolean('general', 'unicode_box') and supports_box_chars():
260275
struct.left_border = u'│'
261276
struct.right_border = u'│'
262277
struct.top_border = u'─'

bpython/inspection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License
22
#
3-
# Copyright (c) 2009-2011 the bpython authors.
3+
# Copyright (c) 2009-2015 the bpython authors.
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)
X Tutup