Skip to content

Commit

Permalink
Debug option to view mod's metadata
Browse files Browse the repository at this point in the history
- Debug parameters in more classes
- Message box enhancements:
  - Scrollable
  - Alignment param
  - Auto width
  - Preserve indentation for short lines
- Up/down arrow support in install screen popup
- Metadata functions in AvailableModule and Registry and the registry's
interface
- Allow rebinding screen container keys
  • Loading branch information
HebaruSan committed Dec 3, 2017
1 parent b13c38a commit d0b5afb
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 39 deletions.
8 changes: 6 additions & 2 deletions ConsoleUI/DependencyScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public class DependencyScreen : ConsoleScreen {
/// <param name="mgr">KSP manager containing instances</param>
/// <param name="cp">Plan of mods to add and remove</param>
/// <param name="rej">Mods that the user saw and did not select, in this pass or a previous pass</param>
public DependencyScreen(KSPManager mgr, ChangePlan cp, HashSet<string> rej) : base()
/// <param name="dbg">True if debug options should be available, false otherwise</param>
public DependencyScreen(KSPManager mgr, ChangePlan cp, HashSet<string> rej, bool dbg) : base()
{
debug = dbg;
manager = mgr;
plan = cp;
registry = RegistryManager.Instance(manager.CurrentInstance).registry;
Expand Down Expand Up @@ -79,7 +81,8 @@ public DependencyScreen(KSPManager mgr, ChangePlan cp, HashSet<string> rej) : ba
if (dependencyList.Selection != null) {
LaunchSubScreen(new ModInfoScreen(
manager, plan,
registry.LatestAvailable(dependencyList.Selection.identifier, manager.CurrentInstance.VersionCriteria())
registry.LatestAvailable(dependencyList.Selection.identifier, manager.CurrentInstance.VersionCriteria()),
debug
));
}
return true;
Expand Down Expand Up @@ -207,6 +210,7 @@ private string StatusSymbol(string identifier)
private IRegistryQuerier registry;
private KSPManager manager;
private ChangePlan plan;
private bool debug;

private Dictionary<string, Dependency> dependencies = new Dictionary<string, Dependency>();
private ConsoleListBox<Dependency> dependencyList;
Expand Down
7 changes: 5 additions & 2 deletions ConsoleUI/InstallScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public class InstallScreen : ProgressScreen {
/// </summary>
/// <param name="mgr">KSP manager containing instances</param>
/// <param name="cp">Plan of mods to install or remove</param>
public InstallScreen(KSPManager mgr, ChangePlan cp)
/// <param name="dbg">True if debug options should be available, false otherwise</param>
public InstallScreen(KSPManager mgr, ChangePlan cp, bool dbg)
: base(
"Installing, Updating, and Removing Mods",
"Calculating..."
)
{
debug = dbg;
manager = mgr;
plan = cp;
}
Expand All @@ -45,7 +47,7 @@ public override void Run(Action process = null)
// CmdLine assumes recs and ignores sugs
if (plan.Install.Count > 0) {
// Track previously rejected optional dependencies and don't prompt for them again.
DependencyScreen ds = new DependencyScreen(manager, plan, rejected);
DependencyScreen ds = new DependencyScreen(manager, plan, rejected, debug);
if (ds.HaveOptions()) {
LaunchSubScreen(ds);
}
Expand Down Expand Up @@ -142,6 +144,7 @@ private void OnModInstalled(CkanModule mod)

private KSPManager manager;
private ChangePlan plan;
private bool debug;
}

}
26 changes: 25 additions & 1 deletion ConsoleUI/ModInfoScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class ModInfoScreen : ConsoleScreen {
/// <param name="mgr">KSP manager containing game instances</param>
/// <param name="cp">Plan of other mods to be added or removed</param>
/// <param name="m">The module to display</param>
public ModInfoScreen(KSPManager mgr, ChangePlan cp, CkanModule m)
/// <param name="dbg">True if debug options should be available, false otherwise</param>
public ModInfoScreen(KSPManager mgr, ChangePlan cp, CkanModule m, bool dbg)
{
debug = dbg;
mod = m;
manager = mgr;
plan = cp;
Expand Down Expand Up @@ -145,6 +147,14 @@ public ModInfoScreen(KSPManager mgr, ChangePlan cp, CkanModule m)
() => LaunchURL(mod.resources.curse)
));
}
if (debug) {
opts.Add(null);
opts.Add(new ConsoleMenuOption(
"DEBUG: View metadata", "", "Display the full registry data for this mod",
true,
ViewMetadata
));
}

if (opts.Count > 0) {
mainMenu = new ConsolePopupMenu(opts);
Expand All @@ -155,6 +165,19 @@ public ModInfoScreen(KSPManager mgr, ChangePlan cp, CkanModule m)
CenterHeader = () => "Mod Details";
}

private bool ViewMetadata()
{
ConsoleMessageDialog md = new ConsoleMessageDialog(
$"\"{mod.identifier}\": {registry.GetAvailableMetadata(mod.identifier)}",
new List<string> {"OK"},
() => $"{mod.name} Metadata",
TextAlign.Left
);
md.Run();
DrawBackground();
return true;
}

private bool LaunchURL(Uri u)
{
// I'm getting error output on Linux, because this runs xdg-open which
Expand Down Expand Up @@ -491,6 +514,7 @@ private void Download()
private IRegistryQuerier registry;
private ChangePlan plan;
private CkanModule mod;
private bool debug;
}

}
6 changes: 3 additions & 3 deletions ConsoleUI/ModListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public ModListScreen(KSPManager mgr, bool dbg)
);
moduleList.AddBinding(Keys.Enter, (object sender) => {
if (moduleList.Selection != null) {
LaunchSubScreen(new ModInfoScreen(manager, plan, moduleList.Selection));
LaunchSubScreen(new ModInfoScreen(manager, plan, moduleList.Selection, debug));
}
return true;
});
Expand Down Expand Up @@ -329,7 +329,7 @@ private bool ViewSuggestions()
}
}
try {
DependencyScreen ds = new DependencyScreen(manager, reinstall, new HashSet<string>());
DependencyScreen ds = new DependencyScreen(manager, reinstall, new HashSet<string>(), debug);
if (ds.HaveOptions()) {
LaunchSubScreen(ds);
bool needRefresh = false;
Expand Down Expand Up @@ -484,7 +484,7 @@ private bool Help()

private bool ApplyChanges()
{
LaunchSubScreen(new InstallScreen(manager, plan));
LaunchSubScreen(new InstallScreen(manager, plan, debug));
RefreshList();
return true;
}
Expand Down
22 changes: 17 additions & 5 deletions ConsoleUI/ProgressScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public override bool RaiseYesNoDialog(string question)
// The installer's questions include embedded newlines for spacing in CmdLine
question.Trim(),
new List<string>() {"Yes", "No"},
null,
TextAlign.Center,
-Console.WindowHeight / 2
);
d.AddBinding(Keys.Y, (object sender) => {
Expand All @@ -71,27 +73,37 @@ public override bool RaiseYesNoDialog(string question)
});

// Scroll messages
d.AddTip("Home / End / Page Up / Page Down", "Scroll messages");
d.AddBinding(Keys.Home, (object sender) => {
d.AddTip("Cursor keys", "Scroll messages");
d.AddBinding(Keys.Home, (object sender) => {
messages.ScrollToTop();
messages.Draw(false);
return true;
});
d.AddBinding(Keys.End, (object sender) => {
d.AddBinding(Keys.End, (object sender) => {
messages.ScrollToBottom();
messages.Draw(false);
return true;
});
d.AddBinding(Keys.PageUp, (object sender) => {
d.AddBinding(Keys.PageUp, (object sender) => {
messages.ScrollUp();
messages.Draw(false);
return true;
});
d.AddBinding(Keys.PageDown, (object sender) => {
d.AddBinding(Keys.PageDown, (object sender) => {
messages.ScrollDown();
messages.Draw(false);
return true;
});
d.AddBinding(Keys.UpArrow, (object sender) => {
messages.ScrollUp(1);
messages.Draw(false);
return true;
});
d.AddBinding(Keys.DownArrow, (object sender) => {
messages.ScrollDown(1);
messages.Draw(false);
return true;
});

bool val = d.Run() == 0;
DrawBackground();
Expand Down
50 changes: 45 additions & 5 deletions ConsoleUI/Toolkit/ConsoleMessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ public class ConsoleMessageDialog : ConsoleDialog {
/// </summary>
/// <param name="m">Message to show</param>
/// <param name="btns">List of captions for buttons</param>
/// <param name="hdr">Function to generate the header</param>
/// <param name="ta">Alignment of the contents</param>
/// <param name="vertOffset">Pass non-zero to move popup vertically</param>
public ConsoleMessageDialog(string m, List<string> btns, int vertOffset = 0)
public ConsoleMessageDialog(string m, List<string> btns, Func<string> hdr = null, TextAlign ta = TextAlign.Center, int vertOffset = 0)
: base()
{
int l = GetLeft(),
r = GetRight();
int w = Console.WindowWidth / 2;
int maxLen = Formatting.MaxLineLength(m);
int w = Math.Min(maxLen + 6, Console.WindowWidth - 4);
int l = (Console.WindowWidth - w) / 2;
int r = -l;
if (hdr != null) {
CenterHeader = hdr;
}

int btnW = btns.Count * buttonWidth + (btns.Count - 1) * buttonPadding;
if (w < btnW + 4) {
// Widen the window to fit the buttons
Expand All @@ -32,6 +39,9 @@ public ConsoleMessageDialog(string m, List<string> btns, int vertOffset = 0)

List<string> messageLines = Formatting.WordWrap(m, w - 4);
int h = 2 + messageLines.Count + (btns.Count > 0 ? 2 : 0) + 2;
if (h > Console.WindowHeight - 4) {
h = Console.WindowHeight - 4;
}

// Calculate vertical position including offset
int t, b;
Expand All @@ -55,13 +65,43 @@ public ConsoleMessageDialog(string m, List<string> btns, int vertOffset = 0)
ConsoleTextBox tb = new ConsoleTextBox(
GetLeft() + 2, GetTop() + 2, GetRight() - 2, GetBottom() - 2 - (btns.Count > 0 ? 2 : 0),
false,
TextAlign.Center,
ta,
() => ConsoleTheme.Current.PopupBg,
() => ConsoleTheme.Current.PopupFg
);
AddObject(tb);
tb.AddLine(m);

int boxH = GetBottom() - 2 - (btns.Count > 0 ? 2 : 0) - (GetTop() + 2) + 1;

if (messageLines.Count > boxH) {
// Scroll
AddBinding(Keys.Home, (object sender) => {
tb.ScrollToTop();
return true;
});
AddBinding(Keys.End, (object sender) => {
tb.ScrollToBottom();
return true;
});
AddBinding(Keys.PageUp, (object sender) => {
tb.ScrollUp();
return true;
});
AddBinding(Keys.PageDown, (object sender) => {
tb.ScrollDown();
return true;
});
AddBinding(Keys.UpArrow, (object sender) => {
tb.ScrollUp(1);
return true;
});
AddBinding(Keys.DownArrow, (object sender) => {
tb.ScrollDown(1);
return true;
});
}

int btnLeft = (Console.WindowWidth - btnW) / 2;
for (int i = 0; i < btns.Count; ++i) {
string cap = btns[i];
Expand Down
14 changes: 7 additions & 7 deletions ConsoleUI/Toolkit/ConsoleTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ public void ScrollToBottom()
/// <summary>
/// Scroll the text box up one page
/// </summary>
public void ScrollUp()
public void ScrollUp(int? howFar = null)
{
int h = GetBottom() - GetTop() + 1;
topLine -= h;
topLine -= howFar ?? (GetBottom() - GetTop() + 1);
if (topLine < 0) {
topLine = 0;
}
Expand All @@ -79,11 +78,12 @@ public void ScrollUp()
/// <summary>
/// Scroll the text box down one page
/// </summary>
public void ScrollDown()
public void ScrollDown(int? howFar = null)
{
int h = GetBottom() - GetTop() + 1;
if (topLine + h <= lines.Count - h) {
topLine += h;
int h = GetBottom() - GetTop() + 1;
int diff = howFar ?? h;
if (topLine + diff <= lines.Count - h) {
topLine += diff;
} else {
ScrollToBottom();
}
Expand Down
23 changes: 22 additions & 1 deletion ConsoleUI/Toolkit/Formatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ public static int ConvertCoord(int val, int max)
}
}

/// <summary>
/// Calculate the longest line length in a string when split on newlines
/// </summary>
/// <param name="msg">String to analyze</param>
/// <returns>
/// Length of longest line
/// </returns>
public static int MaxLineLength(string msg)
{
int len = 0;
string[] hardLines = msg.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None);
foreach (string line in hardLines) {
if (len < line.Length) {
len = line.Length;
}
}
return len;
}

/// <summary>
/// Word wrap a long string into separate lines
/// </summary>
Expand All @@ -42,9 +61,11 @@ public static List<string> WordWrap(string msg, int w)
if (!string.IsNullOrEmpty(msg)) {
// The string is allowed to contain line breaks.
string[] hardLines = msg.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None);
foreach (var line in hardLines) {
foreach (string line in hardLines) {
if (string.IsNullOrEmpty(line)) {
messageLines.Add("");
} else if (line.Length <= w) {
messageLines.Add(line);
} else {
int used = 0;
while (used < line.Length) {
Expand Down
6 changes: 5 additions & 1 deletion ConsoleUI/Toolkit/ScreenContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ protected void AddObject(ScreenObject so)
/// <param name="a">Action to bind to the key</param>
public void AddBinding(ConsoleKeyInfo k, KeyAction a)
{
bindings.Add(k, a);
if (bindings.ContainsKey(k)) {
bindings[k] = a;
} else {
bindings.Add(k, a);
}
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions Core/Registry/AvailableModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -162,6 +164,26 @@ public List<CkanModule> AllAvailable()
return new List<CkanModule>(module_version.Values.Reverse());
}

/// <summary>
/// Return the entire section of registry.json for this mod
/// </summary>
/// <returns>
/// Nicely formatted JSON string containing metadata for all of this mod's available versions
/// </returns>
public string FullMetadata()
{
StringWriter sw = new StringWriter(new StringBuilder());
using (JsonTextWriter writer = new JsonTextWriter(sw) {
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
})
{
new JsonSerializer().Serialize(writer, this);
}
return sw.ToString();
}

}

}
Loading

0 comments on commit d0b5afb

Please sign in to comment.