Skip to content

Commit

Permalink
🎨 adding braille-free mode, activate with 'c'
Browse files Browse the repository at this point in the history
  • Loading branch information
rastapasta committed May 12, 2017
1 parent 46692ec commit 7e255ed
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
43 changes: 38 additions & 5 deletions src/BrailleBuffer.coffee
Expand Up @@ -6,24 +6,34 @@
Implementation inspired by node-drawille (https://github.com/madbence/node-drawille)
* added color support
* added support for filled polygons
* added text label support
* general optimizations
-> more bit shifting/operations, less Math.floors
Will either be merged into node-drawille or become an own module at some point
###
stringWidth = require 'string-width'
config = require './config'
utils = require './utils'

module.exports = class BrailleBuffer
characterMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]]
brailleMap: [[0x1, 0x8],[0x2, 0x10],[0x4, 0x20],[0x40, 0x80]]
asciiMap:
#"▬": [2+32, 4+64]
# "▌": [1+2+4+8]
# "▐": [16+32+64+128]
"": [1+2+16+32]
"": [4+8+64+128]
"": [2+4+32+64]
#"▓": [1+4+32+128, 2+8+16+64]
"": [255]

pixelBuffer: null
charBuffer: null
foregroundBuffer: null
backgroundBuffer: null

asciiToBraille: []

globalBackground: null

termReset: "\x1B[39;49m"
Expand All @@ -33,6 +43,8 @@ module.exports = class BrailleBuffer
@pixelBuffer = new Buffer size
@foregroundBuffer = new Buffer size
@backgroundBuffer = new Buffer size

@_mapBraille()
@clear()

clear: ->
Expand Down Expand Up @@ -63,9 +75,27 @@ module.exports = class BrailleBuffer
_locate: (x, y, cb) ->
return unless 0 <= x < @width and 0 <= y < @height
idx = @_project x, y
mask = @characterMap[y&3][x&1]
mask = @brailleMap[y&3][x&1]
cb idx, mask

_mapBraille: ->
@asciiToBraille = [" "]

masks = []
for char, bits of @asciiMap
continue unless bits instanceof Array
masks.push mask: mask, char: char for mask in bits

reducer = (best, mask) ->
covered = utils.population(mask.mask&i)
return if not best or best.covered < covered
char: mask.char, covered: covered
else
best

for i in [1..255]
@asciiToBraille[i] = masks.reduce(reducer, undefined).char

_termColor: (foreground, background) ->
background = background or @globalBackground
if foreground and background
Expand Down Expand Up @@ -102,7 +132,10 @@ module.exports = class BrailleBuffer
char
else
if not skip
String.fromCharCode 0x2800+@pixelBuffer[idx]
if config.useBraille
String.fromCharCode 0x2800+@pixelBuffer[idx]
else
@asciiToBraille[@pixelBuffer[idx]]
else
skip--
''
Expand Down
4 changes: 4 additions & 0 deletions src/Mapscii.coffee
Expand Up @@ -179,6 +179,10 @@ module.exports = class Mapscii
when "up" then @moveBy 6/Math.pow(2, @zoom), 0
when "down" then @moveBy -6/Math.pow(2, @zoom), 0

when "c"
config.useBraille = !config.useBraille
true

else
null

Expand Down
4 changes: 2 additions & 2 deletions src/Tile.coffee
Expand Up @@ -57,8 +57,8 @@ class Tile
feature.properties.$type = type = [undefined, "Point", "LineString", "Polygon"][feature.type]

if @styler
style = @styler.getStyleFor name, feature
continue unless style
style = @styler.getStyleFor name, feature
continue unless style

color =
style.paint['line-color'] or
Expand Down
2 changes: 2 additions & 0 deletions src/config.coffee
Expand Up @@ -15,6 +15,8 @@ module.exports =

simplifyPolylines: false

useBraille: true

# Downloaded files get persisted in ~/.mapscii
persistDownloadedTiles: true

Expand Down
7 changes: 7 additions & 0 deletions src/utils.coffee
Expand Up @@ -64,4 +64,11 @@ utils =

ll

population: (val) ->
bits = 0
while val>0
bits += val & 1
val >>= 1
bits

module.exports = utils

0 comments on commit 7e255ed

Please sign in to comment.