Skip to content

Commit 52fbdca

Browse files
authored
Fix console errors (#457)
* Fix console errors * Tweaking test code
1 parent 2da9c53 commit 52fbdca

File tree

16 files changed

+441
-401
lines changed

16 files changed

+441
-401
lines changed

src/Microsoft.Repl/Commanding/DefaultCommandDispatcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public IReadOnlyList<string> CollectSuggestions(IShellState shellState)
8484
shellState = shellState ?? throw new ArgumentNullException(nameof(shellState));
8585

8686
string line = shellState.InputManager.GetCurrentBuffer();
87-
TParseResult parseResult = _parser.Parse(line, shellState.ConsoleManager.CaretPosition);
87+
TParseResult parseResult = _parser.Parse(line, shellState.InputManager.CaretPosition);
8888
HashSet<string> suggestions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
8989

9090
foreach (ICommand<TProgramState, TParseResult> command in _commands)
@@ -139,7 +139,7 @@ public async Task ExecuteCommandAsync(IShellState shellState, CancellationToken
139139
private async Task ExecuteCommandInternalAsync(IShellState shellState, CancellationToken cancellationToken)
140140
{
141141
string line = shellState.InputManager.GetCurrentBuffer();
142-
TParseResult parseResult = _parser.Parse(line, shellState.ConsoleManager.CaretPosition);
142+
TParseResult parseResult = _parser.Parse(line, shellState.InputManager.CaretPosition);
143143

144144
if (!string.IsNullOrWhiteSpace(parseResult.CommandText))
145145
{
@@ -171,7 +171,7 @@ public void OnReady(IShellState shellState)
171171
if (!_isReady && !shellState.IsExiting)
172172
{
173173
_onReady(shellState);
174-
shellState.ConsoleManager.ResetCommandStart();
174+
shellState.InputManager.ResetInput();
175175
_isReady = true;
176176
}
177177
}

src/Microsoft.Repl/ConsoleHandling/ConsoleManager.cs

Lines changed: 56 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ public class ConsoleManager : IConsoleManager
1414

1515
public Point Caret => new Point(Console.CursorLeft, Console.CursorTop);
1616

17-
public Point CommandStart
18-
{
19-
get
20-
{
21-
Point c = Caret;
22-
return new Point(c.X - CaretPosition % Console.BufferWidth, c.Y - CaretPosition / Console.BufferWidth);
23-
}
24-
}
25-
26-
public int CaretPosition { get; private set; }
27-
2817
public bool IsKeyAvailable => Console.KeyAvailable;
2918

3019
public bool IsCaretVisible
@@ -35,95 +24,88 @@ public bool IsCaretVisible
3524

3625
public ConsoleManager()
3726
{
38-
Error = new Writable(CaretUpdateScope, Reporter.Error);
27+
Error = new Writable(Reporter.Error);
3928
Console.CancelKeyPress += OnCancelKeyPress;
4029
}
4130

4231
public void Clear()
4332
{
44-
using (CaretUpdateScope())
45-
{
46-
Console.Clear();
47-
ResetCommandStart();
48-
}
33+
Console.Clear();
4934
}
5035

5136
public void MoveCaret(int positions)
5237
{
53-
using (CaretUpdateScope())
38+
if (positions == 0)
5439
{
55-
if (positions == 0)
56-
{
57-
return;
58-
}
40+
return;
41+
}
5942

60-
int bufferWidth = Console.BufferWidth;
61-
int cursorTop = Console.CursorTop;
62-
int cursorLeft = Console.CursorLeft;
43+
int bufferWidth = Console.BufferWidth;
44+
int cursorTop = Console.CursorTop;
45+
int cursorLeft = Console.CursorLeft;
6346

64-
while (positions < 0 && CaretPosition > 0)
47+
while (positions < 0)
48+
{
49+
if (-positions > bufferWidth)
6550
{
66-
if (-positions > bufferWidth)
51+
if (cursorTop == 0)
6752
{
68-
if (cursorTop == 0)
69-
{
70-
cursorLeft = 0;
71-
positions = 0;
72-
}
73-
else
74-
{
75-
positions += bufferWidth;
76-
--cursorTop;
77-
}
53+
cursorLeft = 0;
54+
positions = 0;
7855
}
7956
else
8057
{
81-
int remaining = cursorLeft + positions;
82-
83-
if (remaining >= 0)
84-
{
85-
cursorLeft = remaining;
86-
}
87-
else if (cursorTop == 0)
88-
{
89-
cursorLeft = 0;
90-
}
91-
else
92-
{
93-
--cursorTop;
94-
cursorLeft = bufferWidth + remaining;
95-
}
58+
positions += bufferWidth;
59+
--cursorTop;
60+
}
61+
}
62+
else
63+
{
64+
int remaining = cursorLeft + positions;
9665

97-
positions = 0;
66+
if (remaining >= 0)
67+
{
68+
cursorLeft = remaining;
9869
}
70+
else if (cursorTop == 0)
71+
{
72+
cursorLeft = 0;
73+
}
74+
else
75+
{
76+
--cursorTop;
77+
cursorLeft = bufferWidth + remaining;
78+
}
79+
80+
positions = 0;
9981
}
82+
}
10083

101-
while (positions > 0)
84+
while (positions > 0)
85+
{
86+
if (positions > bufferWidth)
87+
{
88+
positions -= bufferWidth;
89+
++cursorTop;
90+
}
91+
else
10292
{
103-
if (positions > bufferWidth)
93+
int spaceLeftOnLine = bufferWidth - cursorLeft - 1;
94+
if (positions > spaceLeftOnLine)
10495
{
105-
positions -= bufferWidth;
10696
++cursorTop;
97+
cursorLeft = positions - spaceLeftOnLine - 1;
10798
}
10899
else
109100
{
110-
int spaceLeftOnLine = bufferWidth - cursorLeft - 1;
111-
if (positions > spaceLeftOnLine)
112-
{
113-
++cursorTop;
114-
cursorLeft = positions - spaceLeftOnLine - 1;
115-
}
116-
else
117-
{
118-
cursorLeft += positions;
119-
}
120-
121-
positions = 0;
101+
cursorLeft += positions;
122102
}
123-
}
124103

125-
Console.SetCursorPosition(cursorLeft, cursorTop);
104+
positions = 0;
105+
}
126106
}
107+
108+
Console.SetCursorPosition(cursorLeft, cursorTop);
127109
}
128110

129111
public ConsoleKeyInfo ReadKey(CancellationToken cancellationToken)
@@ -143,33 +125,19 @@ public ConsoleKeyInfo ReadKey(CancellationToken cancellationToken)
143125
}
144126
}
145127

146-
public void ResetCommandStart()
147-
{
148-
CaretPosition = 0;
149-
}
150-
151128
public void Write(char c)
152129
{
153-
using (CaretUpdateScope())
154-
{
155-
Reporter.Output.Write(c);
156-
}
130+
Reporter.Output.Write(c);
157131
}
158132

159133
public void Write(string s)
160134
{
161-
using (CaretUpdateScope())
162-
{
163-
Reporter.Output.Write(s);
164-
}
135+
Reporter.Output.Write(s);
165136
}
166137

167138
public void WriteLine()
168139
{
169-
using (CaretUpdateScope())
170-
{
171-
Reporter.Output.WriteLine();
172-
}
140+
Reporter.Output.WriteLine();
173141
}
174142

175143
public void WriteLine(string s)
@@ -179,10 +147,7 @@ public void WriteLine(string s)
179147
return;
180148
}
181149

182-
using (CaretUpdateScope())
183-
{
184-
Reporter.Output.WriteLine(s);
185-
}
150+
Reporter.Output.WriteLine(s);
186151
}
187152

188153
public IDisposable AddBreakHandler(Action onBreak)
@@ -192,18 +157,6 @@ public IDisposable AddBreakHandler(Action onBreak)
192157
return result;
193158
}
194159

195-
private IDisposable CaretUpdateScope()
196-
{
197-
Point currentCaret = Caret;
198-
return new Disposable(() =>
199-
{
200-
Point c = Caret;
201-
int y = c.Y - currentCaret.Y;
202-
int x = c.X - currentCaret.X;
203-
CaretPosition += y * Console.BufferWidth + x;
204-
});
205-
}
206-
207160
private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
208161
{
209162
e.Cancel = true;

src/Microsoft.Repl/ConsoleHandling/IConsoleManager.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ public interface IConsoleManager : IWritable
1010
{
1111
Point Caret { get; }
1212

13-
Point CommandStart { get; }
14-
15-
int CaretPosition { get; }
16-
1713
#pragma warning disable CA1716 // Identifiers should not match keywords
1814
IWritable Error { get; }
1915
#pragma warning restore CA1716 // Identifiers should not match keywords
@@ -26,8 +22,6 @@ public interface IConsoleManager : IWritable
2622

2723
ConsoleKeyInfo ReadKey(CancellationToken cancellationToken);
2824

29-
void ResetCommandStart();
30-
3125
IDisposable AddBreakHandler(Action onBreak);
3226

3327
bool AllowOutputRedirection { get; }
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
5-
64
namespace Microsoft.Repl.ConsoleHandling
75
{
86
internal class Writable : IWritable
97
{
10-
private readonly Func<IDisposable> _caretUpdater;
118
private readonly Reporter _reporter;
129

13-
public Writable(Func<IDisposable> caretUpdater, Reporter reporter)
10+
public Writable(Reporter reporter)
1411
{
15-
_caretUpdater = caretUpdater;
1612
_reporter = reporter;
1713
}
1814

@@ -24,34 +20,22 @@ public bool IsCaretVisible
2420

2521
public void Write(char c)
2622
{
27-
using (_caretUpdater())
28-
{
29-
_reporter.Write(c);
30-
}
23+
_reporter.Write(c);
3124
}
3225

3326
public void Write(string s)
3427
{
35-
using (_caretUpdater())
36-
{
37-
_reporter.Write(s);
38-
}
28+
_reporter.Write(s);
3929
}
4030

4131
public void WriteLine()
4232
{
43-
using (_caretUpdater())
44-
{
45-
_reporter.WriteLine();
46-
}
33+
_reporter.WriteLine();
4734
}
4835

4936
public void WriteLine(string s)
5037
{
51-
using (_caretUpdater())
52-
{
53-
_reporter.WriteLine(s);
54-
}
38+
_reporter.WriteLine(s);
5539
}
5640
}
5741
}

src/Microsoft.Repl/IShellState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public interface IShellState
2121
ISuggestionManager SuggestionManager { get; }
2222

2323
bool IsExiting { get; set; }
24+
25+
void MoveCarets(int positions);
2426
}
2527
}

src/Microsoft.Repl/Input/IInputManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface IInputManager
1111
{
1212
bool IsOverwriteMode { get; set; }
1313

14+
int CaretPosition { get; }
15+
1416
IInputManager RegisterKeyHandler(ConsoleKey key, AsyncKeyPressHandler handler);
1517

1618
IInputManager RegisterKeyHandler(ConsoleKey key, ConsoleModifiers modifiers, AsyncKeyPressHandler handler);
@@ -28,5 +30,7 @@ public interface IInputManager
2830
void RemoveCurrentCharacter(IShellState state);
2931

3032
void Clear(IShellState state);
33+
34+
void MoveCaret(int positions);
3135
}
3236
}

0 commit comments

Comments
 (0)