From 555fe97833ba9ba4b07e60aa46bf1bcb45a9eb6b Mon Sep 17 00:00:00 2001 From: LoafyLemon Date: Sat, 18 May 2024 20:17:25 +0100 Subject: [PATCH] Console * Add line recall support * Add skip adjacent repeating commands for recall * Add execution timestamps * Add separation for each consecutive script reload * Fix history --- game/scripts/utility/console.rpy | 74 ++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/game/scripts/utility/console.rpy b/game/scripts/utility/console.rpy index a7243340..3ec5629d 100644 --- a/game/scripts/utility/console.rpy +++ b/game/scripts/utility/console.rpy @@ -1,4 +1,35 @@ -init 2000 python in _console: +default persistent.console_history = None +default persistent.console_line_history = None + +init 1702 python in _console: + import datetime + + class ConsoleHistoryEntry(object): + lines = 0 + + def __init__(self, command, result=None, is_error=False): + self.command = command + self.result = result + self.is_error = is_error + self.timestamp = datetime.datetime.now().timestamp() + + def update_lines(self): + + if self.result is None: + return + + lines = self.result + if len(lines) > config.console_history_lines * 160: + lines = "…" + self.result[-config.console_history_lines * 160:] + + lines = lines.split("\n") + + if len(lines) > config.console_history_lines: + lines = [ "…" ] + lines[-config.console_history_lines:] + + self.result = "\n".join(lines) + self.lines = len(lines) + class DebugConsoleNew(object): def __init__(self): @@ -7,14 +38,14 @@ init 2000 python in _console: self.line_history = BoundedList(config.console_history_size) self.line_index = 0 - if persistent._console_history is not None: - for i in persistent._console_history: + if persistent.console_history is not None: + for i in persistent.console_history: he = ConsoleHistoryEntry(i[0], i[1], i[2]) he.update_lines() self.history.append(he) - if persistent._console_line_history is not None: - self.line_history.extend(persistent._console_line_history) + if persistent.console_line_history is not None: + self.line_history.extend(persistent.console_line_history) self.first_time = True self.renpy_scripting = False @@ -22,15 +53,16 @@ init 2000 python in _console: self.reset() def backup(self): - - persistent._console_history = [ (i.command, i.result, i.is_error) for i in self.history ] - persistent._console_line_history = list(self.line_history) + persistent.console_history = [ (i.command, i.result, i.is_error) for i in self.history ] + persistent.console_line_history = list(self.line_history) def start(self): he = ConsoleHistoryEntry(None) if self.first_time: - message = __("Press to exit console. Type help for help.\n") + current_time = datetime.datetime.now() + + message = ("-" * 50) + " RESTART OCCURED " + ("-" * 50) he.result = message he.update_lines() self.history.append(he) @@ -42,9 +74,6 @@ init 2000 python in _console: self.renpy_scripting = False def reset(self): - - # The list of lines that have been entered by the user, but not yet - # processed. self.lines = [ "" ] self.line_index = len(self.line_history) @@ -58,6 +87,10 @@ init 2000 python in _console: if self.line_index > len(self.line_history): self.line_index = len(self.line_history) + # Skip adjacent duplicates + while self.line_index > 0 and self.line_index < len(self.line_history) and self.line_history[self.line_index] == self.line_history[self.line_index - 1]: + self.line_index += offset + if self.line_index == len(self.line_history): self.lines = [ "" ] else: @@ -67,6 +100,7 @@ init 2000 python in _console: scope = cs.scope consoleinput = scope["consoleinput"] + consoleinput.caret_pos = 0 consoleinput.update_text(self.lines[-1], consoleinput.editable) renpy.restart_interaction() @@ -120,6 +154,7 @@ init 2000 python in _console: he = ConsoleHistoryEntry(code) self.history.append(he) + self.line_history.append(lines) try: @@ -251,12 +286,14 @@ init 2000 python in _console: matching = find_matching_entries(labels, partial) raise Exception("Matching labels:\n %s" % matching) + renpy.config.at_exit_callbacks.append(console.backup) + init python in _console: import pygame import re def enter(): - if console is None or renpy.in_rollback(): + if console is None: return console.start() @@ -322,7 +359,7 @@ init python in _console: if not self.changed: self.caret_pos = 0 - self.update_text("", self.editable) + self.update_text(" ", self.editable) widget = renpy.get_widget("console", "consolevp") widget.yadjustment._value = 9999999 console.run([content]) @@ -533,14 +570,17 @@ screen console: for entry in history: + $ timestamp = datetime.datetime.fromtimestamp(entry.timestamp).strftime('%H:%M:%S') + $ timestamp = f"{{size=-2}}{{color=#ffffff80}}{timestamp}{{/color}}{{/size}}" + if entry.command is not None: - text "[entry.command!q]" style "console_command_text" + text "[timestamp!i] [entry.command!q]" style "console_command_text" if entry.result is not None: if entry.is_error: - text "[entry.result!q]" style "console_error_text" + text "[timestamp!i] {unicode}➥{/unicode} [entry.result!q]" style "console_error_text" else: - text "{unicode}➥{/unicode} [entry.result!q]" style "console_result_text" + text "[timestamp!i] {unicode}➥{/unicode} [entry.result!q]" style "console_result_text" hbox: text "Command:"