Skip to content

Commit

Permalink
TextBuffer fixes, and .removeTrailingSpaces()
Browse files Browse the repository at this point in the history
  • Loading branch information
cronvel committed Sep 12, 2022
1 parent 9fa2a26 commit bea6010
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 80 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

v2.11.4
-------

TextBuffer fixes, and .removeTrailingSpaces()


v2.11.3
-------

Expand Down
97 changes: 69 additions & 28 deletions browser/termkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6302,6 +6302,34 @@ TextBuffer.prototype.getLineText = function( y = this.cy ) {



// TODOC
// Get the indentation part of the line, return null if the line is empty (no char or no non-space char)
TextBuffer.prototype.getLineIndent = function( y = this.cy ) {
if ( ! this.buffer[ y ] ) { return null ; }

var x , xmin , xmax , cell ,
indent = '' ;

for ( x = 0 , xmax = this.buffer[ y ].length - 1 ; x <= xmax ; x ++ ) {
cell = this.buffer[ y ][ x ] ;
if ( ! cell.filler ) {
if ( cell.char === '\t' || cell.char === ' ' ) {
indent += cell.char ;
}
else if ( cell.char === '\n' ) {
return null ;
}
else {
return indent ;
}
}
}

return null ;
} ;



// TODOC
// Count characters in this line, excluding fillers
TextBuffer.prototype.getLineCharCount = function( y = this.cy ) {
Expand All @@ -6325,6 +6353,47 @@ TextBuffer.prototype.getCellsCharCount = function( cells ) {



// TODOC
// Remove spaces and tabs at the end of the line
TextBuffer.prototype.removeTrailingSpaces = function( y = this.cy , x = null , dry = false ) {
if ( y >= this.buffer.length ) { return '' ; }
if ( ! this.buffer[ y ] ) { this.buffer[ y ] = [] ; }

var line = this.buffer[ y ] ;

x = x ?? line.length - 1 ;

if ( x < 0 || x >= line.length ) { return '' ; }

var deletedStr = '' ,
hasNL = line[ x ].char === '\n' ;

if ( hasNL ) {
x -- ;
}

for ( ; x >= 0 ; x -- ) {
if ( line[ x ].filler ) { continue ; }

let char = line[ x ].char ;

if ( char === ' ' || char === '\t' ) {
deletedStr = char + deletedStr ;
}
else {
break ;
}
}

if ( deletedStr && ! dry ) {
line.splice( x + 1 , deletedStr.length ) ;
}

return deletedStr ;
} ;



// TODOC
// Get the text, but separate before the cursor and after the cursor
TextBuffer.prototype.getCursorSplittedText = function() {
Expand Down Expand Up @@ -7131,34 +7200,6 @@ TextBuffer.prototype.deleteRegion = function( region , getDeleted = false ) {



// TODOC
// Get the indentation part of the line, return null if the line is empty (no char or no non-space char)
TextBuffer.prototype.getLineIndent = function( y = this.cy ) {
if ( ! this.buffer[ y ] ) { return null ; }

var x , xmin , xmax , cell ,
indent = '' ;

for ( x = 0 , xmax = this.buffer[ y ].length - 1 ; x <= xmax ; x ++ ) {
cell = this.buffer[ y ][ x ] ;
if ( ! cell.filler ) {
if ( cell.char === '\t' || cell.char === ' ' ) {
indent += cell.char ;
}
else if ( cell.char === '\n' ) {
return null ;
}
else {
return indent ;
}
}
}

return null ;
} ;



// Misc data are lazily created
TextBuffer.prototype.getMisc = function() {
if ( ! this.buffer[ this.cy ] || ! this.buffer[ this.cy ][ this.cx ] ) { return ; }
Expand Down
2 changes: 1 addition & 1 deletion browser/termkit.min.js

Large diffs are not rendered by default.

78 changes: 31 additions & 47 deletions lib/TextBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,29 @@ TextBuffer.prototype.getLineText = function( y = this.cy ) {


// TODOC
// Get spaces and tabs at the begining of the line
// Get the indentation part of the line, return null if the line is empty (no char or no non-space char)
TextBuffer.prototype.getLineIndent = function( y = this.cy ) {
if ( y >= this.buffer.length ) { return null ; }
if ( ! this.buffer[ y ] ) { this.buffer[ y ] = [] ; }
if ( ! this.buffer[ y ] ) { return null ; }

var indentStr = '' ;
var x , xmin , xmax , cell ,
indent = '' ;

for ( let x = 0 , xmax = this.buffer[ y ].length ; x < xmax ; x ++ ) {
let char = this.buffer[ y ][ x ].char ;
if ( char === ' ' || char === '\t' ) {
indentStr += char ;
}
else {
break ;
for ( x = 0 , xmax = this.buffer[ y ].length - 1 ; x <= xmax ; x ++ ) {
cell = this.buffer[ y ][ x ] ;
if ( ! cell.filler ) {
if ( cell.char === '\t' || cell.char === ' ' ) {
indent += cell.char ;
}
else if ( cell.char === '\n' ) {
return null ;
}
else {
return indent ;
}
}
}

return indentStr ;
return null ;
} ;


Expand Down Expand Up @@ -188,27 +193,34 @@ TextBuffer.prototype.removeTrailingSpaces = function( y = this.cy , x = null , d
if ( y >= this.buffer.length ) { return '' ; }
if ( ! this.buffer[ y ] ) { this.buffer[ y ] = [] ; }

var line = this.buffer[ y ] ;

x = x ?? line.length - 1 ;

if ( x < 0 || x >= line.length ) { return '' ; }

var deletedStr = '' ,
x = x ?? this.buffer[ y ].length - 1 ,
hasNL = this.buffer[ y ][ x ].char === '\n' ;
hasNL = line[ x ].char === '\n' ;

if ( hasNL ) {
x -- ;
}

for ( ; x >= 0 ; x -- ) {
let char = this.buffer[ y ][ x ].char ;

if ( line[ x ].filler ) { continue ; }

let char = line[ x ].char ;

if ( char === ' ' || char === '\t' ) {
deletedStr = char + deletedStr ;
}
else {
break ;
}
}

if ( deletedStr && ! dry ) {
this.buffer[ y ].splice( x + 1 , deletedStr.length ) ;
line.splice( x + 1 , deletedStr.length ) ;
}

return deletedStr ;
Expand Down Expand Up @@ -1022,34 +1034,6 @@ TextBuffer.prototype.deleteRegion = function( region , getDeleted = false ) {



// TODOC
// Get the indentation part of the line, return null if the line is empty (no char or no non-space char)
TextBuffer.prototype.getLineIndent = function( y = this.cy ) {
if ( ! this.buffer[ y ] ) { return null ; }

var x , xmin , xmax , cell ,
indent = '' ;

for ( x = 0 , xmax = this.buffer[ y ].length - 1 ; x <= xmax ; x ++ ) {
cell = this.buffer[ y ][ x ] ;
if ( ! cell.filler ) {
if ( cell.char === '\t' || cell.char === ' ' ) {
indent += cell.char ;
}
else if ( cell.char === '\n' ) {
return null ;
}
else {
return indent ;
}
}
}

return null ;
} ;



// Misc data are lazily created
TextBuffer.prototype.getMisc = function() {
if ( ! this.buffer[ this.cy ] || ! this.buffer[ this.cy ][ this.cx ] ) { return ; }
Expand Down
2 changes: 1 addition & 1 deletion lib/terminfo/terminfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function getTerminfoBuffer( termName ) {


function parseTermBuffer( bufPair , termName ) {
var i , j , getInt , intSize ,
var i , getInt , intSize ,
buf = bufPair.buf ,
offset = 0 ;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "terminal-kit",
"version": "2.11.3",
"version": "2.11.4",
"description": "256 colors, keys and mouse, input field, progress bars, screen buffer (including 32-bit composition and image loading), text buffer, and many more... Whether you just need colors and styles, build a simple interactive command line tool or a complexe terminal app: this is the absolute terminal lib for Node.js!",
"main": "lib/termkit.js",
"directories": {
Expand Down
4 changes: 2 additions & 2 deletions sample/document/drop-down-menu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ var dropDownMenu = new termkit.DropDownMenu( {
]
} ) ;

dropDownMenu.on( 'submit' , onSubmit ) ;
//dropDownMenu.on( 'blinked' , onSubmit ) ;
//dropDownMenu.on( 'submit' , onSubmit ) ;
dropDownMenu.on( 'blinked' , onSubmit ) ;

function onSubmit( buttonValue , action ) {
//console.error( 'Submitted: ' , value ) ;
Expand Down

0 comments on commit bea6010

Please sign in to comment.