Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getState method #613

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function createTerminal() {
protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/terminals/';

term.open(terminalContainer);
term.open(terminalContainer, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the , true as this arg was removed

term.fit();

var initialGeometry = term.proposeGeometry(),
Expand Down
81 changes: 81 additions & 0 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,87 @@ function Terminal(options) {

inherits(Terminal, EventEmitter);

/**
* Returns the current position and style of the terminal cursor.
* It returns an object in the following form: {position: [x, y], style: "cursorStyle"}
*/
Terminal.prototype._getCursor = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be deleted since we're on v3 now.

return {
position: [this.x, this.y],
style: this.getOption('cursorStyle')
};
};


/**
* Returns the current mode of the terminal. Can be one of the following:
* - application
* - insert
* - normal
*/
Terminal.prototype._getMode = function() {
if (this.applicationKeypad) {
return 'application';
} else if (this.insertMode) {
return 'insert';
}

return 'normal';
};


/**
* Serialize and return the terminal's state as a JSON object.
* Also iterate through all terminal properties that have a `getState` methods and embed their
* state as well.
*/
Terminal.prototype.getState = function() {
const properties = Object.keys(this);
const availableOptions = [
'cursorBlink', 'disableStdin', 'scrollback', 'tabStopWidth', 'useFlowControl'
];

let normalBuffer = this.lines._array;
let altBuffer = null;

// This means that the alt buffer is active
if (this.normal) {
altBuffer = normalBuffer;
normalBuffer = this.normal.lines._array;
}

let state = {
buffers: {
normal: normalBuffer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should create a CircularList.getState function which returns data from indexes 0 to CircularList.length. That's all the data that's meaningful:

screen shot 2017-05-27 at 3 14 09 pm

With the 24 lines from the CircularList it should be easy to create a new CircularList constructor or setState method which sets the length to 24 and puts those 24 lines into the list.

alt: altBuffer
},
cursor: this._getCursor(),
geometry: this.geometry,
mode: this._getMode(),
options: {}
};

availableOptions.forEach(function (option) {
state.options[option] = term.getOption(option);
});

// Iterate through all terminal properties to embed their state as well.
for (let i in properties) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this retrieve any data yet? Maybe we should comment it out if it's not used yet?

let key = properties[i];
let value = this[key];

if (!value) {
continue;
}

if (typeof value.getState == 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 when Terminal is in TS I think we can do if value instanceof ISerializable which is even nicer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move forward with this and implement an interface ISerializable. For serialization tests this can be done at the level of the component as well (tests for the Buffer go in Buffer.test.ts).

state[key] = value.getState();
}
}

return state;
};

/**
* back_color_erase feature for xterm.
*/
Expand Down