Skip to content

Commit

Permalink
feat(shape): Update basic kittik shape
Browse files Browse the repository at this point in the history
This change allows to align shapes within the terminal
  • Loading branch information
ghaiklor committed Jan 11, 2016
1 parent bdaebfd commit 6c254a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
18 changes: 9 additions & 9 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ const cursor = require('kittik-cursor').Cursor.create().resetTTY();

Rectangle.create({
text: 'Rectangle',
x: 20,
x: 'center',
y: 2,
width: 15,
height: 5,
background: cursor.COLORS.GREEN,
foreground: cursor.COLORS.BLACK
background: 'green',
foreground: 'black'
}).render(cursor);

Rectangle.create({
text: 'Banana, Banana!!!',
x: 40,
y: 10,
width: 30,
height: 3,
background: cursor.COLORS.DARK_BLUE,
foreground: cursor.COLORS.WHITE
x: 'center',
y: 'middle',
width: '50%',
height: 5,
background: 'dark_blue',
foreground: 'white'
}).render(cursor);

cursor.flush();
6 changes: 1 addition & 5 deletions src/Rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Shape from 'kittik-shape-basic';
* Implements rectangle shape with text support.
*
* @since 1.0.0
* @version 1.0.0
*/
export default class Rectangle extends Shape {
render(cursor) {
Expand All @@ -24,10 +23,7 @@ export default class Rectangle extends Shape {

cursor.moveTo(x1, y1);

for (let y = y1; y <= y2; y++) {
cursor.write(filler);
cursor.moveTo(x1, y);
}
for (let y = y1; y <= y2; y++) cursor.write(filler).moveTo(x1, y);

cursor.moveTo(x1 + (width / 2 - text.length / 2), y1 + (height / 2)).write(text);

Expand Down
38 changes: 13 additions & 25 deletions test/unit/Rectangle.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from 'chai';
import sinon from 'sinon';
import Rectangle from '../../src/Rectangle';
import { Cursor, COLORS } from 'kittik-cursor';
import { Cursor } from 'kittik-cursor';

describe('Shape::Rectangle', () => {
it('Should properly render with default options', () => {
Expand All @@ -28,12 +28,12 @@ describe('Shape::Rectangle', () => {
height: 11,
x: 1,
y: 1,
background: COLORS.YELLOW,
foreground: COLORS.BLACK
background: 'yellow',
foreground: 'black'
});

mock.expects('background').once().withArgs(11);
mock.expects('foreground').once().withArgs(0);
mock.expects('background').once().withArgs('yellow');
mock.expects('foreground').once().withArgs('black');
mock.expects('moveTo').exactly(14).returns(cursor);
mock.expects('write').exactly(13).returns(cursor);

Expand All @@ -47,39 +47,30 @@ describe('Shape::Rectangle', () => {
let obj = rectangle.toObject();

assert.deepEqual(obj, {
name: 'Rectangle',
type: 'Rectangle',
options: {
text: '',
width: 15,
height: 5,
x: 10,
y: 10,
alignX: 'none',
alignY: 'none',
background: undefined,
foreground: undefined,
animation: undefined
foreground: undefined
}
});
});

it('Should properly create rectangle from Object representation', () => {
let obj = {
name: 'Rectangle',
type: 'Rectangle',
options: {
text: 'test',
width: 30,
height: 50,
x: 0,
y: 0,
x: 1,
y: 1,
background: undefined,
foreground: undefined,
animation: {
name: 'print',
options: {
interval: 100
}
}
foreground: undefined
}
};

Expand All @@ -88,12 +79,9 @@ describe('Shape::Rectangle', () => {
assert.equal(rectangle.getText(), 'test');
assert.equal(rectangle.getWidth(), 30);
assert.equal(rectangle.getHeight(), 50);
assert.equal(rectangle.getX(), 0);
assert.equal(rectangle.getY(), 0);
assert.equal(rectangle.getX(), 1);
assert.equal(rectangle.getY(), 1);
assert.isUndefined(rectangle.getBackground());
assert.isUndefined(rectangle.getForeground());
assert.equal(rectangle.getAnimationName(), 'print');
assert.deepEqual(rectangle.getAnimationOptions(), {interval: 100});
assert.ok(rectangle.isAnimated());
});
});

0 comments on commit 6c254a9

Please sign in to comment.