2
0

Made CLI auto-update on menu screen (task count)

And tail the logfile
This commit is contained in:
glax 2023-05-21 14:44:33 +02:00
parent 636d17d287
commit 168bf5a358
3 changed files with 111 additions and 91 deletions

View File

@ -55,4 +55,9 @@ public class Logger : TextWriter
{ {
return _memoryLogger.Tail(lines); return _memoryLogger.Tail(lines);
} }
public string[] GetNewLines()
{
return _memoryLogger.GetNewLines();
}
} }

View File

@ -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++)
ret[logMessageIndex + retLength - logMessages.Count] = logMessages.GetValueAtIndex(logMessageIndex).ToString();
for (int retIndex = 0; retIndex < ret.Length; retIndex++)
{
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;
} }
} }

View File

@ -75,63 +75,79 @@ 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)
{ {
switch (selection) 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)
{ {
case ConsoleKey.L: selection = Console.ReadKey().Key;
PrintTasks(taskManager.GetAllTasks(), logger); switch (selection)
Console.WriteLine("Press any key."); {
Console.ReadKey(); case ConsoleKey.L:
break; PrintTasks(taskManager.GetAllTasks(), logger);
case ConsoleKey.C: Console.WriteLine("Press any key.");
CreateTask(taskManager, settings, logger); Console.ReadKey();
Console.WriteLine("Press any key."); break;
Console.ReadKey(); case ConsoleKey.C:
break; CreateTask(taskManager, settings, logger);
case ConsoleKey.D: Console.WriteLine("Press any key.");
DeleteTask(taskManager, logger); Console.ReadKey();
Console.WriteLine("Press any key."); break;
Console.ReadKey(); case ConsoleKey.D:
break; DeleteTask(taskManager, logger);
case ConsoleKey.E: Console.WriteLine("Press any key.");
ExecuteTaskNow(taskManager, logger); Console.ReadKey();
Console.WriteLine("Press any key."); break;
Console.ReadKey(); case ConsoleKey.E:
break; ExecuteTaskNow(taskManager, logger);
case ConsoleKey.S: Console.WriteLine("Press any key.");
SearchTasks(taskManager, logger); Console.ReadKey();
Console.WriteLine("Press any key."); break;
Console.ReadKey(); case ConsoleKey.S:
break; SearchTasks(taskManager, logger);
case ConsoleKey.R: Console.WriteLine("Press any key.");
PrintTasks(taskManager.GetAllTasks().Where(eTask => eTask.state == TrangaTask.ExecutionState.Running).ToArray(), logger); Console.ReadKey();
Console.WriteLine("Press any key."); break;
Console.ReadKey(); case ConsoleKey.R:
break; PrintTasks(
case ConsoleKey.K: taskManager.GetAllTasks().Where(eTask => eTask.state == TrangaTask.ExecutionState.Running)
PrintTasks(taskManager.GetAllTasks().Where(qTask => qTask.state is TrangaTask.ExecutionState.Enqueued).ToArray(), logger); .ToArray(), logger);
Console.WriteLine("Press any key."); Console.WriteLine("Press any key.");
Console.ReadKey(); Console.ReadKey();
break; break;
case ConsoleKey.F: case ConsoleKey.K:
ShowLastLoglines(logger); PrintTasks(
Console.WriteLine("Press any key."); taskManager.GetAllTasks().Where(qTask => qTask.state is TrangaTask.ExecutionState.Enqueued)
Console.ReadKey(); .ToArray(), logger);
break; Console.WriteLine("Press any key.");
case ConsoleKey.M: Console.ReadKey();
RemoveTaskFromQueue(taskManager, logger); break;
Console.WriteLine("Press any key."); case ConsoleKey.F:
Console.ReadKey(); TailLog(logger);
break; Console.ReadKey();
case ConsoleKey.B: break;
AddTaskToQueue(taskManager, logger); case ConsoleKey.M:
Console.WriteLine("Press any key."); RemoveTaskFromQueue(taskManager, logger);
Console.ReadKey(); Console.WriteLine("Press any key.");
break; Console.ReadKey();
break;
case ConsoleKey.B:
AddTaskToQueue(taskManager, logger);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
}
PrintMenu(taskManager, settings.downloadLocation, logger);
} }
selection = 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.Clear();
Console.WriteLine("Enter q to abort"); string[] lines = logger.Tail(20);
Console.WriteLine($"Number of lines:"); foreach (string message in lines)
Console.Write(message);
string? chosenNumber = Console.ReadLine(); while (!Console.KeyAvailable)
while(chosenNumber is null || chosenNumber.Length < 1)
chosenNumber = Console.ReadLine();
if (chosenNumber.Length == 1 && chosenNumber.ToLower() == "q")
{ {
Console.Clear(); string[] newLines = logger.GetNewLines();
Console.WriteLine("aborted."); foreach(string message in newLines)
logger.WriteLine("Tranga_CLI", "aborted");
return;
}
try
{
string[] lines = logger.Tail(Convert.ToUInt32(chosenNumber));
Console.Clear();
foreach (string message in lines)
Console.Write(message); Console.Write(message);
} Thread.Sleep(40);
catch (Exception e)
{
Console.WriteLine($"Exception: {e.Message}");
logger.WriteLine("Tranga_CLI", e.Message);
} }
} }