Add preliminary support for parsing of command line options (#86)

* Add preliminary support for parsing of command line options

* Remove redundant options for width and height

* Add parser for command line arguments. Type validation outstanding.

* Update TODO list

* Use config for argv's default values, simplify argument management

Co-authored-by: LH <@>
This commit is contained in:
Thorkil Værge 2020-08-12 01:44:39 +02:00 committed by GitHub
parent 219161e13e
commit 34d628d1d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 23 deletions

View File

@ -99,11 +99,11 @@ If your terminal supports mouse events you can drag the map and use your scroll
* MapSCII
* [ ] GeoJSON support via [geojson-vt](https://github.com/mapbox/geojson-vt)
* [ ] CLI support
* [ ] startup parameters
* [ ] TileSource
* [ ] Style
* [ ] center position
* [ ] zoom
* [-] startup parameters
* [X] TileSource
* [X] Style
* [X] center position
* [X] zoom
* [ ] demo mode?
* [ ] mouse control

72
main.js
View File

@ -8,9 +8,79 @@
TODO: params parsing and so on
#*/
'use strict';
const config = require('./src/config');
const Mapscii = require('./src/Mapscii');
const argv = require('yargs')
.option('latitude', {
alias: 'lat',
description: 'Latitude of initial centre',
default: config.initialLat,
type: 'number',
})
.option('longitude', {
alias: 'lon',
description: 'Longitude of initial centre',
default: config.initialLon,
type: 'number',
})
.option('zoom', {
alias: 'z',
description: 'Initial zoom',
default: config.initialZoom,
type: 'number',
})
.option('width', {
alias: 'w',
description: 'Fixed width of rendering in dot units',
type: 'number',
})
.option('height', {
alias: 'h',
description: 'Fixed height of rendering in dot units',
type: 'number',
})
.option('braille', {
alias: 'b',
description: 'Activate braille rendering',
default: config.useBraille,
type: 'boolean',
})
.option('headless', {
alias: 'H',
description: 'Activate headless mode',
default: config.headless,
type: 'boolean',
})
.option('tile_source', {
alias: 'tileSource',
description: 'URL or path to osm2vectortiles source',
default: config.source,
type: 'string',
})
.option('style_file', {
alias: 'style',
description: 'path to json style file',
default: config.styleFile,
type: 'string',
})
.strict()
.argv;
const mapscii = new Mapscii();
const options = {
initialLat: argv.latitude,
initialLon: argv.longitude,
initialZoom: argv.zoom,
size: {
width: argv.width,
height: argv.height
},
useBraille: argv.braille,
headless: argv.headless,
source: argv.tile_source,
styleFile: argv.style_file,
}
const mapscii = new Mapscii(options);
mapscii.init().catch((err) => {
console.error('Failed to start MapSCII.');
console.error(err);

View File

@ -42,7 +42,8 @@
"string-width": "^4.2.0",
"term-mouse": "^0.2.2",
"userhome": "^1.0.0",
"x256": "0.0.2"
"x256": "0.0.2",
"yargs": "^15.3.1"
},
"devDependencies": {
"eslint": "^7.0.0",

View File

@ -31,17 +31,13 @@ class Mapscii {
this.renderer = null;
this.zoom = 0;
// sf lat: 37.787946, lon: -122.407522
// iceland lat: 64.124229, lon: -21.811552
// rgbg
// lat: 49.019493, lon: 12.098341
this.center = {
lat: 52.51298,
lon: 13.42012,
};
this.minZoom = null;
config = Object.assign(config, options);
this.center = {
lat: config.initialLat,
lon: config.initialLon
};
}
async init() {
@ -97,13 +93,8 @@ class Mapscii {
}
_resizeRenderer() {
if (config.size) {
this.width = config.size.width;
this.height = config.size.height;
} else {
this.width = config.output.columns >> 1 << 2;
this.height = config.output.rows * 4 - 4;
}
this.width = config.size && config.size.width ? config.size.width : config.output.columns >> 1 << 2;
this.height = config.size && config.size.height ? config.size.height : config.output.rows * 4 - 4;
this.minZoom = 4-Math.log(4096/this.width)/Math.LN2;

View File

@ -13,6 +13,13 @@ module.exports = {
maxZoom: 18,
zoomStep: 0.2,
// sf lat: 37.787946, lon: -122.407522
// iceland lat: 64.124229, lon: -21.811552
// rgbg
// lat: 49.019493, lon: 12.098341
initialLat: 52.51298,
initialLon: 14.42012,
simplifyPolylines: false,
useBraille: true,