public void ProcessList(string[] arguments) { PrintLineWithColour("Running processes:\n", ProgramConfiguration.TitleColour); Process[] processes = Process.GetProcesses(); Array.Sort(processes, ProcessComparison); List<ColouredString[]> lines = new List<ColouredString[]>(); foreach (Process process in processes) { string moduleString = ""; try { moduleString = process.MainModule.FileName; } catch (Exception) { } ColouredString name = new ColouredString(process.ProcessName, ProgramConfiguration.HighlightColour), pid = new ColouredString(process.Id.ToString()), module = new ColouredString(moduleString); List<ColouredString> columns = new List<ColouredString>(); columns.Add(name); columns.Add(pid); columns.Add(module); lines.Add(columns.ToArray()); } PrintTable(lines.ToArray()); }
public void PrintTable(ColouredString[][] table) { const int padding = 4; List<int> columnWidths = new List<int>(); int columnCount = table.First().Length; for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { int currentWidth = 0; for (int rowIndex = 0; rowIndex < table.Length; rowIndex++) { ColouredString[] row = table[rowIndex]; int width = row[columnIndex].Content.Length; currentWidth = Math.Max(currentWidth, width); } if (columnIndex != columnCount - 1) currentWidth += padding; columnWidths.Add(currentWidth); } for (int rowIndex = 0; rowIndex < table.Length; rowIndex++) { for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { int width = columnWidths[columnIndex]; ColouredString field = table[rowIndex][columnIndex]; string fieldContent = field.Content; fieldContent += new string(' ', width - fieldContent.Length); if (field.Colour == null) Print(fieldContent); else PrintWithColour(fieldContent, field.Colour); } PrintLine(""); } }
public void ListDirectoryContents(string[] arguments) { string path; if (arguments.Length == 1) path = arguments.First(); else path = "."; try { List<ColouredString[]> rows = new List<ColouredString[]>(); DirectoryInfo directory = new DirectoryInfo(path); foreach (DirectoryInfo subDirectory in directory.GetDirectories()) { List<ColouredString> columns = new List<ColouredString>(); ColouredString name = new ColouredString(subDirectory.Name, ProgramConfiguration.ListDirectoryColour), time = new ColouredString(subDirectory.LastWriteTime.ToString()), size = new ColouredString(""); columns.Add(name); columns.Add(time); columns.Add(size); rows.Add(columns.ToArray()); } foreach (FileInfo file in directory.GetFiles()) { const string executableExtension = ".exe"; string name = file.Name; SerialisableColour currentColour; if(name.Length >= executableExtension.Length && name.Substring(name.Length - executableExtension.Length) == executableExtension) currentColour = ProgramConfiguration.ListExecutableColour; else currentColour = ProgramConfiguration.ListFileColour; List<ColouredString> columns = new List<ColouredString>(); ColouredString nameColumn = new ColouredString(name, currentColour), time = new ColouredString(file.LastWriteTime.ToString()), size = new ColouredString(Nil.String.GetFileSizeString(file.Length)); columns.Add(nameColumn); columns.Add(time); columns.Add(size); rows.Add(columns.ToArray()); } PrintTable(rows.ToArray()); } catch (Exception exception) { PrintError(exception.Message); } }