Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 86 additions & 11 deletions PSReadLine/Cmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ public enum AddToHistoryOption
{
SkipAdding,
MemoryOnly,
MemoryAndFile
MemoryAndFile,
SQLite
}

public enum HistoryType
{
Text,
SQLite
}

public enum PredictionSource
Expand Down Expand Up @@ -107,6 +114,12 @@ public class PSConsoleReadLineOptions

public const string DefaultContinuationPrompt = ">> ";

/// <summary>
/// The default history type is text-based history.
/// Users can change default behavior by setting this to SQLite in their profile.
/// </summary>
public const HistoryType DefaultHistoryType = HistoryType.Text;

/// <summary>
/// The maximum number of commands to store in the history.
/// </summary>
Expand Down Expand Up @@ -198,6 +211,7 @@ public PSConsoleReadLineOptions(string hostName, bool usingLegacyConsole)
ResetColors();
EditMode = DefaultEditMode;
ScreenReaderModeEnabled = Accessibility.IsScreenReaderActive();
HistoryType = DefaultHistoryType;
ContinuationPrompt = DefaultContinuationPrompt;
ContinuationPromptColor = Console.ForegroundColor;
ExtraPromptLineCount = DefaultExtraPromptLineCount;
Expand Down Expand Up @@ -225,13 +239,20 @@ public PSConsoleReadLineOptions(string hostName, bool usingLegacyConsole)
var historyFileName = hostName + "_history.txt";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
HistorySavePath = System.IO.Path.Combine(
HistorySavePathText = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Microsoft",
"Windows",
"PowerShell",
"PSReadLine",
historyFileName);
HistorySavePathSQLite = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Microsoft",
"Windows",
"PowerShell",
"PSReadLine",
hostName + "_history.db");
}
else
{
Expand All @@ -240,11 +261,16 @@ public PSConsoleReadLineOptions(string hostName, bool usingLegacyConsole)

if (!String.IsNullOrEmpty(historyPath))
{
HistorySavePath = System.IO.Path.Combine(
HistorySavePathText = System.IO.Path.Combine(
historyPath,
"powershell",
"PSReadLine",
historyFileName);
HistorySavePathSQLite = System.IO.Path.Combine(
historyPath,
"powershell",
"PSReadLine",
hostName + "_history.db");
}
else
{
Expand All @@ -253,18 +279,26 @@ public PSConsoleReadLineOptions(string hostName, bool usingLegacyConsole)

if (!String.IsNullOrEmpty(home))
{
HistorySavePath = System.IO.Path.Combine(
HistorySavePathText = System.IO.Path.Combine(
home,
".local",
"share",
"powershell",
"PSReadLine",
historyFileName);
HistorySavePathSQLite = System.IO.Path.Combine(
home,
".local",
"share",
"powershell",
"PSReadLine",
hostName + "_history.db");
}
else
{
// No HOME, then don't save anything
HistorySavePath = "/dev/null";
HistorySavePathText = "/dev/null";
HistorySavePathSQLite = "/dev/null";
}
}
}
Expand Down Expand Up @@ -333,6 +367,7 @@ public object ContinuationPromptColor
/// that do invoke the script block - this covers the most useful cases.
/// </summary>
public HashSet<string> CommandsToValidateScriptBlockArguments { get; set; }
public HistoryType HistoryType { get; set; } = HistoryType.Text;

/// <summary>
/// When true, duplicates will not be recalled from history more than once.
Expand Down Expand Up @@ -369,9 +404,23 @@ public object ContinuationPromptColor
public ScriptBlock ViModeChangeHandler { get; set; }

/// <summary>
/// The path to the saved history.
/// The path to the text history file.
/// </summary>
public string HistorySavePathText { get; set; }

/// <summary>
/// The path to the SQLite history database.
/// </summary>
public string HistorySavePath { get; set; }
public string HistorySavePathSQLite { get; set; }

/// <summary>
/// Returns the active history save path based on the current <see cref="HistoryType"/>.
/// </summary>
public string HistorySavePath => HistoryType switch
{
HistoryType.SQLite => HistorySavePathSQLite,
_ => HistorySavePathText,
};
public HistorySaveStyle HistorySaveStyle { get; set; }

/// <summary>
Expand Down Expand Up @@ -655,6 +704,20 @@ public EditMode EditMode
[AllowEmptyString]
public string ContinuationPrompt { get; set; }

[Parameter]
public HistoryType HistoryType
{
get => _historyType.GetValueOrDefault();
set
{
_historyType = value;
_historyTypeSpecified = true;
}
}

public HistoryType? _historyType = HistoryType.Text;
internal bool _historyTypeSpecified;

[Parameter]
public SwitchParameter HistoryNoDuplicates
{
Expand Down Expand Up @@ -785,15 +848,27 @@ public HistorySaveStyle HistorySaveStyle

[Parameter]
[ValidateNotNullOrEmpty]
public string HistorySavePath
public string HistorySavePathText
{
get => _historySavePathText;
set
{
_historySavePathText = GetUnresolvedProviderPathFromPSPath(value);
}
}
private string _historySavePathText;

[Parameter]
[ValidateNotNullOrEmpty]
public string HistorySavePathSQLite
{
get => _historySavePath;
get => _historySavePathSQLite;
set
{
_historySavePath = GetUnresolvedProviderPathFromPSPath(value);
_historySavePathSQLite = GetUnresolvedProviderPathFromPSPath(value);
}
}
private string _historySavePath;
private string _historySavePathSQLite;

[Parameter]
[ValidateRange(25, 1000)]
Expand Down
Loading