public static void errorArgument(string command) { if (command == "diff") //This can be tailored to each type of command for varies responses. { Output.writeOut("Diff takes text files as arguments. Please use 'help'."); //Or if the command takes specific arguments } }
public static void Start() { var logLine = new Logger.logFile(); logLine.logCreate(); //Create or overwrite the log file to be used during runtime Output.writeOut("----- C# Git Diff Implementation -----"); Output.writeOut("Input 'help' for directions on how to use."); //A few niceties to help with console usability. }
public static void help(string command) { if (command == "help") { Output.writeOut("'help' displays information about how to use a specific command.\nFor instance, 'help diff' shows how to use diff."); } else if (command == "diff") { Output.writeOut("'diff' returns the difference between two files.\nFor intance, 'diff fileOne.txt fileTwo.txt' will return the differences between those two files.\n'diff' takes at least two arguments."); } else if (command == "quit") { Output.writeOut("'quit' will quit from the program.\nFor instance, 'quit' will close the console window."); } else { UnknownCommand.unknownCommand(command); //If the command isn't listed in the help command, raise an error. } }
public static List <List <string[]> > Read(string fileOne, string fileTwo) { if (fileOne.Contains(".txt") && fileTwo.Contains(".txt")) //Check to make sure both are text files { List <string[]> contentsOne = new List <string[]>(); //Initialise new lists List <string[]> contentsTwo = new List <string[]>(); List <List <string[]> > contentsAll = new List <List <string[]> >(); //And one for the contents of both combined try //Attempt to read the content of the file { string[] contentsFileOne = System.IO.File.ReadAllLines($@"../../../../Files/{fileOne}"); //Only looking at this particular path contentsOne.Add(contentsFileOne); } catch (Exception) //And catch the exception if something is wrong, such as the file not existing or being corrupt { Output.writeOut($"File name {fileOne} is invalid."); return(null); //Return something to satisfy the return state } try { string[] contentsFileTwo = System.IO.File.ReadAllLines($@"../../../../Files/{fileTwo}"); contentsTwo.Add(contentsFileTwo); } catch (Exception) { Output.writeOut($"File name {fileTwo} is invalid."); return(null); } contentsAll.Add(contentsOne); contentsAll.Add(contentsTwo); return(contentsAll); //Contents all is returned with two lists of the file's contents } else { Output.writeOut($"File names don't contain .txt..."); return(null); } }
public void parse(string userInput) //Take the user's input as an argument { var words = userInput.Split(" "); //And split it into the seperate words if (words[0] == "help") //Check what the first (command) word is { try { HelpCommand.help(words[1]); //If there's a command afterwards, for specific help, attempt to use that } catch (IndexOutOfRangeException) { HelpCommand.help(words[0]); //Else use "help" as the help argument } } else if (words[0] == "diff") { List <List <string[]> > contents = new List <List <string[]> >(); //Create a new empty string to put the file contents try { contents = FileRead.Read(words[1], words[2]); //Attempt to read the files given } catch (IndexOutOfRangeException) //If only one file was given { UnknownCommand.unknownArgument("diff"); //Raise an error contents = FileRead.Read("nullOne.txt", "nullTwo.txt"); //Proceed with empty files to prevent later issues } catch (NullReferenceException) //If the tile is not a readable file { UnknownCommand.errorArgument("diff"); contents = FileRead.Read("nullOne.txt", "nullTwo.txt"); } Diff.Difference(contents); //Find the difference using the contents } else if (words[0] == "debug") //Debug allows for quick testing of the diff command { List <List <string[]> > contents = new List <List <string[]> >(); try { contents = FileRead.Read("test1.txt", "test2.txt"); //The only two text files used are the test files. } catch (IndexOutOfRangeException) { UnknownCommand.unknownArgument("diff"); } catch (NullReferenceException) { UnknownCommand.errorArgument("diff"); } Diff.Difference(contents); //Custom files are ignored. } else if (words[0] == "quit") { Output.writeOut("Exiting the console..."); Environment.Exit(0); //Gracefully exit the console. } else if (words[0] == "" || words[0] == " ") //If the user accidentally enters nothing { Console.WriteLine(""); //Don't throw an error, just ignore it and move on } else { UnknownCommand.unknownCommand(words[0]); //If nothing is recognised, raise an error. } }
//A broad class for all kinds of errors public static void unknownCommand(string command) { Output.writeOut($"'{command}' is an unknown command. Please use 'help'."); //If the command isn't recognised }
public static void unknownArgument(string command) { Output.writeOut($"{command} takes arguments. Please use 'help'."); //If the command takes arguments }
public static void Difference(List <List <string[]> > fileContents) //Take the list of lines of text (from both files) { List <string[]> One = new List <string[]>(); //Create new lists for both seperate files List <string[]> Two = new List <string[]>(); bool commence = true; //Use a flag to prevent unnecessary output if inputs are invalid if (fileContents == null) { commence = false; //If the content isn't there, don't commensce } else if (fileContents.Count > 0) { One = fileContents[0]; //If the content is there, split it into the two seperate files Two = fileContents[1]; } if (commence == true) { List <List <string> > linesOne = new List <List <string> >(); //Make some new lists for the lines of text List <List <string> > linesTwo = new List <List <string> >(); int maxLines = 0; //And a variable to hold the max number of lines in the two foreach (string[] list in One) { foreach (string line in list) { List <string> words = new List <string>(line.Split(" ")); //For each line split them into their words linesOne.Add(words); //And add them to the lists } } foreach (string[] list in Two) { foreach (string line in list) { List <string> words = new List <string>(line.Split(" ")); linesTwo.Add(words); } } // ---Equalise Length of Files--- if (linesOne.Count > linesTwo.Count) { maxLines = linesOne.Count; //Assigning the maxLines to the number of lines in the biggets file. linesTwo.Add(new List <string>()); //Add a new empty line to prevent indexing errors when comparing to the end of the list + 1 } else if (linesTwo.Count > linesOne.Count) { maxLines = linesTwo.Count; linesOne.Add(new List <string>()); } else { maxLines = linesOne.Count; //If neither are larger, just use linesOne } Output.writeOut("\nFile One:"); for (int i = 0; i < linesOne.Count; i += 1) { for (int j = 0; j < linesOne[i].Count; j += 1) { Output.Write($"{linesOne[i][j]} "); //Write out the content of fileOne } } Output.writeOut("\n\nFile Two:"); for (int i = 0; i < linesTwo.Count; i += 1) { for (int j = 0; j < linesTwo[i].Count; j += 1) { Output.Write($"{linesTwo[i][j]} "); //And fileTwo } } Output.writeOut("\n\n----------\n"); for (int i = 0; i < maxLines; i += 1) //For every line in the files { Output.Write($"\n>: [Output] Line {i} "); //Write the line number IEnumerable <string> differencesTwo = linesTwo[i].Except(linesOne[i]); //Find the differences between the lines in the two text files IEnumerable <string> differencesOne = linesOne[i].Except(linesTwo[i]); if (linesOne[i].Count < linesTwo[i].Count) //If one of the lines is smaller than the otehr { for (int q = linesOne[i].Count; q < linesTwo[i].Count; q++) { linesOne[i].Add(""); //Add empty strings onto them to make them equal length. } } else if (linesTwo[i].Count < linesOne[i].Count) { for (int q = linesTwo[i].Count; q < linesOne[i].Count; q++) { linesTwo[i].Add(""); } } linesOne[i].Add(""); //And add an extra to prevent over-indexing like before linesTwo[i].Add(""); for (int j = 0; j < linesOne[i].Count; j += 1) //For every word in the line { if (linesOne[i][j] == linesTwo[i][j]) { Console.ForegroundColor = ConsoleColor.White; Output.Write($"{linesOne[i][j]} "); //If theyre the same just output them } else if (linesTwo[i][j] == "") { Console.ForegroundColor = ConsoleColor.White; Output.Write($"{linesOne[i][j]} "); //If there's nothing in fileTwo (it's gotten to the end), keep writing fileOne } else if (linesOne[i][j] == "") { Console.ForegroundColor = ConsoleColor.Green; Output.Write($"{linesTwo[i][j]} "); //If fileOne has reached the end, keep writing fileTwo in green (as it is an addition) } else if (linesOne[i][j] != linesTwo[i][j]) //If theyre not the same { if (differencesTwo.Contains(linesTwo[i][j])) //If the word is different to fileTwo { Console.ForegroundColor = ConsoleColor.Green; Output.writeOut($"{linesTwo[i][j]} "); //It was added, so write it in green if (linesTwo[i][j + 1] != linesOne[i][j]) //But if the next word isn't the same as it, it's been replaced and not inserted beforehand { Console.ForegroundColor = ConsoleColor.Red; Output.Write($"{linesOne[i][j]} "); //So also say that the current word was taken away (replaced) } } else if (differencesOne.Contains(linesOne[i][j])) //If the word is different to fileOne { Console.ForegroundColor = ConsoleColor.Red; Output.Write($"{linesOne[i][j]} "); //It's been taken away, so write it in red } } } } Console.ForegroundColor = ConsoleColor.White; //Reset the terminal to white Output.writeOut(" "); //And add a spare empty line to keep the console neat. } }