Made CLI auto-update on menu screen (task count)
And tail the logfile
This commit is contained in:
parent
636d17d287
commit
168bf5a358
@ -55,4 +55,9 @@ public class Logger : TextWriter
|
|||||||
{
|
{
|
||||||
return _memoryLogger.Tail(lines);
|
return _memoryLogger.Tail(lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string[] GetNewLines()
|
||||||
|
{
|
||||||
|
return _memoryLogger.GetNewLines();
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,7 +4,8 @@ namespace Logging;
|
|||||||
|
|
||||||
public class MemoryLogger : LoggerBase
|
public class MemoryLogger : LoggerBase
|
||||||
{
|
{
|
||||||
private SortedList<DateTime, LogMessage> logMessages = new();
|
private readonly SortedList<DateTime, LogMessage> _logMessages = new();
|
||||||
|
private int _lastLogMessageIndex = 0;
|
||||||
|
|
||||||
public MemoryLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding)
|
public MemoryLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding)
|
||||||
{
|
{
|
||||||
@ -13,27 +14,44 @@ public class MemoryLogger : LoggerBase
|
|||||||
|
|
||||||
protected override void Write(LogMessage value)
|
protected override void Write(LogMessage value)
|
||||||
{
|
{
|
||||||
logMessages.Add(value.logTime, value);
|
_logMessages.Add(value.logTime, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] GetLogMessage()
|
public string[] GetLogMessage()
|
||||||
{
|
{
|
||||||
return Tail(Convert.ToUInt32(logMessages.Count));
|
return Tail(Convert.ToUInt32(_logMessages.Count));
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] Tail(uint? length)
|
public string[] Tail(uint? length)
|
||||||
{
|
{
|
||||||
int retLength;
|
int retLength;
|
||||||
if (length is null || length > logMessages.Count)
|
if (length is null || length > _logMessages.Count)
|
||||||
retLength = logMessages.Count;
|
retLength = _logMessages.Count;
|
||||||
else
|
else
|
||||||
retLength = (int)length;
|
retLength = (int)length;
|
||||||
|
|
||||||
string[] ret = new string[retLength];
|
string[] ret = new string[retLength];
|
||||||
|
|
||||||
for (int logMessageIndex = logMessages.Count - retLength; logMessageIndex < logMessages.Count; logMessageIndex++)
|
for (int retIndex = 0; retIndex < ret.Length; retIndex++)
|
||||||
ret[logMessageIndex + retLength - logMessages.Count] = logMessages.GetValueAtIndex(logMessageIndex).ToString();
|
{
|
||||||
|
ret[retIndex] = _logMessages.GetValueAtIndex(_logMessages.Count - retLength + retIndex).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
_lastLogMessageIndex = _logMessages.Count - 1;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string[] GetNewLines()
|
||||||
|
{
|
||||||
|
int logMessageCount = _logMessages.Count;
|
||||||
|
string[] ret = new string[logMessageCount - _lastLogMessageIndex];
|
||||||
|
|
||||||
|
for (int retIndex = 0; retIndex < ret.Length; retIndex++)
|
||||||
|
{
|
||||||
|
ret[retIndex] = _logMessages.GetValueAtIndex(_lastLogMessageIndex + retIndex).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
_lastLogMessageIndex = logMessageCount;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -75,9 +75,20 @@ public static class Tranga_Cli
|
|||||||
private static void TaskMode(TaskManager.SettingsData settings, Logger logger)
|
private static void TaskMode(TaskManager.SettingsData settings, Logger logger)
|
||||||
{
|
{
|
||||||
TaskManager taskManager = new (settings, logger);
|
TaskManager taskManager = new (settings, logger);
|
||||||
ConsoleKey selection = PrintMenu(taskManager, settings.downloadLocation, logger);
|
ConsoleKey selection = ConsoleKey.EraseEndOfFile;
|
||||||
|
PrintMenu(taskManager, settings.downloadLocation, logger);
|
||||||
while (selection != ConsoleKey.Q)
|
while (selection != ConsoleKey.Q)
|
||||||
{
|
{
|
||||||
|
int taskCount = taskManager.GetAllTasks().Length;
|
||||||
|
int taskRunningCount = taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Running);
|
||||||
|
int taskEnqueuedCount =
|
||||||
|
taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Enqueued);
|
||||||
|
Console.SetCursorPosition(0,1);
|
||||||
|
Console.WriteLine($"Tasks (Running/Queue/Total)): {taskRunningCount}/{taskEnqueuedCount}/{taskCount}");
|
||||||
|
|
||||||
|
if (Console.KeyAvailable)
|
||||||
|
{
|
||||||
|
selection = Console.ReadKey().Key;
|
||||||
switch (selection)
|
switch (selection)
|
||||||
{
|
{
|
||||||
case ConsoleKey.L:
|
case ConsoleKey.L:
|
||||||
@ -106,18 +117,21 @@ public static class Tranga_Cli
|
|||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
break;
|
break;
|
||||||
case ConsoleKey.R:
|
case ConsoleKey.R:
|
||||||
PrintTasks(taskManager.GetAllTasks().Where(eTask => eTask.state == TrangaTask.ExecutionState.Running).ToArray(), logger);
|
PrintTasks(
|
||||||
|
taskManager.GetAllTasks().Where(eTask => eTask.state == TrangaTask.ExecutionState.Running)
|
||||||
|
.ToArray(), logger);
|
||||||
Console.WriteLine("Press any key.");
|
Console.WriteLine("Press any key.");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
break;
|
break;
|
||||||
case ConsoleKey.K:
|
case ConsoleKey.K:
|
||||||
PrintTasks(taskManager.GetAllTasks().Where(qTask => qTask.state is TrangaTask.ExecutionState.Enqueued).ToArray(), logger);
|
PrintTasks(
|
||||||
|
taskManager.GetAllTasks().Where(qTask => qTask.state is TrangaTask.ExecutionState.Enqueued)
|
||||||
|
.ToArray(), logger);
|
||||||
Console.WriteLine("Press any key.");
|
Console.WriteLine("Press any key.");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
break;
|
break;
|
||||||
case ConsoleKey.F:
|
case ConsoleKey.F:
|
||||||
ShowLastLoglines(logger);
|
TailLog(logger);
|
||||||
Console.WriteLine("Press any key.");
|
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
break;
|
break;
|
||||||
case ConsoleKey.M:
|
case ConsoleKey.M:
|
||||||
@ -131,7 +145,9 @@ public static class Tranga_Cli
|
|||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
selection = PrintMenu(taskManager, settings.downloadLocation, logger);
|
PrintMenu(taskManager, settings.downloadLocation, logger);
|
||||||
|
}
|
||||||
|
Thread.Sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.WriteLine("Tranga_CLI", "Exiting.");
|
logger.WriteLine("Tranga_CLI", "Exiting.");
|
||||||
@ -147,7 +163,7 @@ public static class Tranga_Cli
|
|||||||
taskManager.Shutdown(false);
|
taskManager.Shutdown(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConsoleKey PrintMenu(TaskManager taskManager, string folderPath, Logger logger)
|
private static void PrintMenu(TaskManager taskManager, string folderPath, Logger logger)
|
||||||
{
|
{
|
||||||
int taskCount = taskManager.GetAllTasks().Length;
|
int taskCount = taskManager.GetAllTasks().Length;
|
||||||
int taskRunningCount = taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Running);
|
int taskRunningCount = taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Running);
|
||||||
@ -161,10 +177,7 @@ public static class Tranga_Cli
|
|||||||
Console.WriteLine($"{"D: Delete Task",-30}{"S: Search Tasks", -30}{"K: List Task Queue", -30}");
|
Console.WriteLine($"{"D: Delete Task",-30}{"S: Search Tasks", -30}{"K: List Task Queue", -30}");
|
||||||
Console.WriteLine($"{"E: Execute Task now",-30}{"R: List Running Tasks", -30}{"M: Remove Task from Queue", -30}");
|
Console.WriteLine($"{"E: Execute Task now",-30}{"R: List Running Tasks", -30}{"M: Remove Task from Queue", -30}");
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine($"{"U: Update this Screen",-30}{"F: Show Log",-30}{"Q: Exit",-30}");
|
Console.WriteLine($"{"",-30}{"F: Show Log",-30}{"Q: Exit",-30}");
|
||||||
ConsoleKey selection = Console.ReadKey().Key;
|
|
||||||
logger.WriteLine("Tranga_CLI", $"Menu selection: {selection}");
|
|
||||||
return selection;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void PrintTasks(TrangaTask[] tasks, Logger logger)
|
private static void PrintTasks(TrangaTask[] tasks, Logger logger)
|
||||||
@ -258,37 +271,21 @@ public static class Tranga_Cli
|
|||||||
taskManager.RemoveTaskFromQueue(selectedTask);
|
taskManager.RemoveTaskFromQueue(selectedTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ShowLastLoglines(Logger logger)
|
private static void TailLog(Logger logger)
|
||||||
{
|
{
|
||||||
Console.Clear();
|
|
||||||
logger.WriteLine("Tranga_CLI", "Menu: Show Log-lines");
|
logger.WriteLine("Tranga_CLI", "Menu: Show Log-lines");
|
||||||
|
|
||||||
Console.WriteLine("Enter q to abort");
|
|
||||||
Console.WriteLine($"Number of lines:");
|
|
||||||
|
|
||||||
string? chosenNumber = Console.ReadLine();
|
|
||||||
while(chosenNumber is null || chosenNumber.Length < 1)
|
|
||||||
chosenNumber = Console.ReadLine();
|
|
||||||
|
|
||||||
if (chosenNumber.Length == 1 && chosenNumber.ToLower() == "q")
|
|
||||||
{
|
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
Console.WriteLine("aborted.");
|
|
||||||
logger.WriteLine("Tranga_CLI", "aborted");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
string[] lines = logger.Tail(20);
|
||||||
{
|
|
||||||
string[] lines = logger.Tail(Convert.ToUInt32(chosenNumber));
|
|
||||||
Console.Clear();
|
|
||||||
foreach (string message in lines)
|
foreach (string message in lines)
|
||||||
Console.Write(message);
|
Console.Write(message);
|
||||||
}
|
|
||||||
catch (Exception e)
|
while (!Console.KeyAvailable)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Exception: {e.Message}");
|
string[] newLines = logger.GetNewLines();
|
||||||
logger.WriteLine("Tranga_CLI", e.Message);
|
foreach(string message in newLines)
|
||||||
|
Console.Write(message);
|
||||||
|
Thread.Sleep(40);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user