public static string requestValue(this CommanderBase commander, string message, string defaultValue = null, IEnumerable <string> options = null)
        {
            // Console.Write is used here because we want to stay on the current line.

            if (options != null)
            {
                commander.Write($"{message} ({defaultValue.ToString()}) [{string.Join(", ", options)}]: ");
            }
            else if (defaultValue != null)
            {
                commander.Write($"{message} ({defaultValue.ToString()}): ");
            }
            else
            {
                commander.Write($"{message}: ");
            }

            var userVal = commander.GetInput();

            if (string.IsNullOrEmpty(userVal))
            {
                return(defaultValue);
            }

            return(userVal);
        }
 /// <summary>
 /// Requests the user to choose a month number
 /// </summary>
 /// <param name="commander"></param>
 /// <param name="defaultMonth">By default the current month</param>
 /// <returns></returns>
 public static int requestMonth(this CommanderBase commander, int defaultMonth = 0)
 {
     if (defaultMonth == 0)
     {
         defaultMonth = DateTime.Now.Month;
     }
     return(commander.requestValue("Month:", defaultMonth, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }.ToList()));
 }
        public static bool requestBool(this CommanderBase commander, string message = "Yes or No?:", string trueText = "Yes", string falseText = "No", bool defaultValue = true)
        {
            var boolList = new Dictionary <string, bool>()
            {
                { falseText, false },
                { trueText, true }
            };
            var result = commander.requestItem(boolList.ToList(), kvp => $"{kvp.Key}", message, boolList.ToList().FindIndex(k => k.Value == defaultValue));

            return(result.Value);
        }
        /// <summary>
        /// Asks the user for input. The user can see the options to pick from, and a default value is suggested.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="commander"></param>
        /// <param name="message"></param>
        /// <param name="defaultValue"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static int requestValue <T>(this CommanderBase commander, string message, T defaultValue, IEnumerable <int> options = null)
        {
            string sval = string.Empty;

            if (options == null)
            {
                sval = commander.requestValue(message, defaultValue.ToString());
            }
            else
            {
                sval = commander.requestValue(message, defaultValue.ToString(), options.Select(v => v.ToString()).ToList());
            }

            return(int.Parse(sval));
        }
Example #5
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));
        }
Example #7
0
        //
        // Specifics
        //

        public static void WriteEmptyLine(this CommanderBase commander)
        {
            commander.WriteLine(string.Empty);
        }
Example #8
0
 public static void Failed(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Red);
 }
Example #9
0
 //
 // (Un-)Succesfull writers
 //
 public static void Success(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Green);
 }
Example #10
0
 public static void Write(this CommanderBase commander, int message, ConsoleColor color = ConsoleColor.Gray)
 {
     commander.WriteLine(message.ToString(), color);
 }
Example #11
0
 public static void Warning(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Yellow);
 }
Example #12
0
 public static void Debug(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.White);
 }
Example #13
0
 public static void Info(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Cyan);
 }
Example #14
0
 //
 // Logging-like write methods.
 //
 public static void Trace(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Blue);
 }
 /// <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, string message = "item", int defaultIdx = 0)
 {
     return(commander.requestItem(items, null, message, defaultIdx));
 }
Example #16
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);
        }
Example #17
0
 public static void Verbose(this CommanderBase commander, string message)
 {
     commander.WriteLine(message, ConsoleColor.Magenta);
 }