From afe7a530092720a4cd85a3ab28e74c35dd320eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Stra=C3=9Fburger?= Date: Sat, 1 Oct 2016 21:05:16 +0200 Subject: [PATCH] :left_right_arrow: adding config for used input/output stream --- README.md | 2 +- src/Canvas.coffee | 4 ++-- src/Renderer.coffee | 9 ++++----- src/Termap.coffee | 34 +++++++++++++++++++++------------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1338caf..e31f441 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ No web browser around? No worries - discover the planet in your console! * [ ] calculate tile areas * [ ] center based on mercator * [x] handle console resize -* [x] handle console resize + * [x] handle console resize * [ ] mouse control * [x] accurate mouse drag&drop with instant update * [x] hover POIs/labels diff --git a/src/Canvas.coffee b/src/Canvas.coffee index 4e3e5ac..6ff6f0b 100644 --- a/src/Canvas.coffee +++ b/src/Canvas.coffee @@ -31,8 +31,8 @@ module.exports = class Canvas reset: -> @matrix = mat2d.create() - print: -> - process.stdout.write @buffer.frame() + frame: -> + @buffer.frame() translate: (x, y) -> mat2d.translate @matrix, @matrix, vec2.fromValues(x, y) diff --git a/src/Renderer.coffee b/src/Renderer.coffee index d8cab35..56dc7fd 100644 --- a/src/Renderer.coffee +++ b/src/Renderer.coffee @@ -23,6 +23,7 @@ module.exports = class Renderer drawOrder: [ "admin" "building" + "road" "water" "road:structure=bridge" @@ -65,7 +66,7 @@ module.exports = class Renderer labelBuffer: null - constructor: -> + constructor: (@output) -> @labelBuffer = new LabelBuffer() loadStyleFile: (file) -> @@ -83,7 +84,6 @@ module.exports = class Renderer @labelBuffer.clear() - # TODO: better way for background color instead of setting filling FG? if color = @styler.styleById['background']?.paint['background-color'] @canvas.setBackground x256 utils.hex2rgb color @@ -93,15 +93,14 @@ module.exports = class Renderer @canvas.translate @view[0], @view[1] @_drawLayers() - process.stdout.cursorTo 0, 0 - @canvas.print() + @output.write "\x1B[?6h" + @output.write @canvas.frame() @isDrawing = false featuresAt: (x, y) -> @labelBuffer.featuresAt x, y - _write: (output) -> process.stdout.write output diff --git a/src/Termap.coffee b/src/Termap.coffee index fdb6019..eda45ed 100644 --- a/src/Termap.coffee +++ b/src/Termap.coffee @@ -15,6 +15,9 @@ utils = require './utils' module.exports = class Termap config: + input: process.stdin + output: process.stdout + styleFile: __dirname+"/../styles/bright.json" zoomStep: 0.4 @@ -34,18 +37,23 @@ module.exports = class Termap zoom: 0 view: [0, 0] - constructor: -> - @_initControls() + constructor: (options) -> + @config[key] = val for key, val of options + + @_initKeyboard() + @_initMouse() + @_initRenderer() - _initControls: -> - keypress process.stdin - process.stdin.setRawMode true - process.stdin.resume() + _initKeyboard: -> + keypress @config.input + @config.input.setRawMode true + @config.input.resume() - process.stdin.on 'keypress', (ch, key) => @_onKey key + @config.input.on 'keypress', (ch, key) => @_onKey key - @mouse = TermMouse() + _initMouse: -> + @mouse = TermMouse input: @config.input, output: @config.output @mouse.start() @mouse.on 'click', (event) => @_onClick event @@ -53,10 +61,10 @@ module.exports = class Termap @mouse.on 'move', (event) => @_onMouseMove event _initRenderer: -> - @renderer = new Renderer() + @renderer = new Renderer @config.output @renderer.loadStyleFile @config.styleFile - process.stdout.on 'resize', => + @config.output.on 'resize', => @_resizeRenderer() @_draw() @@ -64,8 +72,8 @@ module.exports = class Termap @zoom = Math.log(4096/@width)/Math.LN2 _resizeRenderer: (cb) -> - @width = process.stdout.columns >> 1 << 2 - @height = process.stdout.rows * 4 - 4 + @width = @config.output.columns >> 1 << 2 + @height = @config.output.rows * 4 - 4 @renderer.setSize @width, @height @@ -84,7 +92,7 @@ module.exports = class Termap _onMouseMove: (event) -> # only continue if x/y are valid - return unless event.x <= process.stdout.columns and event.y <= process.stdout.rows + return unless event.x <= @config.output.columns and event.y <= @config.output.rows # start dragging if event.button is "left"