From a0c4746c27656d6617853890d270072c13843c64 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 2 Jul 2017 17:22:36 -0400 Subject: add list_item_types command (#302) --- .../Commands/Player/ListItemTypesCommand.cs | 53 ++++++++++++++++++++++ .../Framework/Commands/Player/ListItemsCommand.cs | 39 ---------------- 2 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs (limited to 'src/TrainerMod/Framework/Commands/Player') diff --git a/src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs b/src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs new file mode 100644 index 00000000..5f14edbb --- /dev/null +++ b/src/TrainerMod/Framework/Commands/Player/ListItemTypesCommand.cs @@ -0,0 +1,53 @@ +using System.Linq; +using StardewModdingAPI; +using TrainerMod.Framework.ItemData; + +namespace TrainerMod.Framework.Commands.Player +{ + /// A command which list item types. + internal class ListItemTypesCommand : TrainerCommand + { + /********* + ** Properties + *********/ + /// Provides methods for searching and constructing items. + private readonly ItemRepository Items = new ItemRepository(); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + public ListItemTypesCommand() + : base("list_item_types", "Lists item types you can filter in other commands.\n\nUsage: list_item_types") { } + + /// Handle the command. + /// Writes messages to the console and log file. + /// The command name. + /// The command arguments. + public override void Handle(IMonitor monitor, string command, ArgumentParser args) + { + // validate + if (!Context.IsWorldReady) + { + monitor.Log("You need to load a save to use this command.", LogLevel.Error); + return; + } + + // handle + ItemType[] matches = + ( + from item in this.Items.GetAll() + orderby item.Type.ToString() + select item.Type + ) + .Distinct() + .ToArray(); + string summary = "Searching...\n"; + if (matches.Any()) + monitor.Log(summary + this.GetTableString(matches, new[] { "type" }, val => new[] { val.ToString() }), LogLevel.Info); + else + monitor.Log(summary + "No item types found.", LogLevel.Info); + } + } +} diff --git a/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs b/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs index 68adf8c2..30c3de3b 100644 --- a/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs +++ b/src/TrainerMod/Framework/Commands/Player/ListItemsCommand.cs @@ -77,44 +77,5 @@ namespace TrainerMod.Framework.Commands.Player yield return weapon; } } - - /// Get an ASCII table for a set of tabular data. - /// The data type. - /// The data to display. - /// The table header. - /// Returns a set of fields for a data value. - private string GetTableString(IEnumerable data, string[] header, Func getRow) - { - // get table data - int[] widths = header.Select(p => p.Length).ToArray(); - string[][] rows = data - .Select(item => - { - string[] fields = getRow(item); - if (fields.Length != widths.Length) - throw new InvalidOperationException($"Expected {widths.Length} columns, but found {fields.Length}: {string.Join(", ", fields)}"); - - for (int i = 0; i < fields.Length; i++) - widths[i] = Math.Max(widths[i], fields[i].Length); - - return fields; - }) - .ToArray(); - - // render fields - List lines = new List(rows.Length + 2) - { - header, - header.Select((value, i) => "".PadRight(widths[i], '-')).ToArray() - }; - lines.AddRange(rows); - - return string.Join( - Environment.NewLine, - lines.Select(line => string.Join(" | ", - line.Select((field, i) => field.PadRight(widths[i], ' ')).ToArray()) - ) - ); - } } } -- cgit