Esempio n. 1
0
		public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite )
		{
			int lineNumber;
			int padLen;
			int winWidth = Console.WindowWidth - 1;
			string l, lt;

			lineStart = Math.Max(lineStart, 0);
			lineNumber = 0;
			padLen = catOptions.showLineNumbers ? 3 : 0;
			if (linesToWrite < 0) {
				linesToWrite = long.MaxValue;
			}

			using (StreamReader reader = File.OpenText(fileName)) {
				while (!reader.EndOfStream) {
					l = reader.ReadLine();
					lt = l.Trim();
					lineNumber++;

					if (lineNumber < lineStart) {
						continue;
					}

					if (catOptions.ignoreLines.Length > 0 && l.StartsWith(catOptions.ignoreLines, StringComparison.CurrentCultureIgnoreCase)) {
						continue;
					} else if (catOptions.ignoreBlankLines && l.Length == 0) {
						continue;
					} else if (catOptions.ignoreWhitespaceLines && lt.Length == 0) {
						continue;
					}

					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 (lt.Length > 0) {
						if (catOptions.wrapText) {
							Console.WriteLine(Bricksoft.PowerCode.Text.Wrap(l.TrimEnd(), winWidth, 0, padLen));
						} else {
							Console.WriteLine(l.TrimEnd());
						}
					} else {
						Console.WriteLine("  ");
					}

					if (lineNumber >= linesToWrite) {
						break;
					}
				}

				reader.Close();
			}

			return true;
		}
Esempio n. 2
0
		public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite )
		{
			BatConfig bdc = new BatConfig();

			int lineNumber;
			int padLen;
			int winWidth = Console.WindowWidth - 1;
			string l, lt;
			Regex varRegex, argRegex;
			Match m;

			lineStart = Math.Max(lineStart, 0);
			lineNumber = 0;
			padLen = catOptions.showLineNumbers ? 3 : 0;
			if (linesToWrite < 0) {
				linesToWrite = long.MaxValue;
			}

			varRegex = new Regex(@"[\!%](?<var>[^%]+)[\!%]", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
			argRegex = new Regex(@"%(?<var>[%]*[\*_~a-zA-Z0-9]*)", RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);


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

			using (StreamReader reader = File.OpenText(fileName)) {
				while (!reader.EndOfStream) {
					if (lineNumber >= linesToWrite) {
						break;
					}

					l = reader.ReadLine();
					lt = l.Trim();
					lineNumber++;

					if (lineNumber < lineStart) {
						continue;
					}

					if (catOptions.ignoreLines.Length > 0 && l.StartsWith(catOptions.ignoreLines, StringComparison.CurrentCultureIgnoreCase)) {
						continue;
					} else if (catOptions.ignoreBlankLines && l.Length == 0) {
						continue;
					} else if (catOptions.ignoreWhitespaceLines && lt.Length == 0) {
						continue;
					}

					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 (lt.StartsWith(":")) {
						int i = l.IndexOf(' ');
						if (i == -1) {
							i = l.Length;
						}

						Console.ForegroundColor = bdc.func;
						Console.Write(l.Substring(0, i));
						Console.ForegroundColor = bdc.defaultColor;
						Console.WriteLine(l.Substring(i).TrimEnd());
						continue;
					}

					//if (lt.StartsWith("@")) {
					//	Console.ForegroundColor = bdc.echoSymbol;
					//	if (catOptions.wrapText) {
					//		Console.WriteLine(Bricksoft.PowerCode.Text.Wrap(l.TrimEnd(), winWidth, 0, padLen));
					//	} else {
					//		Console.WriteLine(l.TrimEnd());
					//	}
					//	Console.ForegroundColor = bdc.defaultColor;
					//	continue;
					//}

					if (lt.IndexOf("rem ") > -1) {
						int i = l.IndexOf("rem ");
						if (i == -1) {
							i = l.Length;
						}

						Console.ForegroundColor = bdc.defaultColor;
						Console.Write(l.Substring(0, i));
						Console.ForegroundColor = bdc.comment;
						if (catOptions.wrapText) {
							Console.WriteLine(Bricksoft.PowerCode.Text.Wrap(l.Substring(i).TrimEnd(), winWidth, 0, padLen));
						} else {
							Console.WriteLine(l.Substring(i).TrimEnd());
						}
						Console.ForegroundColor = bdc.defaultColor;
						continue;
					}

					m = varRegex.Match(l);
					if (m.Success) {
						Console.ForegroundColor = bdc.defaultColor;
						Console.Write(l.Substring(0, m.Index));
						Console.ForegroundColor = bdc.varSymbol;
						Console.Write(m.Groups[0].Value[0]);
						Console.ForegroundColor = bdc.varName;
						Console.Write(m.Groups[1]);
						Console.ForegroundColor = bdc.varSymbol;
						Console.Write(m.Groups[0].Value[0]);
						Console.ForegroundColor = bdc.defaultColor;
						Console.WriteLine(l.Substring(m.Index + m.Groups[1].Length + 2));
						continue;
					}

					m = argRegex.Match(l);
					if (m.Success) {
						Console.ForegroundColor = bdc.defaultColor;
						Console.Write(l.Substring(0, m.Index));
						Console.ForegroundColor = bdc.varSymbol;
						Console.Write("%");
						Console.ForegroundColor = bdc.varName;
						Console.Write(m.Groups[1]);
						Console.ForegroundColor = bdc.defaultColor;
						Console.WriteLine(l.Substring(m.Index + m.Groups[1].Length + 1));
						continue;
					}

					if (lt.Length > 0) {
						if (catOptions.wrapText) {
							Console.WriteLine(Bricksoft.PowerCode.Text.Wrap(l.TrimEnd(), winWidth, 0, padLen));
						} else {
							Console.WriteLine(l.TrimEnd());
						}
					} else {
						Console.WriteLine("  ");
					}
				}

				reader.Close();
			}

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

			return true;
		}
Esempio n. 3
0
		public bool Cat( CatOptions catOptions, string fileName )
		{
			return Cat(catOptions, fileName, 0, long.MaxValue);
		}
Esempio n. 4
0
		public bool CanCat( CatOptions catOptions, string fileName )
		{
			string ext = Path.GetExtension(fileName);
			return ext.Equals(".bat", StringComparison.CurrentCultureIgnoreCase)
				|| ext.Equals(".cmd", StringComparison.CurrentCultureIgnoreCase);
		}
Esempio n. 5
0
		public bool CanCat( CatOptions catOptions, string fileName )
		{
			return Path.GetExtension(fileName).Equals(".cs", StringComparison.CurrentCultureIgnoreCase);
		}
Esempio n. 6
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;
		}
Esempio n. 7
0
		public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite )
		{
			string ext = Path.GetExtension(fileName);
			string[] lines;
			Dictionary<int, int> maxLen = new Dictionary<int, int>();
			List<string[]> lineColumns = new List<string[]>();
			int lineNumber;
			int padLen;
			int winWidth = Console.WindowWidth - 1;
			string colPad;

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

			DelimitedFileReader f = new DelimitedFileReader();
			f.OpenFile(fileName, new FileType(ext.Substring(1), ",", new string[] { ext }, ext.Substring(1)));

			// Get the maxlength of each column.
			for (int i = Math.Min(lines.Length, Math.Max(0, lineStart)); i < Math.Min(lineStart + linesToWrite, lines.Length); i++) {
				string[] ls = f.ParseLine(lines[i]);
				lineColumns.Add(ls);
				for (int c = 0; c < ls.Length; c++) {
					if (maxLen.ContainsKey(c)) {
						maxLen[c] = Math.Max(maxLen[c], ls[c].Length + 1);
					} else {
						maxLen.Add(c, ls[c].Length + 1);
					}
				}
			}

			StringBuilder res = new StringBuilder();

			foreach (string[] ls in lineColumns) {
				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;
				}

				res.Clear();
				for (int c = 0; c < ls.Length; c++) {
					//res.AppendFormat("{0,-" + maxLen[c] + "}, ", ls[c]);
					//res.AppendFormat("{0,-" + maxLen[c] + "} ", ls[c] + ",");
					res.AppendFormat("{0,-" + maxLen[c] + "}, ", ls[c].TrimStart());
				}

				Console.WriteLine(res.ToString().TrimEnd(' ', ','));
				//Console.WriteLine(res.ToString().TrimEnd().TrimEnd(','));
			}

			return true;
		}
Esempio n. 8
0
		public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite )
		{
			csv c = new csv();
			return c.Cat(catOptions, fileName, lineStart, linesToWrite);
		}