#region Arthea License /*********************************************************************** * Arthea MUD by R. Jennings (2007) http://arthea.googlecode.com/ * * By using this code you comply with the Artistic and GPLv2 Licenses. * ***********************************************************************/ #endregion using Arthea.Connections; namespace Arthea.Creation { /// <summary> /// Implements methods for string editor /// </summary> public class StringEditor { #region [rgn] Fields (2) private Connection connection; private String text; #endregion [rgn] #region [rgn] Properties (1) /// <summary> /// Gets or sets the text. /// </summary> /// <value>The text.</value> public String Text { get { return text; } set { text = value; } } #endregion [rgn] #region [rgn] Methods (9) // [rgn] Public Methods (3) /// <summary> /// Starts the string editor in append mode. /// </summary> /// <param name="con">The con.</param> /// <param name="str">The text.</param> /// <returns></returns> public StringEditor Append(Connection con, String str) { text = str; connection = con; Welcome(); connection.Write(str); if (!str.EndsWith(System.Environment.NewLine)) connection.WriteLine(); return this; } /// <summary> /// Composes this string. /// </summary> /// <param name="input">The input.</param> public void Compose(String input) { if (input[0] == '/') { String argument = input.Substring(2); switch (input[1]) { case 'q': connection.Composing = null; break; case 'c': text = string.Empty; connection.WriteLine("Text cleared."); break; case 's': ShowLines(); break; case 'S': connection.WriteLine(text); break; case 'r': String arg1 = argument.FirstArg(); if (!arg1) { connection.WriteLine("Usage: /r 'old string' 'new string'"); } else { text = text.Replace(arg1, argument.FirstArg()); } break; case 'e': int line; if (!int.TryParse(argument.FirstArg(), out line)) { connection.WriteLine("You must specify a line number."); break; } text = LineReplace(line, argument); break; case 'i': if (!int.TryParse(argument.FirstArg(), out line)) { connection.WriteLine("You must specify a line number."); break; } text = LineAdd(line, argument); break; case 'd': if (int.TryParse(argument.FirstArg(), out line)) { text = LineDelete(line); } else { text = DeleteLastLine(); } break; case 'h': connection.WriteLine("Commands:"); connection.WriteLine("/h - displays this help"); connection.WriteLine("/e <line> [text] - replaces line number with text"); connection.WriteLine("/i <line> [text] - inserts text at line number"); connection.WriteLine("/d [line] - deletes the last line or the specified line"); connection.WriteLine("/r <old> [new] - replaces old string with new string"); connection.WriteLine("/c - clears the text"); connection.WriteLine("/s - shows the text with line numbers"); connection.WriteLine("/S - shows the text without line numbers"); connection.WriteLine("/q - quits the editor"); break; default: connection.WriteLine("Invalid command. Type /h for help."); break; } } else { text += input; text += System.Environment.NewLine; } } /// <summary> /// Starts the string editor in edit mode. /// </summary> /// <param name="con">The con.</param> /// <param name="str">The text.</param> /// <returns></returns> public StringEditor Edit(Connection con, String str) { str = string.Empty; text = str; connection = con; Welcome(); return this; } // [rgn] Private Methods (6) private String DeleteLastLine() { string[] lines = text.GetLines(); String newText = new String(); for (int i = 0; i < lines.Length - 1; i++) { newText += lines[i]; newText += System.Environment.NewLine; } return newText; } private String LineAdd(int line, String addStr) { string[] lines = text.GetLines(); String newText = new String(); for (int i = 0; i < lines.Length; i++) { if ((i + 1) == line) newText += addStr; newText += lines[i]; newText += System.Environment.NewLine; } return newText; } private String LineDelete(int line) { string[] lines = text.GetLines(); String newText = new String(); for (int i = 0; i < lines.Length; i++) { if ((i + 1) != line) { newText += lines[i]; newText += System.Environment.NewLine; } } return newText; } private String LineReplace(int line, String addStr) { string[] lines = text.GetLines(); String newText = new String(); for (int i = 0; i < lines.Length; i++) { if ((i + 1) == line) newText += addStr; else newText += lines[i]; newText += System.Environment.NewLine; } return newText; } private void ShowLines() { string[] lines = text.GetLines(); for (int i = 0; i < lines.Length; i++) { connection.WriteLine("{0,2}. {1}", i + 1, lines[i]); } } private void Welcome() { connection.WriteLine("Entering the text editor. Type /? for help and /q to quit."); } #endregion [rgn] } }