Skip to content

Commit 9247839

Browse files
committed
Closes #3: Choose option automatically without pressing enter.
1 parent 5c71c24 commit 9247839

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

xmr-stak-bootstrap/Common/Menu/MenuBuilder.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using XmrStakBootstrap.Common.Helper;
45

56
namespace XmrStakBootstrap.Common.Menu
@@ -47,15 +48,23 @@ public void AddOption(string text, bool enabled, Func<T> action)
4748

4849
public T Execute()
4950
{
50-
if (_options.Count == 0) return ExecuteDefault();
51+
switch (_options.Count)
52+
{
53+
case 0:
54+
return ExecuteDefault();
55+
56+
case 1:
57+
return ExecuteAction(_options.First().Action);
5158

52-
RenderOptions();
53-
var input = GetInput();
59+
default:
60+
RenderOptions();
61+
var input = GetInput();
5462

55-
if (!IsInputValid(input)) return ExecuteDefault();
63+
if (!IsInputValid(input)) return ExecuteDefault();
5664

57-
var option = _options[input - 1];
58-
return option.IsEnabled ? ExecuteAction(option.Action) : ExecuteDefault();
65+
var option = _options[input - 1];
66+
return option.IsEnabled ? ExecuteAction(option.Action) : ExecuteDefault();
67+
}
5968
}
6069

6170
private T ExecuteDefault()
@@ -73,12 +82,38 @@ private bool IsInputValid(int input)
7382
return input >= 1 && input <= _options.Count;
7483
}
7584

76-
private static int GetInput()
85+
private int GetInput()
7786
{
7887
Console.Write(@"Select: ");
88+
89+
var knownOptions = _options.Select((_, i) => (i + 1).ToString()).ToList();
90+
91+
ConsoleKeyInfo key;
92+
var input = "";
7993
int index;
80-
if (!int.TryParse(Console.ReadLine(), out index)) return -1;
81-
return index;
94+
do
95+
{
96+
key = Console.ReadKey(true);
97+
if (key.Key == ConsoleKey.Backspace && input.Length > 0)
98+
{
99+
input = input.Substring(0, input.Length - 1);
100+
// ReSharper disable once LocalizableElement
101+
Console.Write("\b \b");
102+
}
103+
else if (char.IsNumber(key.KeyChar))
104+
{
105+
input += key.KeyChar;
106+
Console.Write(key.KeyChar);
107+
}
108+
109+
if (int.TryParse(input, out index) && IsInputValid(index))
110+
{
111+
var stringInput = index.ToString();
112+
if (knownOptions.Count(x => x.StartsWith(stringInput)) == 1) return index;
113+
}
114+
}
115+
while (key.Key != ConsoleKey.Enter);
116+
return int.TryParse(input, out index) ? index : -1;
82117
}
83118

84119
private void RenderOptions()

xmr-stak-bootstrap/Core/Job/Miner/MinerRunnerMenuJob.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using Unity.Attributes;
4+
using XmrStakBootstrap.Common.Helper;
45
using XmrStakBootstrap.Common.Menu;
56
using XmrStakBootstrap.MasterConfiguration.Model;
67
using XmrStakBootstrap.RunConfiguration.Model;
@@ -44,8 +45,10 @@ public void Execute()
4445
var @continue = true;
4546
while (@continue)
4647
{
47-
Console.WriteLine(@"Active solution: {0}", HasSolution ? RunConfigurationModel.ActiveSolutionConfiguration : "<UNKNOWN>");
48-
Console.WriteLine(@"Active workload: {0}", HasWorkload ? RunConfigurationModel.ActiveWorkloadConfiguration : "<UNKNOWN>");
48+
Console.Write(@"Active solution: ");
49+
ColorConsole(HasSolution ? ConsoleColor.White : ConsoleColor.DarkGray, () => Console.WriteLine(HasSolution ? RunConfigurationModel.ActiveSolutionConfiguration : "<UNKNOWN>"));
50+
Console.Write(@"Active workload: ");
51+
ColorConsole(HasWorkload ? ConsoleColor.White : ConsoleColor.DarkGray, () => Console.WriteLine(HasWorkload ? RunConfigurationModel.ActiveWorkloadConfiguration : "<UNKNOWN>"));
4952
Console.WriteLine();
5053
Console.WriteLine(@"What would you like to do?");
5154

@@ -94,6 +97,11 @@ public void Execute()
9497
}
9598
}
9699

100+
private static void ColorConsole(ConsoleColor color, Action action)
101+
{
102+
using (ConsoleColorClosure.ForegroundColor(color)) action();
103+
}
104+
97105
private void SelectSolution()
98106
{
99107
Console.WriteLine();

0 commit comments

Comments
 (0)