Skip to content

Commit

Permalink
ConsoleInteractor: Strip to 79 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
sils committed Apr 15, 2015
1 parent 3f408af commit e7ea269
Showing 1 changed file with 57 additions and 28 deletions.
85 changes: 57 additions & 28 deletions coalib/output/ConsoleInteractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@


class ConsoleInteractor(Interactor, ConsolePrinter):
STR_GET_VAL_FOR_SETTING = _("Please enter a value for the setting \"{}\" ({}) needed by {}: ")
STR_LINE_DOESNT_EXIST = _("The line belonging to the following result cannot be printed because it refers to a "
"line that doesn't seem to exist in the given file.")
STR_GET_VAL_FOR_SETTING = _("Please enter a value for the setting \"{}\" "
"({}) needed by {}: ")
STR_LINE_DOESNT_EXIST = _("The line belonging to the following result "
"cannot be printed because it refers to a line "
"that doesn't seem to exist in the given file.")
STR_PROJECT_WIDE = _("Project wide:")

def __init__(self,
Expand All @@ -31,7 +33,8 @@ def __init__(self,

def acquire_settings(self, settings_names_dict):
if not isinstance(settings_names_dict, dict):
raise TypeError("The settings_names_dict parameter has to be a dictionary.")
raise TypeError("The settings_names_dict parameter has to be a "
"dictionary.")

result = {}
for setting_name, arr in settings_names_dict.items():
Expand All @@ -52,22 +55,28 @@ def _require_setting(self, setting_name, arr):

if len(arr) == 2:
needed = arr[1]
else: # Translators: this is the and that connects the last two items of an enumeration (1st, 2nd AND 3rd)
else:
needed = ", ".join(arr[1:-1]) + _(" and ") + arr[-1]

return input(self.STR_GET_VAL_FOR_SETTING.format(str(setting_name),
str(arr[0]),
needed))

def _format_line(self, line, real_nr="", sign="|", mod_nr="", symbol="", ):
return "|{:>4}{}{:>4}|{:1}{}".format(real_nr, sign, mod_nr, symbol, line.rstrip("\n"))
return "|{:>4}{}{:>4}|{:1}{}".format(real_nr,
sign,
mod_nr,
symbol,
line.rstrip("\n"))

def _print_result(self, result):
message_string_list = "[{sev}] {bear}:\n{msg}".format(sev=RESULT_SEVERITY.__str__(result.severity),
bear=result.origin,
msg=result.message).split("\n")
message_string_list = "[{sev}] {bear}:\n{msg}".format(
sev=RESULT_SEVERITY.__str__(result.severity),
bear=result.origin,
msg=result.message).split("\n")

return self.print("\n".join([self._format_line(line) for line in message_string_list]))
return self.print("\n".join([self._format_line(line)
for line in message_string_list]))

def _print_actions(self, actions):
self.print(self._format_line(
Expand All @@ -84,7 +93,8 @@ def _print_actions(self, actions):
def _choose_action(self, actions):
while True:
for i, action in enumerate(actions):
self.print(self._format_line("{:>2}: {}".format(i + 1, action.desc)))
self.print(self._format_line("{:>2}: {}".format(i + 1,
action.desc)))

try:
line = self._format_line(_("Please enter the number of the "
Expand Down Expand Up @@ -116,56 +126,75 @@ def _get_action_info(self, action):
return action.name, self.current_section

def _print_segregation(self):
self.print(self._format_line(line="", real_nr="...", sign="|", mod_nr="..."))
self.print(self._format_line(line="",
real_nr="...",
sign="|",
mod_nr="..."))

def _print_lines(self, file_dict, current_line, result_line, result_file):
"""
Prints the lines between the current and the result line. If needed they will be shortened.
Prints the lines between the current and the result line. If needed
they will be shortened.
"""
line_delta = result_line - current_line

if line_delta > self.pre_padding:
self._print_segregation()

for i in range(max(result_line - self.pre_padding, 1), result_line + 1):
self.print(self._format_line(line=file_dict[result_file][i - 1],
real_nr=i,
mod_nr=i))
for i in range(max(result_line - self.pre_padding, 1),
result_line + 1):
self.print(self._format_line(
line=file_dict[result_file][i - 1],
real_nr=i,
mod_nr=i))
else:
for i in range(1, line_delta + 1):
self.print(self._format_line(line=file_dict[result_file][current_line + i - 1],
real_nr=current_line + i,
mod_nr=current_line + i))
self.print(self._format_line(
line=file_dict[result_file][current_line + i - 1],
real_nr=current_line + i,
mod_nr=current_line + i))

def print_results(self, result_list, file_dict):
if not isinstance(result_list, list):
raise TypeError("result_list should be of type list")
if not isinstance(file_dict, dict):
raise TypeError("file_dict should be of type dict")

current_file = False # We can't use None since we need line 109 be executed if file of first result is None
# We can't use None since we need line 109 be executed if file of first
# result is None
current_file = False
current_line = 0

for result in sorted(result_list):
if result.file != current_file:
if result.file in file_dict or result.file is None:
current_file = result.file
current_line = 0
self.print("\n\n{}".format(current_file if current_file is not None else self.STR_PROJECT_WIDE))
self.print("\n\n{}".format(current_file
if current_file is not None
else self.STR_PROJECT_WIDE))
else:
self.log_printer.warn(_("A result ({}) cannot be printed because it refers to a file that doesn't"
" seem to exist.").format(str(result)))
self.log_printer.warn(_("A result ({}) cannot be printed "
"because it refers to a file that "
"doesn't seem to "
"exist.").format(str(result)))
continue

if result.line_nr is not None:
if current_file is None:
raise AssertionError("A result with a line_nr should also have a file.")
raise AssertionError("A result with a line_nr should also "
"have a file.")
if result.line_nr < current_line: # pragma: no cover
raise AssertionError("The sorting of the results doesn't work correctly.")
raise AssertionError("The sorting of the results doesn't "
"work correctly.")
if len(file_dict[result.file]) < result.line_nr - 1:
self.print(self._format_line(line=self.STR_LINE_DOESNT_EXIST))
self.print(self._format_line(
line=self.STR_LINE_DOESNT_EXIST))
else:
self._print_lines(file_dict, current_line, result.line_nr, result.file)
self._print_lines(file_dict,
current_line,
result.line_nr,
result.file)
current_line = result.line_nr

self.print_result(result, file_dict)
Expand Down

0 comments on commit e7ea269

Please sign in to comment.