From 6c254a989929964a9b4e573a210eb927ec6c8f3a Mon Sep 17 00:00:00 2001 From: ghaiklor Date: Mon, 11 Jan 2016 17:29:25 +0200 Subject: [PATCH] feat(shape): Update basic kittik shape This change allows to align shapes within the terminal --- examples/simple.js | 18 +++++++++--------- src/Rectangle.js | 6 +----- test/unit/Rectangle.test.js | 38 +++++++++++++------------------------ 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/examples/simple.js b/examples/simple.js index df6696e..f9aa324 100644 --- a/examples/simple.js +++ b/examples/simple.js @@ -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(); diff --git a/src/Rectangle.js b/src/Rectangle.js index 7cf764a..38489fc 100644 --- a/src/Rectangle.js +++ b/src/Rectangle.js @@ -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) { @@ -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); diff --git a/test/unit/Rectangle.test.js b/test/unit/Rectangle.test.js index 5663049..247b4c9 100644 --- a/test/unit/Rectangle.test.js +++ b/test/unit/Rectangle.test.js @@ -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', () => { @@ -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); @@ -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 } }; @@ -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()); }); });