RunHistoryService.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Microsoft.CmdPal.Core.Common.Services; 6 using Microsoft.CmdPal.UI.ViewModels; 7 8 namespace Microsoft.CmdPal.UI; 9 10 internal sealed class RunHistoryService : IRunHistoryService 11 { 12 private readonly AppStateModel _appStateModel; 13 14 public RunHistoryService(AppStateModel appStateModel) 15 { 16 _appStateModel = appStateModel; 17 } 18 19 public IReadOnlyList<string> GetRunHistory() 20 { 21 if (_appStateModel.RunHistory.Count == 0) 22 { 23 var history = Microsoft.Terminal.UI.RunHistory.CreateRunHistory(); 24 _appStateModel.RunHistory.AddRange(history); 25 } 26 27 return _appStateModel.RunHistory; 28 } 29 30 public void ClearRunHistory() 31 { 32 _appStateModel.RunHistory.Clear(); 33 } 34 35 public void AddRunHistoryItem(string item) 36 { 37 // insert at the beginning of the list 38 if (string.IsNullOrWhiteSpace(item)) 39 { 40 return; // Do not add empty or whitespace items 41 } 42 43 _appStateModel.RunHistory.Remove(item); 44 45 // Add the item to the front of the history 46 _appStateModel.RunHistory.Insert(0, item); 47 48 AppStateModel.SaveState(_appStateModel); 49 } 50 }