mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add support for exclusive mode
This commit is contained in:

committed by
Phil Scott

parent
c2bab0ebf8
commit
7f6f2437b1
@ -118,38 +118,41 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(action));
|
||||
}
|
||||
|
||||
var renderer = CreateRenderer();
|
||||
renderer.Started();
|
||||
|
||||
T result;
|
||||
|
||||
try
|
||||
return await _console.RunExclusive(async () =>
|
||||
{
|
||||
using (new RenderHookScope(_console, renderer))
|
||||
{
|
||||
var context = new ProgressContext(_console, renderer);
|
||||
var renderer = CreateRenderer();
|
||||
renderer.Started();
|
||||
|
||||
if (AutoRefresh)
|
||||
T result;
|
||||
|
||||
try
|
||||
{
|
||||
using (new RenderHookScope(_console, renderer))
|
||||
{
|
||||
using (var thread = new ProgressRefreshThread(context, renderer.RefreshRate))
|
||||
var context = new ProgressContext(_console, renderer);
|
||||
|
||||
if (AutoRefresh)
|
||||
{
|
||||
using (var thread = new ProgressRefreshThread(context, renderer.RefreshRate))
|
||||
{
|
||||
result = await action(context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await action(context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await action(context).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
context.Refresh();
|
||||
context.Refresh();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
renderer.Completed(AutoClear);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
renderer.Completed(AutoClear);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private ProgressRenderer CreateRenderer()
|
||||
|
@ -72,6 +72,11 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
public List<T> Show(IAnsiConsole console)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
if (!console.Profile.Capabilities.Interactive)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
@ -86,50 +91,53 @@ namespace Spectre.Console
|
||||
"terminal does not support ANSI escape sequences.");
|
||||
}
|
||||
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var list = new RenderableMultiSelectionList<T>(
|
||||
console, Title, PageSize, Choices,
|
||||
Selected, converter, HighlightStyle,
|
||||
MoreChoicesText, InstructionsText);
|
||||
|
||||
using (new RenderHookScope(console, list))
|
||||
return console.RunExclusive(() =>
|
||||
{
|
||||
console.Cursor.Hide();
|
||||
list.Redraw();
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var list = new RenderableMultiSelectionList<T>(
|
||||
console, Title, PageSize, Choices,
|
||||
Selected, converter, HighlightStyle,
|
||||
MoreChoicesText, InstructionsText);
|
||||
|
||||
while (true)
|
||||
using (new RenderHookScope(console, list))
|
||||
{
|
||||
var key = console.Input.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
console.Cursor.Hide();
|
||||
list.Redraw();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (Required && list.Selections.Count == 0)
|
||||
var key = console.Input.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Enter)
|
||||
{
|
||||
if (Required && list.Selections.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
list.Select();
|
||||
list.Redraw();
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
list.Select();
|
||||
list.Redraw();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (list.Update(key.Key))
|
||||
{
|
||||
list.Redraw();
|
||||
if (list.Update(key.Key))
|
||||
{
|
||||
list.Redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
console.Cursor.Show();
|
||||
list.Clear();
|
||||
console.Cursor.Show();
|
||||
|
||||
return list.Selections
|
||||
.Select(index => Choices[index])
|
||||
.ToList();
|
||||
return list.Selections
|
||||
.Select(index => Choices[index])
|
||||
.ToList();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -68,35 +68,38 @@ namespace Spectre.Console
|
||||
"terminal does not support ANSI escape sequences.");
|
||||
}
|
||||
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var list = new RenderableSelectionList<T>(
|
||||
console, Title, PageSize, Choices,
|
||||
converter, HighlightStyle, MoreChoicesText);
|
||||
|
||||
using (new RenderHookScope(console, list))
|
||||
return console.RunExclusive(() =>
|
||||
{
|
||||
console.Cursor.Hide();
|
||||
list.Redraw();
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var list = new RenderableSelectionList<T>(
|
||||
console, Title, PageSize, Choices,
|
||||
converter, HighlightStyle, MoreChoicesText);
|
||||
|
||||
while (true)
|
||||
using (new RenderHookScope(console, list))
|
||||
{
|
||||
var key = console.Input.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
break;
|
||||
}
|
||||
console.Cursor.Hide();
|
||||
list.Redraw();
|
||||
|
||||
if (list.Update(key.Key))
|
||||
while (true)
|
||||
{
|
||||
list.Redraw();
|
||||
var key = console.Input.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (list.Update(key.Key))
|
||||
{
|
||||
list.Redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
console.Cursor.Show();
|
||||
list.Clear();
|
||||
console.Cursor.Show();
|
||||
|
||||
return Choices[list.Index];
|
||||
return Choices[list.Index];
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -100,66 +100,69 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
var promptStyle = PromptStyle ?? Style.Plain;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var choices = Choices.Select(choice => converter(choice)).ToList();
|
||||
var choiceMap = Choices.ToDictionary(choice => converter(choice), choice => choice, _comparer);
|
||||
|
||||
WritePrompt(console);
|
||||
|
||||
while (true)
|
||||
return console.RunExclusive(() =>
|
||||
{
|
||||
var input = console.ReadLine(promptStyle, IsSecret, choices);
|
||||
var promptStyle = PromptStyle ?? Style.Plain;
|
||||
var converter = Converter ?? TypeConverterHelper.ConvertToString;
|
||||
var choices = Choices.Select(choice => converter(choice)).ToList();
|
||||
var choiceMap = Choices.ToDictionary(choice => converter(choice), choice => choice, _comparer);
|
||||
|
||||
// Nothing entered?
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
WritePrompt(console);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (DefaultValue != null)
|
||||
var input = console.ReadLine(promptStyle, IsSecret, choices);
|
||||
|
||||
// Nothing entered?
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
|
||||
console.WriteLine();
|
||||
return DefaultValue.Value;
|
||||
if (DefaultValue != null)
|
||||
{
|
||||
console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
|
||||
console.WriteLine();
|
||||
return DefaultValue.Value;
|
||||
}
|
||||
|
||||
if (!AllowEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!AllowEmpty)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
console.WriteLine();
|
||||
|
||||
console.WriteLine();
|
||||
|
||||
T? result;
|
||||
if (Choices.Count > 0)
|
||||
{
|
||||
if (choiceMap.TryGetValue(input, out result) && result != null)
|
||||
T? result;
|
||||
if (Choices.Count > 0)
|
||||
{
|
||||
return result;
|
||||
if (choiceMap.TryGetValue(input, out result) && result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.MarkupLine(InvalidChoiceMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result) || result == null)
|
||||
{
|
||||
console.MarkupLine(InvalidChoiceMessage);
|
||||
console.MarkupLine(ValidationErrorMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result) || result == null)
|
||||
{
|
||||
console.MarkupLine(ValidationErrorMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Run all validators
|
||||
if (!ValidateResult(result, out var validationMessage))
|
||||
{
|
||||
console.MarkupLine(validationMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
// Run all validators
|
||||
if (!ValidateResult(result, out var validationMessage))
|
||||
{
|
||||
console.MarkupLine(validationMessage);
|
||||
WritePrompt(console);
|
||||
continue;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user