New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the output #4420

Merged
merged 1 commit into from Jul 19, 2017
Jump to file or symbol
Failed to load files and symbols.
+115 −19
Diff settings

Always

Just for now

CLI: Fix output

Add colors for letters
Add numbers as input
  • Loading branch information...
Nosferatul committed Jun 27, 2017
commit 0eb0be9e7bf4765dd829d0aeb5a6918fb00d05e1
@@ -1,5 +1,6 @@
import logging
import platform
import os
from termcolor import colored
@@ -88,6 +89,25 @@ def highlight_text(no_color, text, lexer=TextLexer(), style=None):
DIFF_EXCERPT_MAX_SIZE = 4
def color_letter(console_printer, line):
x = -1
y = -1
letter = ''
for i, l in enumerate(line, 0):
if line[i] == '(':
x = i
if line[i] == ')':
y = i
if l.isupper() and x != -1:
letter = l
first_part = line[:x+1]
second_part = line[y:]
console_printer.print(first_part, end='')
console_printer.print(letter, color='blue', end='')
console_printer.print(second_part)
def format_lines(lines, symbol='', line_nr=''):
def sym(x): return ']' if x is '[' else x
return '\n'.join('{}{:>5}{} {}'.format(symbol, sym(symbol), line_nr, line)
@@ -181,6 +201,9 @@ def print_lines(console_printer,
no_color = not console_printer.print_colored
for i in range(sourcerange.start.line, sourcerange.end.line + 1):
# Print affected file's line number in the sidebar.
console_printer.print(format_lines(lines='', line_nr=i, symbol='['),
color=FILE_LINES_COLOR,
end='')
line = file_dict[sourcerange.file][i - 1].rstrip('\n')
try:
@@ -205,7 +228,11 @@ def print_lines(console_printer,
lexer, BackgroundSourceRangeStyle), end='')
console_printer.print(highlight_text(
no_color, line[sourcerange.end.column - 1:], lexer), end='')
no_color, line[sourcerange.end.column - 1:], lexer), end='')
console_printer.print('')
else:
console_printer.print(highlight_text(
no_color, line[printed_chars:], lexer), end='')
console_printer.print('')
@@ -454,6 +481,8 @@ def print_affected_lines(console_printer, file_dict, sourcerange):
:param sourcerange: The SourceRange object referring to the related
lines to print.
"""
console_printer.print('\n' + os.path.relpath(sourcerange.file),
color=FILE_NAME_COLOR)
if sourcerange.start.line is not None:
if len(file_dict[sourcerange.file]) < sourcerange.end.line:
@@ -561,11 +590,10 @@ def choose_action(console_printer, actions):
:return: Return choice of action of user.
"""
while True:
console_printer.print(format_lines('Do (N)othing',
symbol='['))
color_letter(console_printer, '[ ] *0. Do (N)othing')
for i, action in enumerate(actions, 1):
console_printer.print(format_lines('{}'.format(
action.desc), symbol='['))
color_letter(console_printer, format_lines('{:>2}. {}'.format(
i, action.desc), symbol='['))
line = format_lines(STR_ENTER_NUMBER, symbol='[')
@@ -581,6 +609,11 @@ def choose_action(console_printer, actions):
for i, action in enumerate(actions, 1):
if choice in action.desc:
return i
elif choice.isnumeric():
choice = int(choice)
print(choice)
if 0 <= choice <= len(actions):
return choice
console_printer.print(format_lines(
'Please enter a valid letter.', symbol='['))
@@ -2,7 +2,7 @@
import unittest
from unittest.mock import patch
from collections import OrderedDict
from os.path import abspath
from os.path import abspath, relpath
import logging
from pyprint.ConsolePrinter import ConsolePrinter
@@ -352,7 +352,7 @@ def test_print_affected_files(self):
'message',
affected_code=affected_code),
file_dict)
self.assertEqual(stdout.getvalue(), '')
self.assertEqual(stdout.getvalue(), '\n'+relpath(some_file)+'\n')
def test_acquire_actions_and_apply(self):
with make_temp() as testfile_path:
@@ -502,6 +502,33 @@ def test_default_input(self):
with simulate_console_inputs('') as generator:
self.assertFalse(ask_for_action_and_apply(*args))
def test_default_input2(self):
action = TestAction()
args = [self.console_printer, Section(''),
[action.get_metadata()], {'TestAction': action},
set(), Result('origin', 'message'), {}, {}]
with simulate_console_inputs(1, 1) as generator:
self.assertTrue(ask_for_action_and_apply(*args))
def test_default_input3(self):
action = TestAction()
args = [self.console_printer, Section(''),
[action.get_metadata()], {'TestAction': action},
set(), Result('origin', 'message'), {}, {}]
with simulate_console_inputs(1, 'a') as generator:
self.assertTrue(ask_for_action_and_apply(*args))
def test_default_input4(self):
action = TestAction()
args = [self.console_printer, Section(''),
[action.get_metadata()], {'TestAction': action},
set(), Result('origin', 'message'), {}, {}]
with simulate_console_inputs(5, 0) as generator:
self.assertFalse(ask_for_action_and_apply(*args))
def test_print_result_no_input(self):
with make_temp() as testfile_path:
file_dict = {testfile_path: ['1\n', '2\n', '3\n']}
@@ -576,11 +603,16 @@ def test_print_results_for_file(self):
{},
self.console_printer)
self.assertEqual("""
filename
[ ]2 {0}
**** SpaceConsistencyBear [Section: ] ****
! ! [Severity: NORMAL]
! ! {}\n""".format(highlight_text(self.no_color, 'Trailing whitespace found',
style=BackgroundMessageStyle)),
! ! {1}\n""".format(highlight_text(self.no_color, 'line 2', self.lexer),
highlight_text(self.no_color,
'Trailing whitespace found',
style=BackgroundMessageStyle), ''),
stdout.getvalue())
with retrieve_stdout() as stdout:
@@ -599,11 +631,16 @@ def test_print_results_for_file(self):
{},
self.console_printer)
self.assertEqual("""
filename
[ ]5 {0}
**** SpaceConsistencyBear [Section: ] ****
! ! [Severity: NORMAL]
! ! {}\n""".format(highlight_text(self.no_color, 'Trailing whitespace found',
style=BackgroundMessageStyle)),
! ! {1}\n""".format(highlight_text(self.no_color, 'line 5', self.lexer),
highlight_text(self.no_color,
'Trailing whitespace found',
style=BackgroundMessageStyle), ''),
stdout.getvalue())
def test_print_results_sorting(self):
@@ -627,18 +664,25 @@ def test_print_results_sorting(self):
self.console_printer)
self.assertEqual("""
file
[ ]2 {0}
**** SpaceConsistencyBear [Section: ] ****
! ! [Severity: NORMAL]
! ! {1}
! ! Trailing whitespace found
file
[ ]5 {2}
**** SpaceConsistencyBear [Section: ] ****
! ! [Severity: NORMAL]
! ! {1}\n""".format(highlight_text(self.no_color, '\t', self.lexer),
highlight_text(self.no_color,
'Trailing whitespace found',
style=BackgroundMessageStyle)),
style=BackgroundMessageStyle),
highlight_text(self.no_color, 'line 5\t', self.lexer)),
stdout.getvalue())
def test_print_results_multiple_ranges(self):
@@ -659,18 +703,30 @@ def test_print_results_multiple_ranges(self):
for i in range(10)]},
{},
self.console_printer)
self.assertEqual("""li{0}{1}
li{0}{2}
self.assertEqual("""
another_file
[ ]1 li{0}{1}
another_file
[ ]3 li{0}{2}
some_file
[ ]5 li{0}{3}
[ ]6 li{0}{4}
[ ]7 li{0}{5}
**** ClangCloneDetectionBear [Section: ] ****
! ! [Severity: NORMAL]
! ! {3}\n""".format(highlight_text(self.no_color, 'ne', self.lexer,
! ! {6}\n""".format(highlight_text(self.no_color, 'ne', self.lexer,
BackgroundSourceRangeStyle),
highlight_text(self.no_color, ' 1', self.lexer),
highlight_text(self.no_color, ' 3', self.lexer),
highlight_text(self.no_color, ' 5', self.lexer),
highlight_text(self.no_color, ' 6', self.lexer),
highlight_text(self.no_color, ' 7', self.lexer),
highlight_text(self.no_color, 'Clone Found',
style=BackgroundMessageStyle)),
style=BackgroundMessageStyle), ' '),
stdout.getvalue())
def test_print_results_missing_file(self):
@@ -708,10 +764,15 @@ def test_print_results_missing_line(self):
{abspath('file'): ['line ' + str(i + 1) for i in range(5)]},
{},
self.console_printer)
self.assertEqual('\n'
self.assertEqual(
'\nfile\n'
'[ ]5 {0}\n'
'\n'
'**** t [Section: ] ****\n\n'
'! ! [Severity: NORMAL]\n'
'! ! {1}\n'
'\n'
'file\n'
'! !6 {2}'
'\n\n'
'**** t [Section: ] ****\n\n'
@@ -721,7 +782,7 @@ def test_print_results_missing_line(self):
'line 5', self.lexer),
highlight_text(self.no_color, 'msg',
style=BackgroundMessageStyle),
STR_LINE_DOESNT_EXIST),
STR_LINE_DOESNT_EXIST, ' '),
stdout.getvalue())
def test_print_results_without_line(self):
@@ -734,6 +795,8 @@ def test_print_results_without_line(self):
{},
self.console_printer)
self.assertEqual(
'\n'
'file\n'
'\n**** t [Section: ] ****\n\n'
'! ! [Severity: NORMAL]\n'
'! ! {}\n'.format(highlight_text(
ProTip! Use n and p to navigate between commits in a pull request.