-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoryManager.cs
More file actions
106 lines (95 loc) · 3.31 KB
/
HistoryManager.cs
File metadata and controls
106 lines (95 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.IO;
namespace NShell.Shell.History
{
/// <summary>
/// <c>HistoryManager</c> class provide all methods about history.
/// You can load the history of all previous commands,
/// add a command into <c>_history</c>, save the history file.
/// </summary>
public class HistoryManager
{
private readonly string _historyPath;
private readonly List<string> _history = new();
private int _currentIndex = -1;
public int Count => _history.Count;
public HistoryManager(string? path = null)
{
_historyPath = path ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nshell/.nhistory");
Load();
}
/// <summary>
/// Load all commands in the history file.
/// </summary>
private void Load()
{
if (File.Exists(_historyPath))
{
_history.AddRange(File.ReadAllLines(_historyPath));
}
else
{
File.Create(_historyPath).Close();
}
}
/// <summary>
/// Save all the commands into the history file.
/// </summary>
public void Save()
{
File.WriteAllLines(_historyPath, _history);
}
/// <summary>
/// Add a command into the string list <c>_history</c>.
/// <param name="command">The command to save into <c>_history</c></param>
/// </summary>
public void Add(string command)
{
if (!string.IsNullOrWhiteSpace(command))
{
_history.Add(command);
_currentIndex = _history.Count;
File.WriteAllLines(_historyPath, _history);
}
}
/// <summary>
/// Get the previous command into <c>_history</c>.
/// </summary>
/// <returns>A <see cref="string"/> with the string value of the command.</returns>
public string? GetPrevious()
{
if (_history.Count == 0 || _currentIndex <= 0) return null;
_currentIndex--;
return _history[_currentIndex];
}
/// <summary>
/// Get the next command into <c>_history</c>.
/// </summary>
/// <returns>A <see cref="string"/> with the string value of the command.</returns>
public string? GetNext()
{
if (_currentIndex >= _history.Count - 1) return null;
_currentIndex++;
return _history[_currentIndex];
}
/// <summary>
/// Reset the index of the <c>_currentIndex</c>.
/// </summary>
public void ResetIndex()
{
_currentIndex = _history.Count;
}
/// <summary>
/// Get a history item at a specific index.
/// </summary>
/// <param name="index">The index of the history item.</param>
/// <returns>The command at the specified index, or null if out of range.</returns>
public string? GetAt(int index)
{
if (index >= 0 && index < _history.Count)
return _history[index];
return null;
}
}
}