private AddToHistoryOption GetAddToHistoryOption(string line) { // Whitespace only is useless, never add. if (string.IsNullOrWhiteSpace(line)) { return(AddToHistoryOption.SkipAdding); } // Under "no dupes" (which is on by default), immediately drop dupes of the previous line. if (Options.HistoryNoDuplicates && _history.Count > 0 && string.Equals(_history[_history.Count - 1].CommandLine, line, StringComparison.Ordinal)) { return(AddToHistoryOption.SkipAdding); } if (Options.AddToHistoryHandler != null) { if (Options.AddToHistoryHandler == PSConsoleReadLineOptions.DefaultAddToHistoryHandler) { // Avoid boxing if it's the default handler. return(GetDefaultAddToHistoryOption(line)); } object value = Options.AddToHistoryHandler(line); if (value is PSObject psObj) { value = psObj.BaseObject; } if (value is bool boolValue) { return(boolValue ? AddToHistoryOption.MemoryAndFile : AddToHistoryOption.SkipAdding); } if (value is AddToHistoryOption enumValue) { return(enumValue); } if (value is string strValue && Enum.TryParse(strValue, out enumValue)) { return(enumValue); } // 'TryConvertTo' incurs exception handling when the value cannot be converted to the target type. // It's expensive, especially when we need to process lots of history items from file during the // initialization. So do the conversion as the last resort. if (LanguagePrimitives.TryConvertTo(value, out enumValue)) { return(enumValue); } } // Add to both history queue and file by default. return(AddToHistoryOption.MemoryAndFile); }
private string MaybeAddToHistory(string result, List <EditItem> edits, int undoEditIndex, bool readingHistoryFile, bool fromDifferentSession) { bool addToHistory = !string.IsNullOrWhiteSpace(result) && ((Options.AddToHistoryHandler == null) || Options.AddToHistoryHandler(result)); if (addToHistory) { _history.Enqueue(new HistoryItem { _line = result, _edits = edits, _undoEditIndex = undoEditIndex, _saved = readingHistoryFile, _fromDifferentLiveSession = fromDifferentSession, }); _currentHistoryIndex = _history.Count; if (_options.HistorySaveStyle == HistorySaveStyle.SaveIncrementally && !readingHistoryFile) { IncrementalHistoryWrite(); } } // Clear the saved line unless we used AcceptAndGetNext in which // case we're really still in middle of history and might want // to recall the saved line. if (_getNextHistoryIndex == 0) { _savedCurrentLine._line = null; _savedCurrentLine._edits = null; _savedCurrentLine._undoEditIndex = 0; } return(result); }
private string MaybeAddToHistory( string result, List <EditItem> edits, int undoEditIndex, bool fromDifferentSession = false, bool fromInitialRead = false) { bool AddToHistory(string line) { // Whitespace only is useless, never add. if (string.IsNullOrWhiteSpace(line)) { return(false); } // If the user says don't add it, then don't. if (Options.AddToHistoryHandler != null && !Options.AddToHistoryHandler(result)) { return(false); } // Under "no dupes" (which is on by default), immediately drop dupes of the previous line. if (Options.HistoryNoDuplicates && _history.Count > 0) { return(!string.Equals(_history[_history.Count - 1].CommandLine, result, StringComparison.Ordinal)); } return(true); } if (AddToHistory(result)) { var fromHistoryFile = fromDifferentSession || fromInitialRead; _previousHistoryItem = new HistoryItem { CommandLine = result, _edits = edits, _undoEditIndex = undoEditIndex, _saved = fromHistoryFile, FromOtherSession = fromDifferentSession, FromHistoryFile = fromInitialRead, }; if (!fromHistoryFile) { _previousHistoryItem.StartTime = DateTime.UtcNow; } _history.Enqueue(_previousHistoryItem); _currentHistoryIndex = _history.Count; if (_options.HistorySaveStyle == HistorySaveStyle.SaveIncrementally && !fromHistoryFile) { IncrementalHistoryWrite(); } } else { _previousHistoryItem = null; } // Clear the saved line unless we used AcceptAndGetNext in which // case we're really still in middle of history and might want // to recall the saved line. if (_getNextHistoryIndex == 0) { _savedCurrentLine.CommandLine = null; _savedCurrentLine._edits = null; _savedCurrentLine._undoEditIndex = 0; } return(result); }
private string MaybeAddToHistory(string result, List <EditItem> edits, int undoEditIndex) { bool addToHistory = !string.IsNullOrWhiteSpace(result) && ((Options.AddToHistoryHandler == null) || Options.AddToHistoryHandler(result)); if (addToHistory && Options.HistoryNoDuplicates) { // REVIEW: should history be case sensitive - it is now. // A smart comparer could use the ast to ignore case on commands, parameters, // operators and keywords while remaining case sensitive on command arguments. addToHistory = !_hashedHistory.Contains(result); } if (addToHistory) { _history.Enqueue(new HistoryItem { _line = result, _edits = edits, _undoEditIndex = undoEditIndex }); _currentHistoryIndex = _history.Count; } if (_demoMode) { ClearDemoWindow(); } _savedCurrentLine._line = null; _savedCurrentLine._edits = null; _savedCurrentLine._undoEditIndex = 0; return(result); }