Skip to content

Commit

Permalink
feat: added predefined colors (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
golota60 committed Apr 9, 2021
1 parent 0da9160 commit 78a9125
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 32 deletions.
46 changes: 43 additions & 3 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,53 @@ import os from 'os';
import chalk from 'chalk';
import columnify from 'columnify';

export const errorMessage = 'Error - check https://www.npmjs.com/package/yayfetch for more';
export const errorMessage =
'Error - check https://www.npmjs.com/package/yayfetch or https://github.com/golota60/yayfetch for more';

export const bitstoMegabytes = (numberToConvert: number): number => numberToConvert * 9.537 * Math.pow(10, -7);

export const uptimeInMinutes = (): number => os.uptime() / 60;

export const returnInPink = (textToPrint: string): string => chalk.rgb(255, 102, 147)(textToPrint);
export const returnInPink = (text: string): string => chalk.rgb(255, 102, 147)(text);

export const returnInOrange = (text: string): string => chalk.rgb(255, 170, 16)(text);

export const returnInGreen = (text: string): string => chalk.green(text);

export const returnInWhite = (text: string): string => chalk.white(text);

export const returnInBlack = (text: string): string => chalk.black(text);

export const returnInRed = (text: string): string => chalk.red(text);

export const returnInBlue = (text: string): string => chalk.blue(text);

export const returnInYellow = (text: string): string => chalk.rgb(255, 245, 99)(text);

export const returnPredefinedColoredText = (text: string, colorCode: string): string => {
switch (colorCode) {
case 'pink':
return returnInPink(text);
case 'orange':
return returnInOrange(text);
case 'blue':
return returnInBlue(text);
case 'green':
return returnInGreen(text);
case 'white':
return returnInWhite(text);
case 'black':
return returnInBlack(text);
case 'red':
return returnInRed(text);
case 'blue':
return returnInBlue(text);
case 'yellow':
return returnInYellow(text);
default:
return returnInPink(text);
}
};

export const printInTwoColumns = (col1: string, col2: string): void => {
const data = [
Expand All @@ -23,7 +63,7 @@ export const printInTwoColumns = (col1: string, col2: string): void => {
config: { logo: { showHeaders: false }, specs: { showHeaders: false } },
}),
);
}
};

export const yayfetchASCII = `
████ ████████ ████
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PredefinedColors {
color: 'pink' | 'orange' | 'green' | 'white' | 'black' | 'red' | 'blue' | 'yellow';
}
87 changes: 58 additions & 29 deletions src/yayfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import os from 'os';
import yargs from 'yargs';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { uptimeInMinutes, returnInPink, yayfetchASCII, printInTwoColumns } from './helpers/helpers';
import {
uptimeInMinutes,
returnInPink,
yayfetchASCII,
printInTwoColumns,
returnPredefinedColoredText,
} from './helpers/helpers';
import {
getEndianness,
getDisplaysAndGraphicsCards,
Expand Down Expand Up @@ -52,57 +58,74 @@ const getSystemInformation = async (): Promise<SystemInformation> => ({
shellInfo: await getShellInfo(),
});

const allData = async (): Promise<string> => {
const allData = async (color: string): Promise<string> => {
const allData: SystemInformation = await getSystemInformation();
return ` ${returnInPink(allData.osInfo.username + '@' + os.platform())} \n -----------------------------\n
${returnInPink(`Platform:`)} ${os.platform().toLocaleUpperCase()}\n
${returnInPink(`Type:`)} ${os.type()}\n
${returnInPink(`Release:`)} ${os.release()}\n
${returnInPink(`Architecture:`)} ${os.arch()}\n
${returnInPink(`Uptime:`)} ${uptimeInMinutes().toFixed(0)} min\n
${returnInPink(`CPU:`)} ${os.cpus()[0].model}\n
${returnInPink(`GPU(s):`)} ${allData.graphicsInfo.gpuInfo}\n
${returnInPink(`Display(s):`)} ${allData.graphicsInfo.displays}\n
${returnInPink(`Endianness:`)} ${getEndianness()}\n
${returnInPink(`Memory:`)} ${allData.memoryInfo.free}/${allData.memoryInfo.used}/${
return ` ${returnPredefinedColoredText(
allData.osInfo.username + '@' + os.platform(),
color,
)} \n -----------------------------\n
${returnPredefinedColoredText(`Platform:`, color)} ${os.platform().toLocaleUpperCase()}\n
${returnPredefinedColoredText(`Type:`, color)} ${os.type()}\n
${returnPredefinedColoredText(`Release:`, color)} ${os.release()}\n
${returnPredefinedColoredText(`Architecture:`, color)} ${os.arch()}\n
${returnPredefinedColoredText(`Uptime:`, color)} ${uptimeInMinutes().toFixed(0)} min\n
${returnPredefinedColoredText(`CPU:`, color)} ${os.cpus()[0].model}\n
${returnPredefinedColoredText(`GPU(s):`, color)} ${allData.graphicsInfo.gpuInfo}\n
${returnPredefinedColoredText(`Display(s):`, color)} ${allData.graphicsInfo.displays}\n
${returnPredefinedColoredText(`Endianness:`, color)} ${getEndianness()}\n
${returnPredefinedColoredText(`Memory:`, color)} ${allData.memoryInfo.free}/${allData.memoryInfo.used}/${
allData.memoryInfo.total
} MiB (Free/Used/Total)\n
${returnInPink(`Shell:`)} ${allData.shellInfo}`;
${returnPredefinedColoredText(`Shell:`, color)} ${allData.shellInfo}`;
};

async function returnPickedData(valuesToDisplay: Array<string>): Promise<string> {
async function returnPickedData(valuesToDisplay: Array<string>, color: string): Promise<string> {
const allData: SystemInformation = await getSystemInformation();
const pickedVals = [`${chalk.blue(allData.osInfo.username + '@' + os.platform())} \n -----------------------------`];
const pickedVals = [
`${returnPredefinedColoredText(
allData.osInfo.username + '@' + os.platform(),
color,
)} \n -----------------------------`,
];
valuesToDisplay.includes('Platform') &&
pickedVals.push(`${returnInPink('Platform:')} ${os.platform().toLocaleUpperCase()}`);
valuesToDisplay.includes('Type') && pickedVals.push(`${returnInPink('Type:')} ${os.type()}`);
valuesToDisplay.includes('Release') && pickedVals.push(`${returnInPink('Release:')} ${os.release()}`);
valuesToDisplay.includes('Architecture') && pickedVals.push(`${returnInPink('Architecture:')} ${os.arch()}`);
pickedVals.push(`${returnPredefinedColoredText('Platform:', color)} ${os.platform().toLocaleUpperCase()}`);
valuesToDisplay.includes('Type') && pickedVals.push(`${returnPredefinedColoredText('Type:', color)} ${os.type()}`);
valuesToDisplay.includes('Release') &&
pickedVals.push(`${returnPredefinedColoredText('Release:', color)} ${os.release()}`);
valuesToDisplay.includes('Architecture') &&
pickedVals.push(`${returnPredefinedColoredText('Architecture:', color)} ${os.arch()}`);
valuesToDisplay.includes('Uptime') &&
pickedVals.push(`${returnInPink('Uptime:')} ${uptimeInMinutes().toFixed(0)} min`);
valuesToDisplay.includes('CPUs') && pickedVals.push(`${returnInPink('CPU:')} ${os.cpus()[0].model}`);
valuesToDisplay.includes('GPUs') && pickedVals.push(`${returnInPink('GPU(s):')} ${allData.graphicsInfo.gpuInfo}`);
pickedVals.push(`${returnPredefinedColoredText('Uptime:', color)} ${uptimeInMinutes().toFixed(0)} min`);
valuesToDisplay.includes('CPUs') &&
pickedVals.push(`${returnPredefinedColoredText('CPU:', color)} ${os.cpus()[0].model}`);
valuesToDisplay.includes('GPUs') &&
pickedVals.push(`${returnPredefinedColoredText('GPU(s):', color)} ${allData.graphicsInfo.gpuInfo}`);
valuesToDisplay.includes('Displays') &&
pickedVals.push(`${returnInPink('Display(s):')} ${allData.graphicsInfo.displays}`);
valuesToDisplay.includes('Endianness') && pickedVals.push(`${returnInPink('Endianness:')} ${getEndianness()}`);
pickedVals.push(`${returnPredefinedColoredText('Display(s):', color)} ${allData.graphicsInfo.displays}`);
valuesToDisplay.includes('Endianness') &&
pickedVals.push(`${returnPredefinedColoredText('Endianness:', color)} ${getEndianness()}`);
valuesToDisplay.includes('Memory') &&
pickedVals.push(
`${returnInPink('Memory:')} ${allData.memoryInfo.free}/${allData.memoryInfo.used}/${
`${returnPredefinedColoredText('Memory:', color)} ${allData.memoryInfo.free}/${allData.memoryInfo.used}/${
allData.memoryInfo.total
} MiB (Free/Used/Total)`,
);
valuesToDisplay.includes('Shell') && pickedVals.push(`${returnInPink('Shell:')} ${allData.shellInfo}`);
valuesToDisplay.includes('Shell') &&
pickedVals.push(`${returnPredefinedColoredText('Shell:', color)} ${allData.shellInfo}`);
return pickedVals.join('\n\n');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const args = yargs
.command('$0', '', async () => {
const customColor = String(yargs.argv.c || yargs.argv.color);
console.log('customcol', customColor, yargs.argv.color);
if (yargs.argv.p || yargs.argv.pick) {
const inquirerPrompt = await inquirer.prompt<{ displayedValues: Array<string> }>([promptQuestions]);
printInTwoColumns(returnInPink(yayfetchASCII), await returnPickedData(inquirerPrompt.displayedValues));
const pickedData = await returnPickedData(inquirerPrompt.displayedValues, customColor);
printInTwoColumns(returnPredefinedColoredText(yayfetchASCII, customColor), pickedData);
} else {
printInTwoColumns(returnInPink(yayfetchASCII), await allData());
printInTwoColumns(returnPredefinedColoredText(yayfetchASCII, customColor), await allData(customColor));
}
})
.scriptName('')
Expand All @@ -111,6 +134,12 @@ const args = yargs
alias: 'pick',
describe: 'Asks you which information you want displayed',
})
.option('c', {
alias: 'color',
describe: 'Color in which the data will be printed',
default: 'pink',
choices: ['pink', 'orange', 'green', 'white', 'black', 'red', 'blue', 'yellow'],
})
.help()
.version()
.alias('help', 'h')
Expand Down

0 comments on commit 78a9125

Please sign in to comment.