generated from me/template-mit
prototype.js, refactor and add TODOs.
This commit is contained in:
1 parent
4bed4e5aff
commit
cbca8ac5d2
1 file changed
+89
-63
Vendored
+89
-63
@@ -23,10 +23,25 @@
|
|||||||
* - added args, basically took over the program without any incremental commits
|
* - added args, basically took over the program without any incremental commits
|
||||||
* */
|
* */
|
||||||
|
|
||||||
|
/* TODO
|
||||||
|
* - Add way to manage a DB such as ~/.confg/nomadic-weather/locations.json
|
||||||
|
* - -c argument for creating locations
|
||||||
|
* - Should act like "read" to intake data for Season, Nickname, Lat, Long, and Elevation.
|
||||||
|
* - -d argument for deleting locations
|
||||||
|
* - Displays the list and asks which index# to delete.
|
||||||
|
* - -m argument for modifying locations
|
||||||
|
* - Displays a list and asks which index to open, then goes through create-like process.
|
||||||
|
* - Tie locations to seasons, as noted for the GUI in the Design, and sort on that rather than elevation.
|
||||||
|
* */
|
||||||
|
|
||||||
|
/* * * Libraries * * */
|
||||||
|
|
||||||
const yargs = require('yargs/yargs');
|
const yargs = require('yargs/yargs');
|
||||||
const { hideBin } = require('yargs/helpers');
|
const { hideBin } = require('yargs/helpers');
|
||||||
|
|
||||||
const version = '0.1.0';
|
/* * * Variables * * */
|
||||||
|
|
||||||
|
const version = '0.1.1';
|
||||||
|
|
||||||
const days_fore_max = 16;
|
const days_fore_max = 16;
|
||||||
const days_fore_default = 12;
|
const days_fore_default = 12;
|
||||||
@@ -34,52 +49,22 @@ const days_past_default = 2;
|
|||||||
|
|
||||||
const ft_to_m = 0.3048;
|
const ft_to_m = 0.3048;
|
||||||
|
|
||||||
const argv = yargs(hideBin(process.argv))
|
const RED = "\x1b[31m";
|
||||||
.option('days_fore', {
|
const GRN = "\x1b[32m";
|
||||||
alias: 'f',
|
const YLW = "\x1b[33m";
|
||||||
describe: 'Set forecast days, such as "-f 10". Maximum of 16.',
|
const BLU = "\x1b[34m";
|
||||||
type: 'number',
|
const MGN = "\x1b[35m";
|
||||||
requiresArg: true,
|
const CYN = "\x1b[36m";
|
||||||
})
|
const WHT = "\x1b[37m";
|
||||||
.option('days_past', {
|
const RESET = "\x1b[0m";
|
||||||
alias: 'p',
|
|
||||||
describe: 'Set past days, such as "-p 3"',
|
|
||||||
type: 'number',
|
|
||||||
requiresArg: true,
|
|
||||||
})
|
|
||||||
.option('calc_elevation', {
|
|
||||||
alias: 'e',
|
|
||||||
describe: 'Set to calculate elevation at lat/long.',
|
|
||||||
type: 'boolean',
|
|
||||||
})
|
|
||||||
.check((argv) => {
|
|
||||||
if (argv.f !== undefined && (isNaN(argv.f) || argv.f < 0)) {
|
|
||||||
throw new Error('Forecast days must be a non-negative number');
|
|
||||||
}
|
|
||||||
if (argv.p !== undefined && (isNaN(argv.p) || argv.p < 0)) {
|
|
||||||
throw new Error('Past days must be a non-negative number');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
})
|
|
||||||
//.demandOption(['timeout', 'debug']) // Optional: require these arguments
|
|
||||||
.strict()
|
|
||||||
.help()
|
|
||||||
.version(version)
|
|
||||||
.argv
|
|
||||||
;
|
|
||||||
|
|
||||||
function max (value, upper) {
|
const pref_high_upper = 75;
|
||||||
return (value > upper) ? upper : value;
|
const pref_high_lower = 60;
|
||||||
}
|
const pref_low_upper = 55;
|
||||||
const days_fore = max(argv.hasOwnProperty("days_fore") ? argv.days_fore : days_fore_default, days_fore_max);
|
const pref_low_lower = 40;
|
||||||
const days_past = argv.hasOwnProperty("days_past") ? argv.days_past : days_past_default;
|
|
||||||
const calc_elevation = argv.calc_elevation ? true : false;
|
|
||||||
|
|
||||||
const days = days_fore + days_past;
|
const tolerance_light = 10;
|
||||||
|
const tolerance_heavy = 20;
|
||||||
console.log(`Averaging for ${days} days: ${days_fore} forecasted, ${days_past} past.`);
|
|
||||||
if (calc_elevation) console.log("Calculating elevation rather than using provided values.");
|
|
||||||
else console.log("Using user-provided elevations when they exist.");
|
|
||||||
|
|
||||||
const locations = [
|
const locations = [
|
||||||
//{ name: "Flagstaff, AZ", lat: 35.1983, lon: -111.6513, elevation: 6903 },
|
//{ name: "Flagstaff, AZ", lat: 35.1983, lon: -111.6513, elevation: 6903 },
|
||||||
@@ -108,27 +93,17 @@ const locations = [
|
|||||||
];
|
];
|
||||||
locations.sort((a, b) => a.elevation - b.elevation);
|
locations.sort((a, b) => a.elevation - b.elevation);
|
||||||
|
|
||||||
const RED = "\x1b[31m";
|
/* * * Functions * * */
|
||||||
const GRN = "\x1b[32m";
|
|
||||||
const YLW = "\x1b[33m";
|
|
||||||
const BLU = "\x1b[34m";
|
|
||||||
const MGN = "\x1b[35m";
|
|
||||||
const CYN = "\x1b[36m";
|
|
||||||
const WHT = "\x1b[37m";
|
|
||||||
const RESET = "\x1b[0m";
|
|
||||||
|
|
||||||
function highlight(value, min, max) {
|
function highlight(value, min, max) {
|
||||||
const n = parseFloat(value);
|
const n = parseFloat(value);
|
||||||
return (n >= min && n <= max) ? `${GRN}${value}${RESET}` : value;
|
return (n >= min && n <= max) ? `${GRN}${value}${RESET}` : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const pref_high_upper = 75;
|
function pad(str, width) {
|
||||||
const pref_high_lower = 60;
|
const visible = str.replace(/\x1b\[\d+m/g, '').length;
|
||||||
const pref_low_upper = 55;
|
return ' '.repeat(Math.max(0, width - visible)) + str;
|
||||||
const pref_low_lower = 40;
|
}
|
||||||
|
|
||||||
const tolerance_light = 10;
|
|
||||||
const tolerance_heavy = 20;
|
|
||||||
|
|
||||||
function check (value, min, max) {
|
function check (value, min, max) {
|
||||||
const n = parseFloat(value);
|
const n = parseFloat(value);
|
||||||
@@ -156,11 +131,60 @@ function check (value, min, max) {
|
|||||||
//return (n >= min && n <= max) ? `${GRN}${value}${RESET}` : value;
|
//return (n >= min && n <= max) ? `${GRN}${value}${RESET}` : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pad(str, width) {
|
function max (value, upper) {
|
||||||
const visible = str.replace(/\x1b\[\d+m/g, '').length;
|
return (value > upper) ? upper : value;
|
||||||
return ' '.repeat(Math.max(0, width - visible)) + str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* * * Parameters * * */
|
||||||
|
|
||||||
|
const argv = yargs(hideBin(process.argv))
|
||||||
|
.option('days_fore', {
|
||||||
|
alias: 'f',
|
||||||
|
describe: 'Future / forecast days, such as "-f 10". Maximum of 16.',
|
||||||
|
type: 'number',
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.option('days_past', {
|
||||||
|
alias: 'p',
|
||||||
|
describe: 'Past days, such as "-p 3"',
|
||||||
|
type: 'number',
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.option('calc_elevation', {
|
||||||
|
alias: 'e',
|
||||||
|
describe: 'Set to calculate elevation at lat/long rather than use supplied.',
|
||||||
|
type: 'boolean',
|
||||||
|
})
|
||||||
|
.check((argv) => {
|
||||||
|
if (argv.f !== undefined && (isNaN(argv.f) || argv.f < 0)) {
|
||||||
|
throw new Error('Forecast days must be a non-negative number');
|
||||||
|
}
|
||||||
|
if (argv.p !== undefined && (isNaN(argv.p) || argv.p < 0)) {
|
||||||
|
throw new Error('Past days must be a non-negative number');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
//.demandOption(['timeout', 'debug']) // Optional: require these arguments
|
||||||
|
.strict()
|
||||||
|
.help()
|
||||||
|
.version(version)
|
||||||
|
.argv
|
||||||
|
;
|
||||||
|
|
||||||
|
/* * * Processing * * */
|
||||||
|
|
||||||
|
const days_fore = max(argv.hasOwnProperty("days_fore") ? argv.days_fore : days_fore_default, days_fore_max);
|
||||||
|
const days_past = argv.hasOwnProperty("days_past") ? argv.days_past : days_past_default;
|
||||||
|
const calc_elevation = argv.calc_elevation ? true : false;
|
||||||
|
|
||||||
|
const days = days_fore + days_past;
|
||||||
|
|
||||||
|
console.log(`Averaging for ${days} days: ${days_fore} forecasted, ${days_past} past.`);
|
||||||
|
if (calc_elevation) console.log("Calculating elevation rather than using provided values.");
|
||||||
|
else console.log("Using user-provided elevations when they exist.");
|
||||||
|
|
||||||
|
/* * * Main * * */
|
||||||
|
|
||||||
async function getForecast(loc) {
|
async function getForecast(loc) {
|
||||||
const url = new URL("https://api.open-meteo.com/v1/forecast");
|
const url = new URL("https://api.open-meteo.com/v1/forecast");
|
||||||
url.searchParams.set("latitude", loc.lat);
|
url.searchParams.set("latitude", loc.lat);
|
||||||
@@ -213,3 +237,5 @@ async function getForecast(loc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
/* * * Finish * * */
|
||||||
Reference in new issue
Block a user