Skip to content

Commit

Permalink
add image support (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
golota60 authored Nov 20, 2021
1 parent 788338b commit 16795cb
Show file tree
Hide file tree
Showing 7 changed files with 680 additions and 15 deletions.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ Yayfetch is a tool similar to screenfetch, it just displays info about your comp

## Usage


`npx yayfetch@latest` - returns info about your system(`@latest` should be added, cause sometimes npx can display a cached version)

*or* install it globally:
_or_ install it globally:

`npm install -g yayfetch` and then just call `yayfetch`

### Flag-defined features
## Flag-defined features

`-p` or `--pick` - first asks you what information you want to display, then displays it

Expand All @@ -52,11 +51,11 @@ Yayfetch is a tool similar to screenfetch, it just displays info about your comp

`--config <path_to_file>` - specify a file path to a custom config. See [here](#example-config)

### Config-specific features
## Config-specific features

Some more advanced features are almost impossible to implement through flags(to be quite honest, some are already pushing it e.g. `--custom-lines`).

- Custom ASCIIs
- ### Custom ASCIIs

To customize the ASCIIs just define `"ascii"` line in the config. It should be an `Array<string>` with path(s) to the ASCII(s).

Expand All @@ -68,7 +67,34 @@ Example:
}
```

- Line Animations
- ### Custom images

You can also defined iamges instead of ASCIIs, by defining `images` field. Note that this flag is mutually exclusive with `ascii` flag.
Uses [terminal-image](https://github.com/sindresorhus/terminal-image) underneath, so refer to it when specifying `options`.

```ts
interface ImageOptions {
path: string;
options?: {
width?: string | number | undefined;
height?: string | number | undefined;
preserveAspectRatio?: boolean | undefined;
};
}
```

Example:

```json
{
"image": {
"path": "./path/to/file.img",
"options": { "preserveAspectRatio": false }
}
}
```

- ### Line Animations

Output can be animated by `line-animations` flag in the config file. It should be an `AnimationOptions` object.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"inquirer": "^7.0.0",
"log-update": "^4.0.0",
"systeminformation": "^5.6.4",
"terminal-image": "^1.2.1",
"yargs": "^14.2.0"
},
"devDependencies": {
Expand Down
37 changes: 30 additions & 7 deletions src/yayfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os from 'os';
import yargs from 'yargs';
import inquirer from 'inquirer';
import terminalImage from 'terminal-image';
import {
DisplayAndGraphicsCard,
MemoryInfoInterface,
Expand Down Expand Up @@ -237,6 +238,16 @@ yargs
const customLines: string | object = enhancedArgv['custom-lines'];
const animations: AnimationOptions = enhancedArgv['line-animations'];

interface ImageOptions {
path: string;
options?: {
width?: string | number | undefined;
height?: string | number | undefined;
preserveAspectRatio?: boolean | undefined;
};
}
const imageObj: ImageOptions = enhancedArgv['image'];

const customASCIIs: string[] = enhancedArgv['ascii'];

let customASCIIsParsed;
Expand All @@ -246,6 +257,11 @@ yargs
);
}

let image;
if (imageObj) {
image = await terminalImage.file(imageObj.path, imageObj.options);
}

/* Get device data */
let infoToPrint: string[];
if (enhancedArgv.p || enhancedArgv.pick) {
Expand Down Expand Up @@ -284,16 +300,23 @@ yargs
}

if (coloredBoxes && !animations) {
console.warn('Colored boxes will not show when using animations');
/* Empty string to ensure space between boxes */
/* Empty string to ensure space between text and boxes */
infoToPrint = [...infoToPrint, '', getColoredBoxes()];
}

const asciis = showLogo
? (customASCIIsParsed || [yayfetchASCII]).map((e) =>
normalizeASCII(e, 2)
)
: [];
const asciis =
(image && [
image
.split('\n')
.map((e) => `${e.trim()} `)
.join('\n')
.replace(/^\s*$(?:\r\n?|\n)/gm, ''),
]) ||
(showLogo
? (customASCIIsParsed || [yayfetchASCII]).map((e) =>
normalizeASCII(e, 2)
)
: []);
const joinedInfo = infoToPrint.join('\n');

if (animations) {
Expand Down
5 changes: 5 additions & 0 deletions tests/__tests__/image_testfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"image": {
"path": "./tests/images/doggy.jpg"
}
}
Binary file added tests/images/coffee.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/doggy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 16795cb

Please sign in to comment.