Coding style.

Removed the Code widget which was almost the same as the Text widget.
This commit is contained in:
Andrew Hamilton 2016-02-18 19:37:31 +00:00
parent 6eb64745b9
commit 1a12f6e369
13 changed files with 46 additions and 94 deletions

1
TODO
View file

@ -10,6 +10,7 @@ Todo (tool related)
- Report on python doctests. (also coverage of)
- Cache tools._python_version.
- Add bandit tool for python.
- Recognize python test files like: test_*.py
Done

View file

@ -341,48 +341,6 @@ class Table:
for y, row in enumerate(appearances)])
def _parse_rgb(hex_rgb):
if hex_rgb.startswith("#"):
hex_rgb = hex_rgb[1:]
return tuple(eval("0x"+hex_rgb[index:index+2]) for index in [0, 2, 4])
def _char_style_for_token_type(token_type, pygment_style):
token_style = pygment_style.style_for_token(token_type)
fg_color = (None if token_style["color"] is None
else _parse_rgb(token_style["color"]))
bg_color = (None if token_style["bgcolor"] is None
else _parse_rgb(token_style["bgcolor"]))
return termstr.CharStyle(fg_color, bg_color, token_style["bold"],
token_style["italic"], token_style["underline"])
def _pygments_to_termstr(tokens, pygment_style):
return termstr.TermStr("").join(
termstr.TermStr(text, _char_style_for_token_type(
token_type, pygment_style))
for token_type, text in tokens)
class Code:
def __init__(self, tokens, style):
code = _pygments_to_termstr(tokens, style).split("\n")
max_width = max(len(line) for line in code)
height = len(code)
# bg_color = _parse_rgb(style.background_color)
# bg_style = termstr.CharStyle(1, bg_color)
# pad_char = termstr.TermStr(" ", bg_style)
pad_char = " "
self.code = appearance_resize(code, (max_width, height), pad_char)
def appearance_min(self):
return self.code
def appearance(self, dimensions):
return appearance_resize(self.appearance_min(), dimensions)
class Border:
THIN = ["", "", "", "", "", "", "", ""]

View file

@ -8,10 +8,6 @@ import unittest
import fill3 as fill
# import pygments.lexers
# import pygments.styles.default
# import pygments.styles.emacs
class WidgetTests(unittest.TestCase):
@ -68,18 +64,6 @@ class WidgetTests(unittest.TestCase):
"│abcdef│\n"
"└──────┘")
# def test_pygments_widget(self):
# text = "print('hello world')"
# tokens = pygments.lex(text, pygments.lexers.PythonLexer())
# code = fill.Code(tokens, pygments.styles.default.DefaultStyle)
# self.assert_string(code.appearance_min(),
# "\x1b[1m\x1b[38;2;0;128;0m\x1b[48;2;248;248;248m"
# "print\x1b(B\x1b[m\x1b[315m\x1b[48;2;248;248;248m"
# "(\x1b(B\x1b[m\x1b[38;2;186;33;33m\x1b[48;2;248;248;248m"
# "'hello world'\x1b(B\x1b[m\x1b[315m\x1b[48;2;248;248;248m"
# ")\x1b(B\x1b[m\x1b[315m\x1b[40m\n\x1b(B\x1b[m\x1b[31m"
# "\x1b[48;2;248;248;248m \x1b[0m")
def test_placeholder_widget(self):
placeholder = fill.Placeholder(self.TEXT_A)
self.assert_string(placeholder.appearance_min(), "A")

View file

@ -5,5 +5,4 @@
int main()
{
 cout << "Hello World" << endl;
}

} 

View file

@ -1,3 +1,2 @@
def hi():
 print("hi")

 print("hi")

View file

@ -1,3 +1,2 @@
#!/usr/bin/perl
print "Hello, world!\n";

print "Hello, world!\n";

View file

@ -1,2 +1,2 @@
def hi():
def hi():


View file

@ -1,2 +1,2 @@
def hi():
def hi():


View file

@ -1,2 +1,2 @@




View file

@ -4,5 +4,4 @@
{
 printf("Hello World\n");
 return 0;
}

} 

View file

@ -5,5 +5,4 @@
int main()
{
 cout << "Hello World" << endl;
}

} 

View file

@ -3,5 +3,4 @@
void hello();
#endif

#endif 

View file

@ -123,11 +123,30 @@ def _run_command(command, status_text=Status.ok):
return status, fill3.Text(_fix_input(output))
def _syntax_highlight_code(text, path):
def _syntax_highlight(text, lexer, style, pad_char=" "):
def _parse_rgb(hex_rgb):
if hex_rgb.startswith("#"):
hex_rgb = hex_rgb[1:]
return tuple(eval("0x"+hex_rgb[index:index+2]) for index in [0, 2, 4])
def _char_style_for_token_type(token_type):
token_style = style.style_for_token(token_type)
fg_color = (None if token_style["color"] is None
else _parse_rgb(token_style["color"]))
bg_color = (None if token_style["bgcolor"] is None
else _parse_rgb(token_style["bgcolor"]))
return termstr.CharStyle(fg_color, bg_color, token_style["bold"],
token_style["italic"],
token_style["underline"])
text = fill3.join("",
[termstr.TermStr(text, _char_style_for_token_type(token_type))
for token_type, text in pygments.lex(text, lexer)])
return fill3.Text(text, pad_char=pad_char)
def _syntax_highlight_using_path(text, path):
lexer = pygments.lexers.get_lexer_for_filename(path, text)
tokens = pygments.lex(text, lexer)
native_style = pygments.styles.get_style_by_name("native")
return fill3.Code(tokens, native_style)
return _syntax_highlight(text, lexer, native_style)
def pygments_(path):
@ -138,7 +157,8 @@ def pygments_(path):
return Status.not_applicable, fill3.Text("Not unicode")
else:
try:
source_widget = _syntax_highlight_code(_fix_input(text), path)
source_widget = _syntax_highlight_using_path(_fix_input(text),
path)
except pygments.util.ClassNotFound:
return Status.normal, fill3.Text(text)
return Status.normal, source_widget
@ -279,11 +299,10 @@ def python_unittests(path):
python_version = _python_version(path)
cmd = [path] if _has_shebang_line(path) else [python_version, path]
stdout, stderr, returncode = _do_command(cmd, timeout=TIMEOUT)
markup = pygments.lex(stderr, _python_console_lexer)
status = Status.ok if returncode == 0 else Status.problem
native_style = pygments.styles.get_style_by_name("native")
code = fill3.Code(markup, native_style)
return status, code
return status, _syntax_highlight(stderr, _python_console_lexer,
native_style)
else:
return Status.not_applicable, fill3.Text("No tests.")
python_unittests.dependencies = {"python", "python3"}
@ -359,7 +378,7 @@ pylint.dependencies = {"pylint", "pylint3"}
def python_gut(path):
with open(path) as module_file:
output = gut.gut_module(module_file.read())
source_widget = _syntax_highlight_code(_fix_input(output), path)
source_widget = _syntax_highlight_using_path(_fix_input(output), path)
return Status.normal, source_widget
python_gut.dependencies = set()
@ -396,7 +415,7 @@ python_mccabe.dependencies = {"python-mccabe", "python3-mccabe"}
def python_tidy(path): # Deps: found on internet?
stdout, *rest = _do_command(["python", "python-tidy.py", path])
return Status.normal, _syntax_highlight_code(stdout, path)
return Status.normal, _syntax_highlight_using_path(stdout, path)
def disassemble_pyc(path):
@ -428,7 +447,7 @@ perldoc.dependencies = {"perl-doc"}
def perltidy(path):
stdout, *rest = _do_command(["perltidy", "-st", path])
return Status.normal, _syntax_highlight_code(stdout, path)
return Status.normal, _syntax_highlight_using_path(stdout, path)
perltidy.dependencies = {"perltidy"}
@ -526,8 +545,7 @@ html2text.dependencies = {"html2text"}
def bcpp(path):
stdout, stderr, returncode = _do_command(["bcpp", "-fi", path])
status = Status.normal if returncode == 0 else Status.problem
source_widget = _syntax_highlight_code(stdout, path)
return status, source_widget
return status, _syntax_highlight_using_path(stdout, path)
bcpp.dependencies = {"bcpp"}
@ -540,8 +558,7 @@ def uncrustify(path):
stdout, stderr, returncode = _do_command(
["uncrustify", "-c", config_path, "-f", path])
status = Status.normal if returncode == 0 else Status.problem
source_widget = _syntax_highlight_code(stdout, path)
return status, source_widget
return status, _syntax_highlight_using_path(stdout, path)
uncrustify.dependencies = {"uncrustify"}
@ -744,11 +761,9 @@ def run_tool_no_error(path, tool):
except subprocess.TimeoutExpired:
status, result = Status.timed_out, fill3.Text("Timed out")
except:
# Maybe use code.InteractiveInterpreter.showtraceback() ?
tokens = pygments.lex(traceback.format_exc(),
_get_python_traceback_lexer())
native_style = pygments.styles.get_style_by_name("native")
status, result = Status.error, fill3.Code(tokens, native_style)
status, result = Status.error, _syntax_highlight(
traceback.format_exc(), _get_python_traceback_lexer(),
pygments.styles.get_style_by_name("native"))
return status, result