@@ -14,17 +14,6 @@ public class ConsoleManager : IConsoleManager
14
14
15
15
public Point Caret => new Point ( Console . CursorLeft , Console . CursorTop ) ;
16
16
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
-
28
17
public bool IsKeyAvailable => Console . KeyAvailable ;
29
18
30
19
public bool IsCaretVisible
@@ -35,95 +24,88 @@ public bool IsCaretVisible
35
24
36
25
public ConsoleManager ( )
37
26
{
38
- Error = new Writable ( CaretUpdateScope , Reporter . Error ) ;
27
+ Error = new Writable ( Reporter . Error ) ;
39
28
Console . CancelKeyPress += OnCancelKeyPress ;
40
29
}
41
30
42
31
public void Clear ( )
43
32
{
44
- using ( CaretUpdateScope ( ) )
45
- {
46
- Console . Clear ( ) ;
47
- ResetCommandStart ( ) ;
48
- }
33
+ Console . Clear ( ) ;
49
34
}
50
35
51
36
public void MoveCaret ( int positions )
52
37
{
53
- using ( CaretUpdateScope ( ) )
38
+ if ( positions == 0 )
54
39
{
55
- if ( positions == 0 )
56
- {
57
- return ;
58
- }
40
+ return ;
41
+ }
59
42
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 ;
63
46
64
- while ( positions < 0 && CaretPosition > 0 )
47
+ while ( positions < 0 )
48
+ {
49
+ if ( - positions > bufferWidth )
65
50
{
66
- if ( - positions > bufferWidth )
51
+ if ( cursorTop == 0 )
67
52
{
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 ;
78
55
}
79
56
else
80
57
{
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 ;
96
65
97
- positions = 0 ;
66
+ if ( remaining >= 0 )
67
+ {
68
+ cursorLeft = remaining ;
98
69
}
70
+ else if ( cursorTop == 0 )
71
+ {
72
+ cursorLeft = 0 ;
73
+ }
74
+ else
75
+ {
76
+ -- cursorTop ;
77
+ cursorLeft = bufferWidth + remaining ;
78
+ }
79
+
80
+ positions = 0 ;
99
81
}
82
+ }
100
83
101
- while ( positions > 0 )
84
+ while ( positions > 0 )
85
+ {
86
+ if ( positions > bufferWidth )
87
+ {
88
+ positions -= bufferWidth ;
89
+ ++ cursorTop ;
90
+ }
91
+ else
102
92
{
103
- if ( positions > bufferWidth )
93
+ int spaceLeftOnLine = bufferWidth - cursorLeft - 1 ;
94
+ if ( positions > spaceLeftOnLine )
104
95
{
105
- positions -= bufferWidth ;
106
96
++ cursorTop ;
97
+ cursorLeft = positions - spaceLeftOnLine - 1 ;
107
98
}
108
99
else
109
100
{
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 ;
122
102
}
123
- }
124
103
125
- Console . SetCursorPosition ( cursorLeft , cursorTop ) ;
104
+ positions = 0 ;
105
+ }
126
106
}
107
+
108
+ Console . SetCursorPosition ( cursorLeft , cursorTop ) ;
127
109
}
128
110
129
111
public ConsoleKeyInfo ReadKey ( CancellationToken cancellationToken )
@@ -143,33 +125,19 @@ public ConsoleKeyInfo ReadKey(CancellationToken cancellationToken)
143
125
}
144
126
}
145
127
146
- public void ResetCommandStart ( )
147
- {
148
- CaretPosition = 0 ;
149
- }
150
-
151
128
public void Write ( char c )
152
129
{
153
- using ( CaretUpdateScope ( ) )
154
- {
155
- Reporter . Output . Write ( c ) ;
156
- }
130
+ Reporter . Output . Write ( c ) ;
157
131
}
158
132
159
133
public void Write ( string s )
160
134
{
161
- using ( CaretUpdateScope ( ) )
162
- {
163
- Reporter . Output . Write ( s ) ;
164
- }
135
+ Reporter . Output . Write ( s ) ;
165
136
}
166
137
167
138
public void WriteLine ( )
168
139
{
169
- using ( CaretUpdateScope ( ) )
170
- {
171
- Reporter . Output . WriteLine ( ) ;
172
- }
140
+ Reporter . Output . WriteLine ( ) ;
173
141
}
174
142
175
143
public void WriteLine ( string s )
@@ -179,10 +147,7 @@ public void WriteLine(string s)
179
147
return ;
180
148
}
181
149
182
- using ( CaretUpdateScope ( ) )
183
- {
184
- Reporter . Output . WriteLine ( s ) ;
185
- }
150
+ Reporter . Output . WriteLine ( s ) ;
186
151
}
187
152
188
153
public IDisposable AddBreakHandler ( Action onBreak )
@@ -192,18 +157,6 @@ public IDisposable AddBreakHandler(Action onBreak)
192
157
return result ;
193
158
}
194
159
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
-
207
160
private void OnCancelKeyPress ( object sender , ConsoleCancelEventArgs e )
208
161
{
209
162
e . Cancel = true ;
0 commit comments