1
0
Files
Toak/Commands/HistoryCommand.cs

89 lines
2.7 KiB
C#

using Spectre.Console;
using Toak.Core;
namespace Toak.Commands;
public static class HistoryCommand
{
public static async Task ExecuteAsync(int count, string grep, string export, bool shred, bool verbose)
{
Logger.Verbose = verbose;
var historyManager = new HistoryManager();
if (shred)
{
historyManager.ClearHistory();
AnsiConsole.MarkupLine("[green]History successfully shredded.[/]");
return;
}
var entries = historyManager.LoadHistory();
if (entries.Count == 0)
{
AnsiConsole.MarkupLine("[yellow]No history found.[/]");
return;
}
// Apply grep filter
if (!string.IsNullOrWhiteSpace(grep))
{
entries = entries.Where(e =>
e.RawTranscript.Contains(grep, StringComparison.OrdinalIgnoreCase) ||
e.RefinedText.Contains(grep, StringComparison.OrdinalIgnoreCase))
.ToList();
if (entries.Count == 0)
{
AnsiConsole.MarkupLine($"[yellow]No history entries match '{grep}'.[/]");
return;
}
}
// Get last N
entries = entries.OrderBy(e => e.Timestamp).TakeLast(count).ToList();
// Export
if (!string.IsNullOrWhiteSpace(export))
{
try
{
await using var writer = new StreamWriter(export);
await writer.WriteLineAsync($"# Toak Transcriptions - {DateTime.Now:yyyy-MM-dd}");
await writer.WriteLineAsync();
foreach (var entry in entries)
{
await writer.WriteLineAsync($"## {entry.Timestamp.ToLocalTime():HH:mm:ss}");
await writer.WriteLineAsync(entry.RefinedText);
await writer.WriteLineAsync();
}
AnsiConsole.MarkupLine($"[green]Successfully exported {entries.Count} entries to {export}[/]");
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error exporting history:[/] {ex.Message}");
}
return;
}
// Display
var table = new Table().Border(TableBorder.Rounded);
table.AddColumn("Time");
table.AddColumn("Skill");
table.AddColumn("Text");
foreach (var entry in entries)
{
table.AddRow(
$"[dim]{entry.Timestamp.ToLocalTime():HH:mm:ss}[/]",
entry.SkillName != null ? $"[blue]{entry.SkillName}[/]" : "-",
entry.RefinedText.Replace("[", "[[").Replace("]", "]]") // Escape spectre markup
);
}
AnsiConsole.Write(table);
}
}