@@ -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 ;
0 commit comments