Exemple #1
0
		public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite )
		{
			MdConfig mdc = new MdConfig();

			string[] allLines;
			List<string> lines;
			string lastType = "";
			int lineNumber;
			int padLen;
			int width = Console.WindowWidth - 1;
			string colPad;
			Regex urlRegex, boldRegex;
			Match m;

			allLines = File.ReadAllLines(fileName);
			lineStart = Math.Max(lineStart, 0);
			lineNumber = 0;
			padLen = catOptions.showLineNumbers ? 3 : 0;
			if (linesToWrite < 0) {
				linesToWrite = long.MaxValue;
			}
			colPad = string.Empty;

			urlRegex = new Regex(@"\[(?<url>.+)\]\((?<title>.*)\)", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
			boldRegex = new Regex(@"__(?<text>[^_]+)__", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);

			//if ((m = urlRegex.Match("some content here.. Here is an [http://google.com/file](url)..")).Success) {
			//	Console.Write("found a match!");
			//	Console.Write(m.Groups.Count);
			//}

			Console.ForegroundColor = mdc.defaultColor;
			Console.BackgroundColor = mdc.defaultBackground;

			lines = new List<string>();
			for (int lineIndex = Math.Min(allLines.Length, Math.Max(0, lineStart)); lineIndex < Math.Min(lineStart + linesToWrite, allLines.Length); lineIndex++) {
				lines.Add(allLines[lineIndex]);
			}

			for (int lineIndex = 0; lineIndex < lines.Count; lineIndex++) {
				string l = lines[lineIndex];
				lineNumber++;

				if (catOptions.showLineNumbers) {
					Console.BackgroundColor = catOptions.lineNumBackColor;
					Console.ForegroundColor = catOptions.lineNumForeColor;
					Console.Write("{0," + padLen + "}", lineNumber);
					Console.BackgroundColor = catOptions.defaultBackColor;
					Console.ForegroundColor = catOptions.defaultForeColor;
				}

				if (l.StartsWith("####")) {
					// h4 and above use the same style..
					Console.ForegroundColor = mdc.h4;
					Console.WriteLine(l);
					Console.ForegroundColor = mdc.defaultColor;
					lastType = "h4";
					continue;
				}
				if (l.StartsWith("###")) {
					Console.ForegroundColor = mdc.h3;
					Console.WriteLine(l);
					Console.ForegroundColor = mdc.defaultColor;
					lastType = "h3";
					continue;
				}
				if (l.StartsWith("##")) {
					Console.ForegroundColor = mdc.h2;
					Console.WriteLine(l);
					Console.ForegroundColor = mdc.defaultColor;
					lastType = "h2";
					continue;
				}
				if (l.StartsWith("#")) {
					Console.ForegroundColor = mdc.h1;
					Console.WriteLine(l);
					Console.ForegroundColor = mdc.defaultColor;
					lastType = "h1";
					continue;
				}

#if false
				ColoredString s = new ColoredString();
				bool modified = true;
				bool urlRegexDone = false;

				//s = ColoredString.Parse(l);

				//while (modified) {
				//	modified = false;

				//	if (!urlRegexDone) {
				//		m = urlRegex.Match(s._value.ToString());
				//		if (m.Success) {
				//			//Console.ForegroundColor = mdc.debugColor;
				//			//Console.WriteLine(">" + l);
				//			//Console.WriteLine();
				//			//for (int i = 0; i < m.Groups.Count; i++) {
				//			//	Console.WriteLine(">" + i + " " + m.Groups[i]);
				//			//}
				//			//Console.WriteLine();
				//			//foreach (Capture c in m.Captures) {
				//			//	Console.WriteLine(">" + c.Index + " " + c.Value);
				//			//}
				//			//Console.WriteLine();
				//			//Console.WriteLine(">" + m.Index + " " + m.Value);

				//			//Console.ForegroundColor = mdc.defaultColor;
				//			//Console.Write(l.Substring(0, m.Index));
				//			//Console.ForegroundColor = mdc.linkSymbols;
				//			//Console.Write("[");
				//			//Console.ForegroundColor = mdc.linkUrl;
				//			//Console.Write(m.Groups[1]);
				//			//Console.ForegroundColor = mdc.linkSymbols;
				//			//Console.Write("](");
				//			//Console.ForegroundColor = mdc.linkTitle;
				//			//Console.Write(m.Groups[2]);
				//			//Console.ForegroundColor = mdc.linkSymbols;
				//			//Console.Write(")");
				//			//Console.ForegroundColor = mdc.defaultColor;
				//			//Console.Write(l.Substring(m.Index + m.Groups[1].Length + m.Groups[2].Length + 4));

				//			//s.Clear()
				//			// .Append("{").Append(mdc.defaultColor).Append("}")
				//			// .Append(l.Substring(0, m.Index))
				//			// .Append('{').Append(mdc.linkSymbols).Append("}")
				//			// .Append('[')
				//			// .Append('{').Append(mdc.linkUrl).Append("}")
				//			// .Append(m.Groups[1])
				//			// .Append('{').Append(mdc.linkSymbols).Append("}")
				//			// .Append("](")
				//			// .Append('{').Append(mdc.linkTitle).Append("}")
				//			// .Append(m.Groups[2])
				//			// .Append('{').Append(mdc.linkSymbols).Append("}")
				//			// .Append(")")
				//			// .Append('{').Append(mdc.defaultColor).Append("}")
				//			// .Append(l.Substring(m.Index + m.Groups[1].Length + m.Groups[2].Length + 4));

				//			s.Clear()
				//			 .Append(l.Substring(0, m.Index), mdc.defaultColor)
				//			 .Append('[', mdc.linkSymbols)
				//			 .Append(m.Groups[1], mdc.linkUrl)
				//			 .Append("](", mdc.linkSymbols)
				//			 .Append(m.Groups[2], mdc.linkTitle)
				//			 .Append(")", mdc.linkSymbols)
				//			 .Append(l.Substring(m.Index + m.Groups[1].Length + m.Groups[2].Length + 4), mdc.defaultColor);

				//			l = s.ToString();
				//			modified = true;
				//		}

				//		urlRegexDone = true;
				//	}
				//}
#endif

				string lt = l.Trim();

				if (lt.Length == 0) {
					if (lastType == "code" && (lineIndex < lines.Count - 1 && lines[lineIndex + 1].StartsWith("    "))) {
						l = "    ";
					} else {
						Console.WriteLine(l);
						lastType = "";
						continue;
					}
				}

				if (l.StartsWith("    ")) {
					Console.BackgroundColor = mdc.codePrefixBackground;
					Console.Write("    ");
					Console.BackgroundColor = mdc.defaultBackground;

					l = l.Substring(4);
					lt = l.Trim();
					//Console.ForegroundColor = mdc.debugColor;
					//Console.WriteLine("'" + l + "'");

					if (lt.StartsWith(">") || lt.StartsWith("$")) {
						int i = l.IndexOfAny(new char[] { '>', '$' });
						Console.ForegroundColor = mdc.codeConsoleCmdSymbol;
						Console.Write(l.Substring(0, i + 1));
						Console.ForegroundColor = mdc.defaultColor;
						Console.WriteLine(l.Substring(i + 1));
					} else {
						Console.WriteLine(l);
					}

					lastType = "code";
					continue;
				}

				m = urlRegex.Match(l);
				if (m.Success) {
					//Console.ForegroundColor = mdc.debugColor;
					//Console.WriteLine(">" + l);
					//Console.WriteLine();
					//for (int i = 0; i < m.Groups.Count; i++) {
					//	Console.WriteLine(">" + i + " " + m.Groups[i]);
					//}
					//Console.WriteLine();
					//foreach (Capture c in m.Captures) {
					//	Console.WriteLine(">" + c.Index + " " + c.Value);
					//}
					//Console.WriteLine();
					//Console.WriteLine(">" + m.Index + " " + m.Value);

					Console.ForegroundColor = mdc.defaultColor;
					Console.Write(l.Substring(0, m.Index));
					Console.ForegroundColor = mdc.linkSymbols;
					Console.Write("[");
					Console.ForegroundColor = mdc.linkUrl;
					Console.Write(m.Groups[1]);
					Console.ForegroundColor = mdc.linkSymbols;
					Console.Write("](");
					Console.ForegroundColor = mdc.linkTitle;
					Console.Write(m.Groups[2]);
					Console.ForegroundColor = mdc.linkSymbols;
					Console.Write(")");
					Console.ForegroundColor = mdc.defaultColor;
					Console.Write(l.Substring(m.Index + m.Groups[1].Length + m.Groups[2].Length + 4));

					lastType = "url";
					continue;
				}

				m = boldRegex.Match(l);
				if (m.Success) {
					//foreach (Capture c in m.Captures) {
					//	Console.WriteLine(">" + c.Index + " " + c.Value);
					//}

					Console.ForegroundColor = mdc.defaultColor;
					Console.Write(Text.Wrap(l.Substring(0, m.Index), width, 0));
					Console.ForegroundColor = mdc.emphasisText;
					Console.Write(m.Groups[1]);
					Console.ForegroundColor = mdc.defaultColor;
					Console.Write(Text.Wrap(l.Substring(m.Index + m.Groups[1].Length + 4), new int[] { width - Console.CursorLeft, width }, new int[] { 0 }));

					lastType = "bold";
					continue;
				}

				int indentation;
				if (l.StartsWith(" ")) {
					indentation = 0;
					foreach (char c in l) {
						if (c == ' ') {
							indentation++;
						} else {
							break;
						}
					}
				} else {
					indentation = 0;
				}

				Console.WriteLine(Text.Wrap(l, width, indentation));
				lastType = "text";
			}

			Console.ForegroundColor = mdc.originalColor;
			Console.BackgroundColor = mdc.originalBackground;

			return true;
		}
		public static void Write( ColoredString coloredString )
		{
			if (coloredString == null || coloredString.Length == 0) {
				return;
			}

			ConsoleColor backupForeColor = Console.ForegroundColor,
						 backupBackColor = Console.BackgroundColor;
			int count = 0;
			string value = coloredString._value.ToString();

			foreach (KeyValuePair<int, ConsoleColor?> pair in coloredString._foreColors) {
				if (pair.Value.HasValue) {
					Console.ForegroundColor = pair.Value.Value;
				}
				if (coloredString._backColors[pair.Key].HasValue) {
					Console.BackgroundColor = coloredString._backColors[pair.Key].Value;
				}

				if (count == 0 && pair.Key > 0) {
					Console.Write(value.Substring(0, pair.Key - 1));
				}

				Console.Write(value.Substring(0, pair.Key));
			}

			Console.ForegroundColor = backupForeColor;
			Console.BackgroundColor = backupBackColor;
		}
		public static void WriteLine( ColoredString coloredString )
		{
			ColoredString.Write(coloredString);
			Console.WriteLine();
		}
		public static ColoredString Parse( string Value )
		{
			ColoredString cs;
			List<string> sections;
			String[] parts;
			int len;

			cs = new ColoredString();

			foreach (String color in Enum.GetNames(typeof(ConsoleColor))) {
				Value = Regex.Replace(Value, @"\{" + color + @"\}", "\r\n@={" + color + @"}", RegexOptions.IgnoreCase);
			}

			sections = new List<string>(Value.Split(new string[] { "\r\n@=" }, StringSplitOptions.RemoveEmptyEntries));

			foreach (String val in sections) {
				parts = val.Split(new Char[] { '}' }, 2);
				parts[0] = parts[0].Substring(1); // remove the preceding '{'

				//coloredStrings.Add(parts[1], (ConsoleColor)Enum.Parse(typeof(ConsoleColor), parts[0])));
				len = cs._value.Length;
				cs._foreColors.Add(len, (ConsoleColor)Enum.Parse(typeof(ConsoleColor), parts[0]));
				cs._backColors.Add(len, null);
				cs._value.Append(parts[1]);
			}

			return cs;
		}