190 lines
4.6 KiB
C#
190 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows.Input;
|
|
using AmebaPro3_ControlPanel.Utilities;
|
|
using Microsoft.Win32;
|
|
|
|
namespace AmebaPro3_ControlPanel.ViewModels;
|
|
|
|
public class ConsolePanelViewModel : ViewModelBase
|
|
{
|
|
private string _title;
|
|
private bool _isConnected;
|
|
private string _currentCommand = string.Empty;
|
|
|
|
public ConsolePanelViewModel(string title)
|
|
{
|
|
_title = title;
|
|
SendCommand = new RelayCommand(_ => SendCurrentCommand(true));
|
|
ClearLog = new RelayCommand(_ => LogLines.Clear());
|
|
SaveLog = new RelayCommand(_ => SaveLogToFile());
|
|
LoadCommandFileCommand = new RelayCommand(_ => LoadCommandsFromFile());
|
|
}
|
|
|
|
public string Title
|
|
{
|
|
get => _title;
|
|
set => SetProperty(ref _title, value);
|
|
}
|
|
|
|
public bool IsConnected
|
|
{
|
|
get => _isConnected;
|
|
set => SetProperty(ref _isConnected, value);
|
|
}
|
|
|
|
public ObservableCollection<string> LogLines { get; } = new();
|
|
|
|
public ObservableCollection<string> History { get; } = new();
|
|
|
|
public string CurrentCommand
|
|
{
|
|
get => _currentCommand;
|
|
set => SetProperty(ref _currentCommand, value);
|
|
}
|
|
|
|
public ICommand SendCommand { get; }
|
|
|
|
public ICommand ClearLog { get; }
|
|
|
|
public ICommand SaveLog { get; }
|
|
|
|
public ICommand LoadCommandFileCommand { get; }
|
|
|
|
protected virtual void SendCurrentCommand(bool addToHistory)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(CurrentCommand))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var command = CurrentCommand.Trim();
|
|
LogLines.Add($"> {command}");
|
|
if (addToHistory)
|
|
{
|
|
AddHistoryEntry(command);
|
|
}
|
|
CurrentCommand = string.Empty;
|
|
}
|
|
|
|
public void SendFromHistory(string command)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command))
|
|
{
|
|
return;
|
|
}
|
|
|
|
CurrentCommand = command;
|
|
SendCurrentCommand(true);
|
|
}
|
|
|
|
protected virtual void SaveLogToFile()
|
|
{
|
|
var dialog = new SaveFileDialog
|
|
{
|
|
Filter = "Text Files (*.txt)|*.txt|Log Files (*.log)|*.log|All Files (*.*)|*.*",
|
|
FileName = $"{Title}_Log.txt"
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
try
|
|
{
|
|
File.WriteAllLines(dialog.FileName, LogLines);
|
|
LogLines.Add($"Log saved to {dialog.FileName}");
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
LogLines.Add($"Failed to save log: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void LoadCommandsFromFile()
|
|
{
|
|
var dialog = new OpenFileDialog
|
|
{
|
|
Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*",
|
|
Multiselect = false
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
try
|
|
{
|
|
var commands = File.ReadAllLines(dialog.FileName)
|
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
|
.Select(line => line.Trim())
|
|
.ToList();
|
|
|
|
foreach (var command in commands)
|
|
{
|
|
AddHistoryEntry(command);
|
|
}
|
|
|
|
if (commands.Count > 0)
|
|
{
|
|
LogLines.Add($"Loaded {commands.Count} command(s) from file.");
|
|
}
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
LogLines.Add($"Failed to load commands: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SeedSampleLog(IEnumerable<string> lines)
|
|
{
|
|
foreach (var line in lines)
|
|
{
|
|
LogLines.Add(line);
|
|
}
|
|
}
|
|
|
|
public void SeedSampleHistory(IEnumerable<string> commands)
|
|
{
|
|
foreach (var command in commands)
|
|
{
|
|
AddHistoryEntry(command);
|
|
}
|
|
}
|
|
|
|
public void DeleteHistoryItems(IEnumerable<string> itemsToDelete)
|
|
{
|
|
foreach (var item in itemsToDelete)
|
|
{
|
|
History.Remove(item);
|
|
}
|
|
}
|
|
|
|
public void DeleteLogLines(IEnumerable<string> linesToDelete)
|
|
{
|
|
foreach (var line in linesToDelete)
|
|
{
|
|
LogLines.Remove(line);
|
|
}
|
|
}
|
|
|
|
protected void AddHistoryEntry(string command)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var trimmed = command.Trim();
|
|
for (var i = History.Count - 1; i >= 0; i--)
|
|
{
|
|
if (History[i] == trimmed)
|
|
{
|
|
History.RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
History.Add(trimmed);
|
|
}
|
|
}
|