prototype.js, refactor and add TODOs.

This commit is contained in:
me committed 2026-09-12 15:23:00 -07:00
1 parent 4bed4e5aff
commit cbca8ac5d2
1 file changed
+89 -63
+89 -63
View File
@@ -23,10 +23,25 @@
* - 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 { hideBin } = require('yargs/helpers');
const version = '0.1.0';
/* * * Variables * * */
const version = '0.1.1';
const days_fore_max = 16;
const days_fore_default = 12;
@@ -34,52 +49,22 @@ const days_past_default = 2;
const ft_to_m = 0.3048;
const argv = yargs(hideBin(process.argv))
.option('days_fore', {
alias: 'f',
describe: 'Set forecast days, such as "-f 10". Maximum of 16.',
type: 'number',
requiresArg: true,
})
.option('days_past', {
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
;
const RED = "\x1b[31m";
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 max (value, upper) {
return (value > upper) ? upper : value;
}
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 pref_high_upper = 75;
const pref_high_lower = 60;
const pref_low_upper = 55;
const pref_low_lower = 40;
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.");
const tolerance_light = 10;
const tolerance_heavy = 20;
const locations = [
//{ 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);
const RED = "\x1b[31m";
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";
/* * * Functions * * */
function highlight(value, min, max) {
const n = parseFloat(value);
return (n >= min && n <= max) ? `${GRN}${value}${RESET}` : value;
}
const pref_high_upper = 75;
const pref_high_lower = 60;
const pref_low_upper = 55;
const pref_low_lower = 40;
const tolerance_light = 10;
const tolerance_heavy = 20;
function pad(str, width) {
const visible = str.replace(/\x1b\[\d+m/g, '').length;
return ' '.repeat(Math.max(0, width - visible)) + str;
}
function check (value, min, max) {
const n = parseFloat(value);
@@ -156,11 +131,60 @@ function check (value, min, max) {
//return (n >= min && n <= max) ? `${GRN}${value}${RESET}` : value;
}
function pad(str, width) {
const visible = str.replace(/\x1b\[\d+m/g, '').length;
return ' '.repeat(Math.max(0, width - visible)) + str;
function max (value, upper) {
return (value > upper) ? upper : value;
}
/* * * 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) {
const url = new URL("https://api.open-meteo.com/v1/forecast");
url.searchParams.set("latitude", loc.lat);
@@ -213,3 +237,5 @@ async function getForecast(loc) {
}
}
})();
/* * * Finish * * */