Ejemplo n.º 1
0
        /// <summary>
        /// Write the items list using the given format.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="commander"></param>
        /// <param name="items"></param>
        /// <param name="writeFormat"></param>
        public static void WriteList <T>(this CommanderBase commander, IEnumerable <T> items, Func <T, string> writeFormat)
        {
            foreach (var i in items)
            {
                if (writeFormat == null)
                {
                    commander.WriteLine(i.ToString());
                }
                else
                {
                    commander.WriteLine(writeFormat(i));
                }
            }

            commander.Info($"There are {items.Count()} items found.");
        }
        /// <summary>
        /// Request the user to pick an item from the list given.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="commander"></param>
        /// <param name="items"></param>
        /// <param name="defaultIdx"></param>
        /// <returns></returns>
        public static T requestItem <T>(this CommanderBase commander, IEnumerable <T> items, Func <T, string> writeFormat, string message = "item", int defaultIdx = 0)
        {
            var idx = 0;

            foreach (var i in items)
            {
                if (writeFormat == null)
                {
                    commander.WriteLine($"[{idx}]: {i.ToString()}");
                }
                else
                {
                    commander.WriteLine($"[{idx}]: {writeFormat(i)}");
                }

                idx++;
            }

            var selectedItem = commander.requestValue(message, defaultIdx);

            return(items.ElementAt(selectedItem));
        }
Ejemplo n.º 3
0
        public static void WriteAsTable <T>(this CommanderBase commander, IEnumerable <T> items, IDictionary <string, Func <T, object> > columns, string nullReplacementValue = "NULL")
        {
            // Dictionary to hold the max lenght of a column-value.
            var lenDict = new Dictionary <string, int>();

            foreach (var c in columns)
            {
                // Determine the max lenght of values of the column
                var len = ((items == null | !items.Any())
                        ? 0
                        : items.Max(i => {
                    var r = c.Value.Invoke(i);
                    return((r == null) ? nullReplacementValue.Length : r.ToString().Length);
                })
                           );

                // Store the max len
                lenDict.Add(c.Key, Math.Max(len, c.Key.Length));
            }

            // Title
            var lenTotal = lenDict.Values.Sum()
                           + ((lenDict.Count() - 1) * 3);
            var titleLen = typeof(T).Name.Length;

            commander.WriteLine($"| {typeof(T).Name}{new String(' ', lenTotal - titleLen)} |", ConsoleColor.White);

            // Table Headers
            foreach (var header in columns)
            {
                var len = lenDict[header.Key];
                commander.Write($"| {header.Key.PadRight(len)} ", ConsoleColor.White);
            }
            commander.WriteLine($"|", ConsoleColor.White);

            // Separator-line/row
            foreach (var i in columns)
            {
                commander.Write($"|{new String('=', lenDict[i.Key] + 2)}");
            }
            commander.WriteLine($"|");

            // Values
            foreach (var i in items)
            {
                foreach (var c in columns)
                {
                    // Get the value first
                    var result = c.Value.Invoke(i);
                    // .. check if not null
                    //var value = (result == null) ? string.Empty : result;
                    var value = (result == null)
                        ? nullReplacementValue
                        : result.ToString()
                                .Replace(Environment.NewLine, string.Empty);

                    commander.Write($"| {value.ToString().PadRight(lenDict[c.Key])} ");
                }
                commander.WriteLine($"|");
            }

            // Footer
            var message = $"Lines: {items.Count()}, Columns: {columns.Count}";

            commander.WriteLine($"| {message}{new String(' ', lenTotal - message.Length)} |", ConsoleColor.White);
        }
Ejemplo n.º 4
0
        //
        // Specifics
        //

        public static void WriteEmptyLine(this CommanderBase commander)
        {
            commander.WriteLine(string.Empty);
        }
Ejemplo n.º 5
0
 public static void Failed(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Red);
 }
Ejemplo n.º 6
0
 //
 // (Un-)Succesfull writers
 //
 public static void Success(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Green);
 }
Ejemplo n.º 7
0
 public static void Write(this CommanderBase commander, int message, ConsoleColor color = ConsoleColor.Gray)
 {
     commander.WriteLine(message.ToString(), color);
 }
Ejemplo n.º 8
0
 public static void Warning(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Yellow);
 }
Ejemplo n.º 9
0
 public static void Debug(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.White);
 }
Ejemplo n.º 10
0
 public static void Verbose(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Magenta);
 }
Ejemplo n.º 11
0
 public static void Info(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Cyan);
 }
Ejemplo n.º 12
0
 //
 // Logging-like write methods.
 //
 public static void Trace(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Blue);
 }