1
1
import * as events from "events" ;
2
2
import { Char , attributesFlyweight , defaultAttributes } from "./Char" ;
3
- import { Cursor } from "./Cursor" ;
4
3
import * as i from "./Interfaces" ;
5
4
import * as e from "./Enums" ;
6
5
import { List } from "immutable" ;
@@ -13,7 +12,9 @@ interface SavedState {
13
12
14
13
export class ScreenBuffer extends events . EventEmitter {
15
14
public static hugeOutputThreshold = 300 ;
16
- public cursor : Cursor = new Cursor ( ) ;
15
+ public cursorPosition : RowColumn = { row : 0 , column : 0 } ;
16
+ public _showCursor = true ;
17
+ public _blinkCursor = true ;
17
18
public activeScreenBufferType = e . ScreenBufferType . Standard ;
18
19
private storage = List < List < Char > > ( ) ;
19
20
private _attributes : i . Attributes = { ...defaultAttributes , color : e . Color . White , weight : e . Weight . Normal } ;
@@ -43,10 +44,10 @@ export class ScreenBuffer extends events.EventEmitter {
43
44
this . moveCursorRelative ( { horizontal : - 1 } ) ;
44
45
break ;
45
46
case e . KeyCode . Tab :
46
- this . moveCursorAbsolute ( { column : Math . floor ( ( this . cursor . column + 8 ) / 8 ) * 8 } ) ;
47
+ this . moveCursorAbsolute ( { column : Math . floor ( ( this . cursorPosition . column + 8 ) / 8 ) * 8 } ) ;
47
48
break ;
48
49
case e . KeyCode . NewLine :
49
- if ( this . cursor . row === this . _margins . bottom ) {
50
+ if ( this . cursorPosition . row === this . _margins . bottom ) {
50
51
this . scrollUp ( 1 ) ;
51
52
} else {
52
53
this . moveCursorRelative ( { vertical : 1 } ) ;
@@ -68,7 +69,7 @@ export class ScreenBuffer extends events.EventEmitter {
68
69
69
70
scrollDown ( count : number ) {
70
71
this . storage = this . storage . splice ( ( this . _margins . bottom || 0 ) - count + 1 , count ) . toList ( ) ;
71
- times ( count , ( ) => this . storage = this . storage . splice ( this . cursor . row , 0 , undefined ) . toList ( ) ) ;
72
+ times ( count , ( ) => this . storage = this . storage . splice ( this . cursorPosition . row , 0 , undefined ) . toList ( ) ) ;
72
73
}
73
74
74
75
scrollUp ( count : number , deletedLine = this . _margins . top ) {
@@ -91,7 +92,7 @@ export class ScreenBuffer extends events.EventEmitter {
91
92
toRenderable ( status : e . Status , fromStorage = this . storage ) : List < List < Char > > {
92
93
let storage = fromStorage ;
93
94
94
- if ( status === e . Status . InProgress && ( this . cursor . show || this . cursor . blink ) ) {
95
+ if ( status === e . Status . InProgress && ( this . _showCursor || this . _blinkCursor ) ) {
95
96
const cursorRow = this . cursorPosition . row - ( this . storage . size - fromStorage . size ) ;
96
97
const cursorColumn = this . cursorPosition . column ;
97
98
@@ -141,28 +142,39 @@ export class ScreenBuffer extends events.EventEmitter {
141
142
}
142
143
143
144
showCursor ( state : boolean ) : void {
144
- this . ensureRowExists ( this . cursor . row ) ;
145
- this . cursor . show = state ;
145
+ this . ensureRowExists ( this . cursorPosition . row ) ;
146
+ this . _showCursor = state ;
146
147
this . emitData ( ) ;
147
148
}
148
149
149
150
blinkCursor ( state : boolean ) : void {
150
- this . ensureRowExists ( this . cursor . row ) ;
151
- this . cursor . blink = state ;
151
+ this . ensureRowExists ( this . cursorPosition . row ) ;
152
+ this . _blinkCursor = state ;
152
153
this . emitData ( ) ;
153
154
}
154
155
155
- moveCursorRelative ( position : Advancement ) : this {
156
- this . cursor . moveRelative ( position ) ;
157
- this . ensureRowExists ( this . cursor . row ) ;
156
+ moveCursorRelative ( advancement : Advancement ) : this {
157
+ const row = Math . max ( 0 , this . cursorPosition . row + ( advancement . vertical || 0 ) ) ;
158
+ const column = Math . max ( 0 , this . cursorPosition . column + ( advancement . horizontal || 0 ) ) ;
159
+
160
+ this . moveCursorAbsolute ( { row : row , column : column } ) ;
161
+
162
+ this . ensureRowExists ( this . cursorPosition . row ) ;
158
163
this . emitData ( ) ;
159
164
160
165
return this ;
161
166
}
162
167
163
168
moveCursorAbsolute ( position : Partial < RowColumn > ) : this {
164
- this . cursor . moveAbsolute ( position , this . homePosition ) ;
165
- this . ensureRowExists ( this . cursor . row ) ;
169
+ if ( typeof position . column === "number" ) {
170
+ this . cursorPosition . column = Math . max ( position . column , 0 ) + this . homePosition . column ;
171
+ }
172
+
173
+ if ( typeof position . row === "number" ) {
174
+ this . cursorPosition . row = Math . max ( position . row , 0 ) + this . homePosition . row ;
175
+ }
176
+
177
+ this . ensureRowExists ( this . cursorPosition . row ) ;
166
178
this . emitData ( ) ;
167
179
168
180
return this ;
@@ -254,10 +266,6 @@ export class ScreenBuffer extends events.EventEmitter {
254
266
return this . storage . size ;
255
267
}
256
268
257
- get cursorPosition ( ) : RowColumn {
258
- return this . cursor . getPosition ( ) ;
259
- }
260
-
261
269
isEmpty ( ) : boolean {
262
270
return this . storage . size === 0 ;
263
271
}
0 commit comments