Skip to content

Commit fc921c8

Browse files
committed
TrimSlider: turns out direct Console usage was causing flickering
Realized the constant `Console.Clear()` was the reason for all the flickering - switched to buffered AnsiConsole which only updates when needed. Also fixes the cursor randomly showing up during slider updates It should be way smoother now
1 parent 15fe173 commit fc921c8

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

Features/TrimSlider/TrimmingSlider.cs

+35-22
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ public string ShowSlider()
1717
{
1818
RenderInterface();
1919

20-
var key = Console.ReadKey(true);
20+
var key = AnsiConsole.Console.Input.ReadKey(true);
21+
if (key == null) continue;
2122

2223
if (_state.IsTypingNumber)
2324
{
24-
HandleNumberInput(key);
25+
HandleNumberInput(key.Value);
2526
continue;
2627
}
2728

28-
if (HandleNavigationKey(key))
29+
if (HandleNavigationKey(key.Value))
2930
{
3031
return FormatResult();
3132
}
@@ -36,22 +37,26 @@ public string ShowSlider()
3637

3738
private void RenderInterface()
3839
{
40+
HideCursor();
41+
StringWriter output = new();
42+
var console = AnsiConsole.Create(new AnsiConsoleSettings
43+
{
44+
Out = new AnsiConsoleOutput(output)
45+
});
46+
47+
console.Clear();
48+
DrawSlider(console);
49+
DrawInstructions(console);
50+
3951
AnsiConsole.Clear();
40-
DrawSlider();
41-
DrawInstructions();
52+
AnsiConsole.Write(output.ToString());
53+
ShowCursor();
4254
}
4355

44-
private void DrawInstructions()
45-
{
46-
if (_state.IsTypingNumber)
47-
{
48-
AnsiConsole.Write(DisplayStrings.GetTimeInput(_state.NumberBuffer));
49-
}
50-
else
51-
{
52-
AnsiConsole.Write(DisplayStrings.Controls);
53-
}
54-
}
56+
private void DrawInstructions(IAnsiConsole console)
57+
=> console.Write(_state.IsTypingNumber
58+
? DisplayStrings.GetTimeInput(_state.NumberBuffer)
59+
: DisplayStrings.Controls);
5560

5661
private void HandleNumberInput(ConsoleKeyInfo key)
5762
{
@@ -93,7 +98,9 @@ private bool HandleNavigationKey(ConsoleKeyInfo key)
9398
return true;
9499
}
95100

96-
var step = (key.Modifiers & ConsoleModifiers.Shift) != 0 ? Constants.MillisecondStep : Constants.SecondStep;
101+
var step = (key.Modifiers & ConsoleModifiers.Shift) != 0
102+
? Constants.MillisecondStep
103+
: Constants.SecondStep;
97104

98105
return key.Key switch
99106
{
@@ -111,15 +118,15 @@ private bool HandleNavigationKey(ConsoleKeyInfo key)
111118

112119
private static bool SetResult(bool result) => result;
113120

114-
private void DrawSlider()
121+
private void DrawSlider(IAnsiConsole console)
115122
{
116123
var slider = CreateSliderVisualization();
117124

118-
AnsiConsole.MarkupLine($"\nVideo duration: [blue]{FormatTime(_duration)}[/]");
119-
AnsiConsole.MarkupLine(
125+
console.MarkupLine($"\nVideo duration: [blue]{FormatTime(_duration)}[/]");
126+
console.MarkupLine(
120127
$"Selected range: [green]{_state.FormatRange()}[/]\n");
121-
AnsiConsole.MarkupLine($"Currently adjusting: [blue]{(_state.IsAdjustingStart ? "Start" : "End")}[/] position\n");
122-
AnsiConsole.MarkupLine($"0s {slider} {_duration.TotalSeconds:F2}s");
128+
console.MarkupLine($"Currently adjusting: [blue]{(_state.IsAdjustingStart ? "Start" : "End")}[/] position\n");
129+
console.MarkupLine($"0s {slider} {_duration.TotalSeconds:F2}s");
123130
}
124131

125132
private string CreateSliderVisualization() =>
@@ -136,4 +143,10 @@ private string FormatResult()
136143
}
137144

138145
private bool IsCancelled() => _state.IsCancelled;
146+
147+
private static void HideCursor()
148+
=> AnsiConsole.Cursor.Hide();
149+
150+
private static void ShowCursor()
151+
=> AnsiConsole.Cursor.Show();
139152
}

0 commit comments

Comments
 (0)