diff options
author | Jak Daniels | 2014-09-22 16:54:12 +0100 |
---|---|---|
committer | BlueWall | 2014-09-23 10:36:00 -0400 |
commit | bde60cc92e0966177a1aeb03dce3183a92b7a38c (patch) | |
tree | de52febcc9eeac28c8637a140abc862243c39510 /OpenSim/Framework/Console/LocalConsole.cs | |
parent | Modifications to previous IAR commits to bring them more inline with existing... (diff) | |
download | opensim-SC_OLD-bde60cc92e0966177a1aeb03dce3183a92b7a38c.zip opensim-SC_OLD-bde60cc92e0966177a1aeb03dce3183a92b7a38c.tar.gz opensim-SC_OLD-bde60cc92e0966177a1aeb03dce3183a92b7a38c.tar.bz2 opensim-SC_OLD-bde60cc92e0966177a1aeb03dce3183a92b7a38c.tar.xz |
Add persistent command history in console
Signed-off-by: BlueWall <jamesh@bluewallgroup.com>
Diffstat (limited to 'OpenSim/Framework/Console/LocalConsole.cs')
-rw-r--r-- | OpenSim/Framework/Console/LocalConsole.cs | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/OpenSim/Framework/Console/LocalConsole.cs b/OpenSim/Framework/Console/LocalConsole.cs index 260a86f..28293c0 100644 --- a/OpenSim/Framework/Console/LocalConsole.cs +++ b/OpenSim/Framework/Console/LocalConsole.cs | |||
@@ -32,6 +32,8 @@ using System.Reflection; | |||
32 | using System.Text; | 32 | using System.Text; |
33 | using System.Text.RegularExpressions; | 33 | using System.Text.RegularExpressions; |
34 | using System.Threading; | 34 | using System.Threading; |
35 | using System.IO; | ||
36 | using Nini.Config; | ||
35 | using log4net; | 37 | using log4net; |
36 | 38 | ||
37 | namespace OpenSim.Framework.Console | 39 | namespace OpenSim.Framework.Console |
@@ -41,7 +43,9 @@ namespace OpenSim.Framework.Console | |||
41 | /// </summary> | 43 | /// </summary> |
42 | public class LocalConsole : CommandConsole | 44 | public class LocalConsole : CommandConsole |
43 | { | 45 | { |
44 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
47 | private string m_historyPath; | ||
48 | private bool m_historyEnable; | ||
45 | 49 | ||
46 | // private readonly object m_syncRoot = new object(); | 50 | // private readonly object m_syncRoot = new object(); |
47 | private const string LOGLEVEL_NONE = "(none)"; | 51 | private const string LOGLEVEL_NONE = "(none)"; |
@@ -79,8 +83,54 @@ namespace OpenSim.Framework.Console | |||
79 | return Colors[(Math.Abs(input.ToUpper().GetHashCode()) % Colors.Length)]; | 83 | return Colors[(Math.Abs(input.ToUpper().GetHashCode()) % Colors.Length)]; |
80 | } | 84 | } |
81 | 85 | ||
82 | public LocalConsole(string defaultPrompt) : base(defaultPrompt) | 86 | public LocalConsole(string defaultPrompt, IConfig startupConfig = null) : base(defaultPrompt) |
83 | { | 87 | { |
88 | |||
89 | if (startupConfig == null) return; | ||
90 | |||
91 | m_historyEnable = startupConfig.GetBoolean("ConsoleHistoryFileEnabled", false); | ||
92 | if (!m_historyEnable) | ||
93 | { | ||
94 | m_log.Info("[LOCAL CONSOLE]: Persistent command line history from file is Disabled"); | ||
95 | return; | ||
96 | } | ||
97 | |||
98 | string m_historyFile = startupConfig.GetString("ConsoleHistoryFile", "OpenSimConsoleHistory.txt"); | ||
99 | int m_historySize = startupConfig.GetInt("ConsoleHistoryFileLines", 100); | ||
100 | m_historyPath = Path.GetFullPath(Path.Combine(Util.configDir(), m_historyFile)); | ||
101 | m_log.InfoFormat("[LOCAL CONSOLE]: Persistent command line history is Enabled, up to {0} lines from file {1}", m_historySize, m_historyPath); | ||
102 | |||
103 | if (File.Exists(m_historyPath)) | ||
104 | { | ||
105 | using (StreamReader history_file = new StreamReader(m_historyPath)) | ||
106 | { | ||
107 | string line; | ||
108 | while ((line = history_file.ReadLine()) != null) | ||
109 | { | ||
110 | m_history.Add(line); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | if (m_history.Count > m_historySize) | ||
115 | { | ||
116 | while (m_history.Count > m_historySize) | ||
117 | m_history.RemoveAt(0); | ||
118 | |||
119 | using (StreamWriter history_file = new StreamWriter(m_historyPath)) | ||
120 | { | ||
121 | foreach (string line in m_history) | ||
122 | { | ||
123 | history_file.WriteLine(line); | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | m_log.InfoFormat("[LOCAL CONSOLE]: Read {0} lines of command line history from file {1}", m_history.Count, m_historyPath); | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | m_log.InfoFormat("[LOCAL CONSOLE]: Creating new empty command line history file {0}", m_historyPath); | ||
132 | File.Create(m_historyPath).Dispose(); | ||
133 | } | ||
84 | } | 134 | } |
85 | 135 | ||
86 | private void AddToHistory(string text) | 136 | private void AddToHistory(string text) |
@@ -89,6 +139,10 @@ namespace OpenSim.Framework.Console | |||
89 | m_history.RemoveAt(0); | 139 | m_history.RemoveAt(0); |
90 | 140 | ||
91 | m_history.Add(text); | 141 | m_history.Add(text); |
142 | if (m_historyEnable) | ||
143 | { | ||
144 | File.AppendAllText(m_historyPath, text + Environment.NewLine); | ||
145 | } | ||
92 | } | 146 | } |
93 | 147 | ||
94 | /// <summary> | 148 | /// <summary> |