diff --git a/r2ai/bubble.py b/r2ai/bubble.py index 089ab896..1d3d50fc 100644 --- a/r2ai/bubble.py +++ b/r2ai/bubble.py @@ -1,6 +1,8 @@ +"""Bubble chat helper functions to make clippy-ai look nice.""" + import os -clippy_begin=""" +CLIPPY_BEGIN = """ .--. _|_ | O O | @@ -10,29 +12,43 @@ / | `---' .---------------------------------------------------------------' '----""" -clippy_end="""`-----------------------------------------------------------------------""" +CLIPPY_END = """`-----------------------------------------------------------------------""" -user_begin=""" |\\ +USER_BEGIN = """ |\\ | \\ .--' '--""" -user_end='`---' +USER_END = '`---' def query(text): - l = len(text) + 10 - print("\033[F\033[J") - print(user_begin + ("-"*(l-5)) + ".") - pad = " " * (l - len(text)) - print(f"| {text} {pad} |") - print(user_end+ ("-"*l) + "'") + """Display the user text using an ascii-art bubble chat + + Parameters: + text (string): Message to display + + """ + l = len(text) + 10 + print("\033[F\033[J") + print(USER_BEGIN + ("-" * (l - 5)) + ".") + pad = " " * (l - len(text)) + print(f"| {text} {pad} |") + print(USER_END + ("-"*l) + "'") def getpad(): - tw = os.get_terminal_size().columns - 75 - pad = "-" - if tw > 0: - pad = "-" * tw - return pad + """Generate padding with dashes + """ + tw = os.get_terminal_size().columns - 75 + pad = "-" + if tw > 0: + pad = "-" * tw + return pad + def response_begin(): - print(clippy_begin + getpad() + ".") + """Print the beginning of the clippy response + """ + print(CLIPPY_BEGIN + getpad() + ".") + def response_end(): - print(clippy_end + getpad() + "'") + """Print the end of the clippy response + """ + print(CLIPPY_END + getpad() + "'") diff --git a/r2ai/tab.py b/r2ai/tab.py index e0fc9c16..1ce07683 100644 --- a/r2ai/tab.py +++ b/r2ai/tab.py @@ -98,7 +98,7 @@ def display_matches(self, substitution, matches, longest_match_length): def tab_hist(text): if not have_readline: - print("Cannot find readline") + print("Cannot find readline", file=sys.stderr) return False def tab_evals(x): @@ -107,16 +107,19 @@ def tab_evals(x): def tab_write(): if not have_readline: - print("Cannot find readline") + print("Cannot find readline", file=sys.stderr) return False readline.write_history_file(R2AI_HISTFILE) def tab_init(): if not have_readline: - print("Cannot find readline") + print("Cannot find readline", file=sys.stderr) return False completer = MyCompleter(list(set(commands))) - readline.read_history_file(R2AI_HISTFILE) + try: + readline.read_history_file(R2AI_HISTFILE) + except FileNotFoundError: + pass readline.set_completer(completer.complete) readline.set_completer_delims('\t\n;') readline.set_completion_display_matches_hook(completer.display_matches)