/// <summary>
 /// Concatenates two ConsoleStrings together.
 /// </summary>
 /// <param name="other">The string to append.</param>
 public void Append(ConsoleString other)
 {
     foreach (var c in other.ToArray()) // ToArray() prevents concurrent modification when a and b refer to the same object
     {
         this.Add(c);
     }
 }
示例#2
0
        private static ConsoleString FormatAsTable(List<ConsoleString> columns, List<List<ConsoleString>> rows, string rowPrefix = "")
        {
            if (rows.Count == 0) return new ConsoleString();

            Dictionary<int, int> maximums = new Dictionary<int, int>();

#if __MonoCS__
            int optionDescriptionWidth = 20;
            int standardColumnWidth = 80;

            List<int> columnWidths = new List<int>();
#endif

            for (int i = 0; i < columns.Count; i++)
            {
#if __MonoCS__
                columnWidths.Add(i == 0 ? optionDescriptionWidth : standardColumnWidth - optionDescriptionWidth);
#endif
                maximums.Add(i, columns[i].Length);
            }
            for (int i = 0; i < columns.Count; i++)
            {
                foreach (var row in rows)
                {
#if __MonoCS__
                       maximums[i] = columnWidths[i];
#else
                    maximums[i] = Math.Max(maximums[i], row[i].Length);
#endif
                }
            }

            ConsoleString ret = new ConsoleString();
            int buffer = 3;

            ret += rowPrefix;
            for (int i = 0; i < columns.Count; i++)
            {
                var val = columns[i];
                while (val.Length < maximums[i] + buffer) val += " ";
                ret += val;
            }

            ret += "\n";

            foreach (var row in rows)
            {
                ret += rowPrefix;
                for (int i = 0; i < columns.Count; i++)
                {
                    var val = row[i];
                    while (val.Length < maximums[i] + buffer) val += " ";

                    ret += val;
                }
                ret += "\n";
            }

            return ret;
        }
示例#3
0
        /// <summary>
        /// Compare this ConsoleString to another ConsoleString or a plain string.
        /// </summary>
        /// <param name="obj">The ConsoleString or plain string to compare to.</param>
        /// <returns>True if equal, false otherwise.</returns>
        public override bool Equals(object obj)
        {
            if (obj is string)
            {
                return(ToString().Equals(obj as string));
            }

            ConsoleString other = obj as ConsoleString;

            if (object.ReferenceEquals(other, null))
            {
                return(false);
            }
            if (other.Length != this.Length)
            {
                return(false);
            }


            for (int i = 0; i < this.Length; i++)
            {
                if (this[i] != other[i])
                {
                    return(false);
                }
            }

            return(true);
        }
示例#4
0
        /// <summary>
        /// Replaces all occurrances of the given string with the replacement value using the specified formatting.
        /// </summary>
        /// <param name="toFind">The substring to find.</param>
        /// <param name="toReplace">The replacement value.</param>
        /// <param name="foregroundColor">The foreground color (defaults to the console's foreground color at initialization time).</param>
        /// <param name="backgroundColor">The background color (defaults to the console's background color at initialization time).</param>
        /// <returns>A new ConsoleString with the replacements.</returns>
        public ConsoleString Replace(string toFind, string toReplace, ConsoleColor?foregroundColor = null,
                                     ConsoleColor?backgroundColor = null)
        {
            ConsoleString ret = new ConsoleString();

            ret.Append(this);

            int startIndex = 0;

            while (true)
            {
                string toString     = ret.ToString();
                int    currentIndex = toString.IndexOf(toFind, startIndex);
                if (currentIndex < 0)
                {
                    break;
                }
                for (int i = 0; i < toFind.Length; i++)
                {
                    ret.RemoveAt(currentIndex);
                }
                ret.InsertRange(currentIndex,
                                toReplace.Select(c => new ConsoleCharacter(c, foregroundColor, backgroundColor)));
                startIndex = currentIndex + toReplace.Length;
            }

            return(ret);
        }
示例#5
0
        public static ConsoleString GetStyledUsage <T>(string exeName = null)
        {
            if (exeName == null)
            {
                var assembly = Assembly.GetEntryAssembly();
                if (assembly == null)
                {
                    throw new InvalidOperationException(
                              "EventStore.Rags could not determine the name of your executable automatically.  This may happen if you run GetUsage<T>() from within unit tests.  Use GetUsageT>(string exeName) in unit tests to avoid this exception.");
                }

                exeName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
            }

            ConsoleString ret = new ConsoleString();

            ret += new ConsoleString("Usage: " + exeName + " [arguments]");

            ret += Environment.NewLine;

            ret += GetOptionsUsage(typeof(T).GetProperties(), false);

            ret += Environment.NewLine;

            return(ret);
        }
示例#6
0
        private static ConsoleString FormatAsTableWindows(List <ConsoleString> columns, List <List <ConsoleString> > rows,
                                                          string rowPrefix = "")
        {
            if (rows.Count == 0)
            {
                return(new ConsoleString());
            }

            Dictionary <int, int> maximums = new Dictionary <int, int>();

            for (int i = 0; i < columns.Count; i++)
            {
                maximums.Add(i, columns[i].Length);
            }

            for (int i = 0; i < columns.Count; i++)
            {
                foreach (var row in rows)
                {
                    maximums[i] = Math.Max(maximums[i], row[i].Length);
                }
            }

            ConsoleString ret    = new ConsoleString();
            int           buffer = 3;

            ret += rowPrefix;
            for (int i = 0; i < columns.Count; i++)
            {
                var val = columns[i];
                while (val.Length < maximums[i] + buffer)
                {
                    val += " ";
                }
                ret += val;
            }

            ret += "\n";

            foreach (var row in rows)
            {
                ret += rowPrefix;
                for (int i = 0; i < columns.Count; i++)
                {
                    var val = row[i];
                    while (val.Length < maximums[i] + buffer)
                    {
                        val += " ";
                    }

                    ret += val;
                }

                ret += "\n";
            }

            return(ret);
        }
示例#7
0
        /// <summary>
        /// Get a substring of this ConsoleString starting at the given index and with the given length.
        /// </summary>
        /// <param name="start">the start index.</param>
        /// <param name="length">the number of characters to return</param>
        /// <returns>A new ConsoleString representing the substring requested.</returns>
        public ConsoleString Substring(int start, int length)
        {
            ConsoleString ret = new ConsoleString();

            for (int i = start; i < start + length; i++)
            {
                ret.Add(this[i]);
            }

            return(ret);
        }
        /// <summary>
        /// Replaces all matches of the given regular expression with the replacement value using the specified formatting.
        /// </summary>
        /// <param name="regex">The regular expression to find.</param>
        /// <param name="toReplace">The replacement value.</param>
        /// <param name="foregroundColor">The foreground color (defaults to the console's foreground color at initialization time).</param>
        /// <param name="backgroundColor">The background color (defaults to the console's background color at initialization time).</param>
        /// <returns></returns>
        public ConsoleString ReplaceRegex(string regex, string toReplace, ConsoleColor?foregroundColor = null, ConsoleColor?backgroundColor = null)
        {
            ConsoleString ret = new ConsoleString();

            ret.Append(this);
            MatchCollection matches = Regex.Matches(this.ToString(), regex);

            foreach (Match match in matches)
            {
                ret = ret.Replace(match.Value, toReplace ?? match.Value, foregroundColor, backgroundColor);
            }

            return(ret);
        }
示例#9
0
        private static ConsoleString GetOptionsUsage(IEnumerable <PropertyInfo> opts, bool ignoreActionProperties)
        {
            if (!opts.Any())
            {
                return(new ConsoleString("There are no options"));
            }

            var usageInfos = opts.Select(o => new ArgumentUsageInfo(o));

            var ret = new ConsoleString();

            var currentGroup = string.Empty;

            var longestArg = usageInfos.Select(u => u.Name)
                             .Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur);

            foreach (var usageInfo in usageInfos.OrderBy(x => x.Group))
            {
                if (currentGroup != usageInfo.Group && !string.IsNullOrEmpty(usageInfo.Group))
                {
                    currentGroup = usageInfo.Group;
                    ret         += $"{Environment.NewLine}{currentGroup}:{Environment.NewLine}";
                }

                ret += string.Format("  -{0,-" + (longestArg.Length + 2) + "}{1}", usageInfo.Name,
                                     usageInfo.Description);

                if (usageInfo.PossibleValues.Any())
                {
                    ret += $" ({string.Join(", ", usageInfo.PossibleValues)})";
                }

                ret += Environment.NewLine;
            }

            return(ret);
        }
示例#10
0
        private static ConsoleString GetOptionsUsage(IEnumerable<PropertyInfo> opts, bool ignoreActionProperties)
        {
            if (opts.Count() == 0)
            {
                return new ConsoleString("There are no options");
            }

            var usageInfos = opts.Select(o => new ArgumentUsageInfo(o));

            List<ConsoleString> columnHeaders = new List<ConsoleString>()
            {
                new ConsoleString("OPTION", ConsoleColor.Yellow),
                new ConsoleString("DESCRIPTION", ConsoleColor.Yellow),
            };

            List<List<ConsoleString>> rows = new List<List<ConsoleString>>();
            string currentGroup = String.Empty;
            foreach (ArgumentUsageInfo usageInfo in usageInfos.OrderBy(x => x.Group))
            {
                if (currentGroup != usageInfo.Group && !String.IsNullOrEmpty(usageInfo.Group))
                {
                    currentGroup = usageInfo.Group;
                    rows.Add(new List<ConsoleString>() { ConsoleString.Empty, ConsoleString.Empty, ConsoleString.Empty });
                    rows.Add(new List<ConsoleString>()
                    {
                        new ConsoleString(currentGroup),
                        ConsoleString.Empty,
                        ConsoleString.Empty
                    });
                }

                var descriptionString = new ConsoleString(usageInfo.Description);

                var aliases = usageInfo.Aliases.OrderBy(a => a.Length).ToList();
                var maxInlineAliasLength = 8;
                string inlineAliasInfo = "";

                int aliasIndex;
                for (aliasIndex = 0; aliasIndex < aliases.Count; aliasIndex++)
                {
                    var proposedInlineAliases = inlineAliasInfo == string.Empty ? aliases[aliasIndex] : inlineAliasInfo + ", " + aliases[aliasIndex];
                    if (proposedInlineAliases.Length <= maxInlineAliasLength)
                    {
                        inlineAliasInfo = proposedInlineAliases;
                    }
                    else
                    {
                        break;
                    }
                }

                if (inlineAliasInfo != string.Empty) inlineAliasInfo = " (" + inlineAliasInfo + ")";

                rows.Add(new List<ConsoleString>()
                {
                    new ConsoleString("")+("-" + usageInfo.Name + inlineAliasInfo),
                    descriptionString,
                });

                for (int i = aliasIndex; i < aliases.Count; i++)
                {
                    rows.Add(new List<ConsoleString>()
                    {
                        new ConsoleString("  "+aliases[i]),
                        ConsoleString.Empty,
                    });
                }

                foreach (var possibleValue in usageInfo.PossibleValues)
                {
                    rows.Add(new List<ConsoleString>()
                    {
                        ConsoleString.Empty,
                        new ConsoleString("  "+possibleValue),
                    });
                }
            }

            return FormatAsTable(columnHeaders, rows, "   ");
        }
示例#11
0
 /// <summary>
 /// Write this formatted character to the console.
 /// </summary>
 public void Write()
 {
     ConsoleString.WriteHelper(this.ForegroundColor, this.BackgroundColor, Value);
 }
示例#12
0
        private static ConsoleString FormatAsTableUnix(List <ConsoleString> columns, List <List <ConsoleString> > rows, string rowPrefix = "")
        {
            if (rows.Count == 0)
            {
                return(new ConsoleString());
            }

            Dictionary <int, int> maximums = new Dictionary <int, int>();

            int optionDescriptionWidth = 20;
            int standardColumnWidth    = 80;

            List <int> columnWidths = new List <int>();

            for (int i = 0; i < columns.Count; i++)
            {
                columnWidths.Add(i == 0 ? optionDescriptionWidth : standardColumnWidth - optionDescriptionWidth);
                maximums.Add(i, columns[i].Length);
            }
            for (int i = 0; i < columns.Count; i++)
            {
                foreach (var row in rows)
                {
                    maximums[i] = columnWidths[i];
                }
            }

            ConsoleString ret    = new ConsoleString();
            int           buffer = 3;

            ret += rowPrefix;
            for (int i = 0; i < columns.Count; i++)
            {
                var val = columns[i];
                while (val.Length < maximums[i] + buffer)
                {
                    val += " ";
                }
                ret += val;
            }

            ret += "\n";

            foreach (var row in rows)
            {
                ret += rowPrefix;
                for (int i = 0; i < columns.Count; i++)
                {
                    var val = row[i];
                    while (val.Length < maximums[i] + buffer)
                    {
                        val += " ";
                    }

                    ret += val;
                }
                ret += "\n";
            }

            return(ret);
        }