public SimpleTail(FileArguments[] fileList) { this.fileList = fileList; foreach (FileArguments fileArgs in fileList) { // Verify that the file exists, abort early if it doesn't if (!File.Exists(fileArgs.path)) { Console.WriteLine("No file could be found at: " + fileArgs.path); continue; } // Print header with file path, if quiet mode isn't enabled. if(!fileArgs.quiet) { PrintHeader(fileArgs.path); } // Print last 10 lines of file LinkedCharBuffer lines = ReadLastLines(LINE_COUNT, fileArgs.path); foreach (char c in lines) { Console.Write(c); } // Start file watcher, if follow flag is set if (fileArgs.follow) { FollowFileTimer(fileArgs.path); } } }
static void Main(string[] args) { List<FileArguments> fileList = new List<FileArguments>(); FileArguments fileArgs = new FileArguments(); // Collect arguments, if any if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { string arg = args[i]; // Is the argument an option or file? if (arg.StartsWith("-")) { // OPTION: switch (arg) { case "-f": fileArgs.follow = true; break; case "-q": fileArgs.quiet = true; break; default: Console.WriteLine("Unsupported option '{0}'. Usage: ([-f] [-q] [FILE]) ... ", arg); return; // TODO: Support more options such as '-n', '--retry' and '-s' } } else { // FILE: fileArgs.path = arg; fileList.Add(fileArgs); fileArgs = new FileArguments(); // Prep a new args object for further files } } } // If no arguments, prompt on stdout if (fileList.Count == 0) { fileArgs = new FileArguments(); Console.WriteLine("Which file do you wish to print tail of?"); fileArgs.path = Console.ReadLine(); Console.WriteLine("Do you wish to follow the file? (y/n) (Outputs appended data as the file grows)"); string fInput = Console.ReadLine(); if (fInput == "y" || fInput == "yes") { fileArgs.follow = true; } fileList.Add(fileArgs); } new SimpleTail(fileList.ToArray()); #if DEBUG Console.ReadLine(); #endif }