Skip to content

Commit

Permalink
feat(package): ✨ favorite city system
Browse files Browse the repository at this point in the history
  • Loading branch information
suptower committed Aug 7, 2023
1 parent a393363 commit 85e8015
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
47 changes: 47 additions & 0 deletions bin/configHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const configHandler = async noclear => {
{ title: "Set temperature unit", value: "unit" },
{ title: "Set forecast hour traversing style", value: "traverse" },
{ title: "Preset Times Configuration", value: "preset" },
{ title: "Set favorite location", value: "location"},
{ title: "Set default action for blank command", value: "default"},
{ title: "Clear config", value: "clear" },
{ title: "Cancel", value: "cancel" },
],
Expand Down Expand Up @@ -57,6 +59,14 @@ export const configHandler = async noclear => {
if (response.config === "preset") {
await presetConfig();
await recall(false);
}
if (response.config === "location") {
await setLocation();
await recall(false);
}
if (response.config === "default") {
await setDefault();
await recall(false);
} else if (response.config === "cancel") {
process.exit(0);
}
Expand Down Expand Up @@ -364,3 +374,40 @@ const resetPresets = async () => {
await config.set("presetOptions", presetTimes);
console.log(chalk.green("Preset times reset."));
};

const setLocation = async () => {
const current = await config.get("location");
console.log(chalk.yellow("Current favorite location: " + chalk.magenta(current)));
const location = await prompts({
type: "text",
name: "value",
message: "Enter a favorite location",
initial: current,
});
if (location.value !== undefined && location.value !== "") {
await config.set("location", location.value);
console.log(chalk.green("Favorite location set."));
} else {
console.log(chalk.red("Favorite location undefined."));
}
}

const setDefault = async () => {
const current = await config.get("default");
console.log(chalk.yellow("Current default preset: " + chalk.magenta(current)));
const action = await prompts({
type: "select",
name: "value",
message: "Select a default preset",
choices: [
{ title: "Prompt Menu", value: "prompt" },
{ title: "Three Day Forecast", value: "threeday"},
],
});
if (action.value !== undefined) {
await config.set("default", action.value);
console.log(chalk.green("Default preset set."));
} else {
console.log(chalk.red("Default preset undefined."));
}
}
17 changes: 16 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import gradient from "gradient-string";
// read package.json
import { fileURLToPath } from "url";
import path, { dirname } from "path";
import { tripleForecast } from "./tripleForecast.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand All @@ -53,6 +54,14 @@ const schema = {
{ title: "Evening", value: "18" },
],
},
location: {
type: "string",
default: "Munich",
},
default: {
type: "string",
default: "threeday",
}
};

const config = new Conf({ projectName: "weather-cli", schema });
Expand Down Expand Up @@ -198,6 +207,12 @@ if (options.config) {
if (argv.length > 0) {
weatherprompt(argv.join(" "));
} else {
weatherprompt();
const fav = await config.get("location");
const setting = await config.get("default");
if (setting === "threeday") {
tripleForecast(fav);
} else {
weatherprompt(fav);
}
}
}
25 changes: 1 addition & 24 deletions bin/resources/weather_icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,6 @@ const conditionIcons = {
VeryCloudy: COLOR_250(" \n .--. \n .-( ). \n (___.__)__) ") + COLOR_111("\n "),
};

/* Test function
Object.entries(conditionIcons).forEach(([key, value]) => {
console.log(chalk.blue(key));
console.log(value);
});
*/

export function getIcon(iconCode) {
for (let i = 0; i < weatherConditions.length; i++) {
if (weatherConditions[i].code === iconCode) {
Expand Down Expand Up @@ -206,20 +199,4 @@ function drawIcon(iconCondition) {
default:
return conditionIcons.Sunny;
}
}

/* not in use right now
// take string with multiple lines
// pad the lines with spaces to the center
// join the lines back together
// return the string
function padCenter(string) {
const lines = string.split("\n");
const maxLength = Math.max(...lines.map(line => line.length));
const paddedLines = lines.map(line => {
const spaces = " ".repeat((maxLength - line.length) / 2);
return spaces + line + spaces;
});
return paddedLines.join("\n");
}
*/
}

0 comments on commit 85e8015

Please sign in to comment.