Sfoglia il codice sorgente

tools: save ConsoleHistory to file

Daniele Bartolini 5 anni fa
parent
commit
fdea46a357

+ 1 - 0
docs/changelog.rst

@@ -18,6 +18,7 @@ Changelog
 
 * Added the ability to select from a number of templates (samples) when creating new projects
 * Fixed an issue that caused the Level Editor to ask multiple times whether save the level in some circumnstances
+* Level Editor now restores the Console's history from previous sessions
 
 **Lua API**
 

+ 7 - 0
tools/level_editor/level_editor.vala

@@ -460,6 +460,7 @@ public class LevelEditorApplication : Gtk.Application
 
 		load_settings();
 		_user.load(_user_file.get_path());
+		_console_view._entry_history.load(_console_history_file.get_path());
 
 		if (_source_dir == null)
 		{
@@ -1384,6 +1385,7 @@ public class LevelEditorApplication : Gtk.Application
 		// Save editor settings.
 		_user.save(_user_file.get_path());
 		save_settings();
+		_console_view._entry_history.save(_console_history_file.get_path());
 
 		// Destroy widgets.
 		if (_resource_chooser != null)
@@ -2082,10 +2084,12 @@ public static GLib.File _toolchain_dir;
 public static GLib.File _templates_dir;
 public static GLib.File _config_dir;
 public static GLib.File _logs_dir;
+public static GLib.File _cache_dir;
 public static GLib.File _documents_dir;
 public static GLib.File _log_file;
 public static GLib.File _settings_file;
 public static GLib.File _user_file;
+public static GLib.File _console_history_file;
 
 public static GLib.FileStream _log_stream;
 public static ConsoleView _console_view;
@@ -2184,10 +2188,13 @@ public static int main(string[] args)
 	_logs_dir = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "logs"));
 	try { _logs_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
 	_documents_dir = GLib.File.new_for_path(GLib.Environment.get_user_special_dir(GLib.UserDirectory.DOCUMENTS));
+	_cache_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_cache_dir(), "crown"));
+	try { _cache_dir.make_directory(); } catch (Error e) { /* Nobody cares */ }
 
 	_log_file = GLib.File.new_for_path(GLib.Path.build_filename(_logs_dir.get_path(), new GLib.DateTime.now_utc().format("%Y-%m-%d") + ".log"));
 	_settings_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "settings.sjson"));
 	_user_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "user.sjson"));
+	_console_history_file = GLib.File.new_for_path(GLib.Path.build_filename(_cache_dir.get_path(), "console_history.txt"));
 
 	_log_stream = GLib.FileStream.open(_log_file.get_path(), "a");
 

+ 22 - 0
tools/widgets/console_view.vala

@@ -52,6 +52,28 @@ public class EntryHistory
 		else
 			return _data[_capacity - (distance - _index)];
 	}
+
+	public void save(string path)
+	{
+		FileStream fs = FileStream.open(path, "wb");
+		if (fs == null)
+			return;
+
+		uint first_entry = _index + (_capacity - _size);
+		for (uint ii = 0; ii < _size; ++ii)
+			fs.printf("%s\n", _data[(first_entry + ii) % _capacity]);
+	}
+
+	public void load(string path)
+	{
+		FileStream fs = FileStream.open(path, "rb");
+		if (fs == null)
+			return;
+
+		string? line = null;
+		while ((line = fs.read_line()) != null)
+			push(line);
+	}
 }
 
 public class ConsoleView : Gtk.Box