Skip to content

Commit

Permalink
Add missing API method: TextBox#setTabWidth()
Browse files Browse the repository at this point in the history
  • Loading branch information
cronvel committed Sep 16, 2022
1 parent e19b0f6 commit 9fb28ce
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 255 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

v2.11.6
-------

Add missing API method: TextBox#setTabWidth()


v2.11.5
-------

Expand Down
634 changes: 395 additions & 239 deletions browser/termkit.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion browser/termkit.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/TextBox.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ See [Element's key event](Element.md#ref.Element.event.key).
* scrollY `number` the initial vertical scroll value, default: 0
* extraScrolling `boolean` if unset (the default), it is possible to scroll down until both the content bottom and textBox bottom are on the same line,
if set, it is possible to scroll down until the bottom of the content reaches the top of the textBox
* tabWidth `number` (default: 4) number of cells (=spaces) for the tab character
* lineWrap `boolean` when set, the text content is wrapped to the next line instead of being clipped by the textBox border
* wordWrap `boolean` like `lineWrap` but is word-aware, i.e. it doesn't split words
* firstLineRightShift `number` if set (default: 0) , the first-line of content is right-shifted from this amount of cells, may be useful for prompt,
Expand Down
19 changes: 18 additions & 1 deletion lib/TextBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function TextBuffer( options = {} ) {

this.hidden = false ;

this.tabWidth = options.tabWidth || 4 ;
this.tabWidth = + options.tabWidth || 4 ;
this.forceInBound = !! options.forceInBound ;

// If set to a number, force line-splitting when exceeding that width
Expand Down Expand Up @@ -434,6 +434,23 @@ TextBuffer.prototype.setCursorOffset = function( offset ) {



// TODOC
TextBuffer.prototype.setTabWidth = function( tabWidth ) {
this.tabWidth = + tabWidth || 4 ;
this.reTab() ;
} ;



// TODOC
TextBuffer.prototype.reTab = function() {
for ( let y = 0 ; y < this.buffer.length ; y ++ ) {
this.reTabLine( 0 , y ) ;
}
} ;



// Recompute tabs
TextBuffer.prototype.reTabLine = function( startAt = 0 , y = this.cy ) {
var length , cell , index , fillSize , input , output ,
Expand Down
11 changes: 0 additions & 11 deletions lib/document/EditableTextBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ function EditableTextBox( options ) {
this.onDragEnd = this.onDragEnd.bind( this ) ;
this.onMiddleClick = this.onMiddleClick.bind( this ) ;

// Hooks
this.newLineAutoIndentHook = options.newLineAutoIndentHook ?? null ;

if ( options.keyBindings ) { this.keyBindings = options.keyBindings ; }

// Editable textbox get extraScrolling by default
Expand Down Expand Up @@ -337,14 +334,6 @@ userActions.newLine = function() {

this.textBuffer.newLine() ;

if ( this.newLineAutoIndentHook ) {
let indentStr = this.newLineAutoIndentHook() ;
if ( indentStr ) {
count += this.textBuffer.insert( indentStr ) ;
insertedString += indentStr ;
}
}

if ( this.stateMachine ) {
this.textBuffer.runStateMachine() ;
}
Expand Down
17 changes: 17 additions & 0 deletions lib/document/TextBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function TextBox( options ) {
// Right shift of the first-line, may be useful for prompt, or continuing another box in the flow
this.firstLineRightShift = options.firstLineRightShift || 0 ;

this.tabWidth = options.tabWidth || 4 ; // How many cells (=spaces) for the tab character

this.wordWrap = !! ( options.wordWrap || options.wordwrap ) ;
this.lineWrap = !! ( options.lineWrap || this.wordWrap ) ;

Expand Down Expand Up @@ -142,6 +144,7 @@ TextBox.prototype.initChildren = function() {
//width: this.textAreaWidth ,
//height: this.textAreaHeight
firstLineRightShift: this.firstLineRightShift ,
tabWidth: this.tabWidth ,
lineWrapWidth: this.lineWrap ? this.textAreaWidth : null ,
wordWrap: this.wordWrap ,
dstClipRect: {
Expand All @@ -161,6 +164,7 @@ TextBox.prototype.initChildren = function() {
if ( this.useAltTextBuffer ) {
this.altTextBuffer = new TextBuffer( {
firstLineRightShift: this.firstLineRightShift ,
tabWidth: this.tabWidth ,
lineWrapWidth: this.lineWrap ? this.textAreaWidth : null ,
wordWrap: this.wordWrap ,
dstClipRect: {
Expand Down Expand Up @@ -516,6 +520,19 @@ TextBox.prototype.addContent = function( content , mode , dontDraw ) {



// TODOC
TextBox.prototype.setTabWidth = function( tabWidth , internal = false ) {
this.tabWidth = + tabWidth || 4 ;
this.textBuffer.setTabWidth( this.tabWidth ) ;
if ( this.altTextBuffer ) { this.altTextBuffer.setTabWidth( this.tabWidth ) ; }

if ( ! internal ) {
this.draw() ;
}
} ;



// TODOC
TextBox.prototype.setStateMachine = function( stateMachine , internal = false ) {
this.stateMachine = stateMachine ;
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.5",
"version": "2.11.6",
"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
7 changes: 5 additions & 2 deletions sample/document/editable-text-box-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var document = term.createDocument( {
//var placeHolder = 'var name = "Bob" ;\nconsole.log( `Hello ${name}! How are you?` ) ;\nconsole.log( `Hello $name}! How are you?` ) ;\n\n' ;
//var placeHolder = 'fn( 1 ) ;\n\n' ;
//var placeHolder = 're = /^some regexp (bob|bill) [a-zA-Z_-]+$/g ;\n' ;
var placeHolder = 're = {\n prop: "value" ,\n key: 3\n} ;\n' ;
var placeHolder = 're = {\n\tprop: "value" ,\n\tkey: 3\n} ;\n' ;
//var placeHolder = '\n' ;

try {
Expand Down Expand Up @@ -79,15 +79,18 @@ var textBox = new termkit.EditableTextBox( {
height: 20 ,
scrollable: true ,
vScrollBar: true ,
//tabWidth: 2 ,
//lineWrap: true ,
wordWrap: true ,
stateMachine: stateMachine
} ) ;

document.giveFocusTo( textBox ) ;

//setTimeout( () => textBox.setTabWidth( 8 ) , 1000 ) ;
//setTimeout( () => textBox.setTabWidth( 2 ) , 2000 ) ;

term.on( 'key' , function( key ) {

switch( key ) {
case 'CTRL_C' :
term.grabInput( false ) ;
Expand Down

0 comments on commit 9fb28ce

Please sign in to comment.