From 3cc97accfc5a2298eb969964187e98b08aa13df4 Mon Sep 17 00:00:00 2001 From: Asmageddon Date: Wed, 30 May 2012 15:28:57 +0200 Subject: [PATCH] Added saving and loading legacy history. Optional, enabled by default --- deluge/ui/console/modes/alltorrents.py | 3 +- deluge/ui/console/modes/legacy.py | 79 ++++++++++++++++++++- deluge/ui/console/modes/preference_panes.py | 1 + 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/deluge/ui/console/modes/alltorrents.py b/deluge/ui/console/modes/alltorrents.py index 5474458a5..c4bcc3bca 100644 --- a/deluge/ui/console/modes/alltorrents.py +++ b/deluge/ui/console/modes/alltorrents.py @@ -176,7 +176,8 @@ DEFAULT_PREFS = { "sort_primary": "queue", "sort_secondary": "name", "separate_complete": True, - "ring_bell": False + "ring_bell": False, + "save_legacy_history": True } column_pref_names = ["queue","name","size","state", diff --git a/deluge/ui/console/modes/legacy.py b/deluge/ui/console/modes/legacy.py index b6126fbf7..b86c247c5 100644 --- a/deluge/ui/console/modes/legacy.py +++ b/deluge/ui/console/modes/legacy.py @@ -45,6 +45,7 @@ import deluge.ui.console.colors as colors from twisted.internet import defer, reactor from deluge.ui.client import client import deluge.component as component +import deluge.configmanager from deluge.ui.console.modes import format_utils strwidth = format_utils.strwidth @@ -57,6 +58,8 @@ import re LINES_BUFFER_SIZE = 5000 INPUT_HISTORY_SIZE = 500 +MAX_HISTFILE_SIZE = 2000 + def complete_line(line, possible_matches): "Find the common prefix of possible matches, proritizing matching-case elements" @@ -113,7 +116,6 @@ class Legacy(BaseMode, component.Component): component.Component.__init__(self, "LegacyUI") self.batch_write = False - self.lines = [] # A list of strings to be displayed based on the offset (scroll) self.lines = [] @@ -137,6 +139,56 @@ class Legacy(BaseMode, component.Component): self.console_config = component.get("AllTorrents").config + #To avoid having to truncate the file every time we're writing + # or doing it on exit(and therefore relying on an error-less + # or in other words clean exit, we're going to have two files + # that we swap around based on length + config_dir = deluge.configmanager.get_config_dir() + self.history_file = [ + os.path.join(config_dir, "legacy.hist1"), + os.path.join(config_dir, "legacy.hist2") + ] + self._hf_lines = [0, 0] + + if self.console_config["save_legacy_history"]: + try: + lines1 = open(self.history_file[0], 'r').read().splitlines() + self._hf_lines[0] = len(lines1) + except: + lines1 = [] + self._hf_lines[0] = 0 + + try: + lines2 = open(self.history_file[1], 'r').read().splitlines() + self._hf_lines[1] = len(lines2) + except: + lines2 = [] + self._hf_lines[1] = 0 + + #The non-full file is the active one + if self._hf_lines[0] > self._hf_lines[1]: + self.lines = lines1 + lines2 + else: + self.lines = lines2 + lines1 + + if len(self.lines) > MAX_HISTFILE_SIZE: + self.lines = self.lines[-MAX_HISTFILE_SIZE:] + + #Instead of having additional input history file, we can + # simply scan for lines beginning with ">>> " + for line in self.lines: + line = format_utils.remove_formatting(line) + if line.startswith(">>> "): + self.input_history.append( line[4:] ) + + self.input_history_index = len(self.input_history) + + #if len(self.lines) >= 5: + #if any(self.lines[-5:]): + #for i in range(5): + #self.add_line(" ", False) + + # show the cursor curses.curs_set(2) @@ -398,6 +450,30 @@ class Legacy(BaseMode, component.Component): """ + if self.console_config["save_legacy_history"]: + #Determine which file is the active one + #If both are under maximum, it's first, otherwise it's the one not full + if self._hf_lines[0] < MAX_HISTFILE_SIZE and self._hf_lines[1] < MAX_HISTFILE_SIZE: + active_file = 0 + if self._hf_lines[0] > self._hf_lines[1]: + active_file = 1 + else: + active_file = 0 + + #Write the line + f = open(self.history_file[active_file], 'a') + f.write( text + os.linesep ) + + #And increment line counter + self._hf_lines[active_file] += 1 + + #If the active file reaches max size, we truncate it + # therefore swapping the currently active file + if self._hf_lines[active_file] == MAX_HISTFILE_SIZE: + self._hf_lines[1 - active_file] = 0 + f = open(self.history_file[1 - active_file], 'w') + f.truncate(0) + def get_line_chunks(line): """ Returns a list of 2-tuples (color string, text) @@ -573,6 +649,7 @@ class Legacy(BaseMode, component.Component): :param line: str, the line to print """ + self.add_line(line, not self.batch_write) diff --git a/deluge/ui/console/modes/preference_panes.py b/deluge/ui/console/modes/preference_panes.py index e85d0476d..6a914d19b 100644 --- a/deluge/ui/console/modes/preference_panes.py +++ b/deluge/ui/console/modes/preference_panes.py @@ -318,6 +318,7 @@ class InterfacePane(BasePane): self.add_checked_input("third_tab_lists_all","Third tab lists all remaining torrents in legacy mode",parent.console_config["third_tab_lists_all"]) self.add_int_spin_input("torrents_per_tab_press","Torrents per tab press",parent.console_config["torrents_per_tab_press"], 5, 100) self.add_checked_input("ring_bell","Ring system bell when a download finishes",parent.console_config["ring_bell"]) + self.add_checked_input("save_legacy_history","Store and load command line history in Legacy mode",parent.console_config["save_legacy_history"]) self.add_header("Columns To Display", True)