Skip to content

Commit

Permalink
Merge pull request #17 from Knutakir/prettify-printing (fixes #15)
Browse files Browse the repository at this point in the history
Prettify printing 💃
  • Loading branch information
knutkirkhorn authored Apr 3, 2019
2 parents 7039c55 + 1a2ddd2 commit 11c2c58
Show file tree
Hide file tree
Showing 5 changed files with 2,767 additions and 2,514 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '8'
- '6'
- '4'
- '10'
- '8'
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Set a timeout that print the value every `seconds` seconds. The timeout restarts

### `--percentage`, `-p [h|d|w]`
Print the percentage change the last hour, day and week. If the flag is set to `h` then percentage change last hour is printed. It is the same for `d` and days, and `w` and week.
If the percentage is negative it is printed in bright red otherwise it is printed in green.

## Related
- [btc-value](https://github.com/Knutakir/btc-value) - API for this module
Expand Down
102 changes: 68 additions & 34 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const btcValue = require('btc-value');
const meow = require('meow');
const fs = require('fs');
const chalk = require('chalk');
const Ora = require('ora');
const spinner = new Ora();
const configFile = __dirname + '/config.json';
const config = require(configFile);
let defaultCurrency = config.default;
Expand Down Expand Up @@ -74,6 +77,7 @@ const cli = meow(`
function isValidCurrencyCode(currencyCode) {
currencyCode = currencyCode.toUpperCase();
let currency;

for (let i = 0; i < btcValue.currencies.length; i++) {
if (currencyCode === btcValue.currencies[i].code) {
currency = btcValue.currencies[i];
Expand All @@ -82,13 +86,37 @@ function isValidCurrencyCode(currencyCode) {
}

if (!currency) {
console.log('Please choose a valid currency code');
spinner.stop();
console.log(chalk.redBright('❌ Please choose a valid currency code'));
console.log('Type `btc-value -l` for a list of all valid currencies');
process.exit(1);
}

return currency;
}

// Helper functon for printing and stopping the spinner
function printOutput(input) {
spinner.stop();
console.log(input);
}

// Helper function for printing percentage in red and green
function printPercentage(percentage) {
if (percentage.startsWith('-')) {
printOutput(chalk.redBright(percentage));
} else {
printOutput(chalk.green(percentage));
}
}

// Helper function to print error and exit with code 1
function exitError(error) {
spinner.stop();
console.log(chalk.redBright(`❌ ${error}`));
process.exit(1);
}

// For calling all funtions every time in a timeout with `a` flag
function checkAllFlags() {
// If `s` flag is set => set currency as default
Expand All @@ -97,20 +125,19 @@ function checkAllFlags() {

const newConfig = JSON.stringify(
{
"default": {
"code": defaultCurrency.code,
"symbol": defaultCurrency.symbol
default: {
code: defaultCurrency.code,
symbol: defaultCurrency.symbol
},
"quantity": quantity,
"autorefresh": autorefresh
quantity: quantity,
autorefresh: autorefresh
}, null, 4);

fs.writeFile(configFile, newConfig, function(error) {
if (error) {
console.log('Something wrong happened, could not save new default currency.');
process.exit(1);
exitError('Something wrong happened, could not save new default currency.');
} else {
console.log('Default currency set to: ' + defaultCurrency.code + ' (' + defaultCurrency.symbol + ')');
console.log(chalk.green(`✔️ Default currency set to: ${defaultCurrency.code} (${defaultCurrency.symbol})`));
}
});
}
Expand All @@ -125,51 +152,54 @@ function checkAllFlags() {
quantity = cli.flags.q;
const newConfig = JSON.stringify(
{
"default": {
"code": defaultCurrency.code,
"symbol": defaultCurrency.symbol
default: {
code: defaultCurrency.code,
symbol: defaultCurrency.symbol
},
"quantity": quantity,
"autorefresh": autorefresh
quantity: quantity,
autorefresh: autorefresh
}, null, 4);

fs.writeFile(configFile, newConfig, function(error) {
if (error) {
console.log('Something wrong happened, could not save new quantity.');
process.exit(1);
exitError('Something wrong happened, could not save new quantity.');
} else {
console.log('Quantity set to: ' + quantity);
console.log(chalk.green(`✔️ Quantity set to: ${quantity}`));
console.log(`Value of ${quantity} BTC:`);
spinner.start();
}
});
}
} else {
console.log(`Value of ${quantity} BTC:`);
spinner.start();
}
console.log('Value of ' + quantity + ' BTC:');

multiplier = quantity;
}

// If `p` flag is set => print percentage change
if (cli.flags.p !== undefined) {
if (cli.flags.p == 'h') {
btcValue.getPercentageChangeLastHour().then(percentage => {
console.log(percentage + '%');
printPercentage(percentage + '%');
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
} else if (cli.flags.p == 'd' || cli.flags.p == '') {
btcValue.getPercentageChangeLastDay().then(percentage => {
console.log(percentage + '%');
printPercentage(percentage + '%');
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
} else if (cli.flags.p == 'w') {
btcValue.getPercentageChangeLastWeek().then(percentage => {
console.log(percentage + '%');
printPercentage(percentage + '%');
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
} else {
console.log('Invalid percentage input. Check `btc-value --help`.');
process.exit(1);
exitError('Invalid percentage input. Check `btc-value --help`.');
}
} else {
// If `d` flag is set => return value as double
Expand All @@ -181,29 +211,29 @@ function checkAllFlags() {

if (currency.code === 'USD') {
btcValue(cli.flags.d, multiplier).then(value => {
console.log(currency.symbol + value);
printOutput(currency.symbol + value);
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
} else {
btcValue.getConvertedValue(currency.code, cli.flags.d, multiplier).then(value => {
console.log(currency.symbol + value);
printOutput(currency.symbol + value);
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
}
} else {
if (defaultCurrency.code === 'USD') {
btcValue(cli.flags.d, multiplier).then(value => {
console.log(defaultCurrency.symbol + value);
printOutput(defaultCurrency.symbol + value);
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
} else {
btcValue.getConvertedValue(defaultCurrency.code, cli.flags.d, multiplier).then(value => {
console.log(defaultCurrency.symbol + value);
printOutput(defaultCurrency.symbol + value);
}).catch(() => {
console.log('Please check your internet connection');
exitError('Please check your internet connection');
});
}
}
Expand All @@ -214,13 +244,16 @@ function checkAllFlags() {
if (cli.flags.a !== true) {
autorefresh = cli.flags.a;
}

autorefreshTimer = setTimeout(checkAllFlags, autorefresh * 1000);
spinner.start();
}
}

// If `l` flag is set => print list of supported currency codes
if (cli.flags.l) {
let currencyOutprint = ' List of all supported currency codes:';

for (let i = 0; i < btcValue.currencies.length; i++) {
// To seperate the currency codes on different lines
if (i % 9 === 0) {
Expand All @@ -232,6 +265,7 @@ if (cli.flags.l) {
currencyOutprint += ', ';
}
}

console.log(currencyOutprint);
process.exit(0);
}
Expand Down
Loading

0 comments on commit 11c2c58

Please sign in to comment.