↔️ adding config for used input/output stream

This commit is contained in:
Michael Straßburger 2016-10-01 21:05:16 +02:00
parent d0ada3f13d
commit afe7a53009
4 changed files with 28 additions and 21 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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"