Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
tbjgolden committed Apr 19, 2022
1 parent bef48fc commit 128dc5a
Show file tree
Hide file tree
Showing 7 changed files with 1,970 additions and 204 deletions.
60 changes: 39 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
# Use case
# term-kitty-img

**OSS lib development**
Show images in your kitty terminal.

- npm package
- supports node-browser shared env out of box
- cli
- boilerplate to wire a cli to a lib
- web
- this is for use during development
- supports live reload
(Based on [npm:term-img](https://github.com/sindresorhus/term-img) but for kitty protocol instead of iTerm2)

This is not a [node app starter](https://github.com/mrwade/ultimate-node-stack), a [web app starter](https://github.com/withastro/astro), or a [hybrid starter](https://github.com/vercel/next.js/).
![term-kitty-img in use](./example.png)

# Init
# Install

```sh
# The usual
git clone https://github.com/tbjgolden/just-build.git <dir>
cd <dir>
npm install
# One time init function to convert template to new project
node _scripts/init.js
npm install term-kitty-img
```

# Key data
# Usage

```js
import { terminalKittyImage } from "term-kitty-img";

function fallback() {
// Return something else when not supported
}

console.log(
terminalKittyImage("unicorn.jpg", {
width: 800, // default bounding box of 600px wide
height: 150, // default bounding box of 600px high
// note: dimensions are in pixels (cannot use percentages or cells)
preserveAspectRatio: false, // default true
// note:
// (when false, width and height are exact, image stretches)
// (when true, width and height act as a contain box, image shrinks to fit)
fallback: () => {
console.error("Could not load unicorn");
},
})
);
```

Dev environment requires:
## Notes

- node 10+
- npm >= 5.2.0
The kitty protocol is very awkward to use with Node. I was lucky to find a way to make it
work by using spawn with stdin:inherit, if someone has a better way to solve this lmk!

---

Output code is ES6 and targets:

- node 10+
- All major non-dead browsers (>93%)

---

MIT
61 changes: 55 additions & 6 deletions cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
#!/usr/bin/env node
#!/usr/bin/env node --no-warnings

import { hello } from "term-kitty-img";
import { terminalKittyImage } from "term-kitty-img";
import { Command } from "commander";
import fs from "node:fs";
const program = new Command();

const [_cmd, _fileName, ...args] = process.argv;
const arg = args.join(" ");
program
.name("term-kitty-img")
.description("CLI to print an image in Kitty terminals")
.argument("<string>", "string to split")
.option("-s <number>", "simple (width=n, height=n, preserve)")
.option("--width <number>", "set image width")
.option("--height <number>", "set image height")
.option("--stretch", "disable aspect-ratio preservation");

if (arg !== "") {
console.log(hello(arg));
program.parse();

const opts = program.opts();
const out: any = {
callback: () => {
console.error("Environment does not support kitty images");
process.exit(1);
},
};
if (opts.s !== undefined) {
if (Number.isNaN(parseInt(opts.s))) {
throw new Error("size must be a number");
} else {
const size = parseInt(opts.s);
out.width = size;
out.height = size;
out.stretch = false;
}
} else {
if (opts.width !== undefined) {
if (Number.isNaN(parseInt(opts.width))) {
throw new Error("width must be a number");
} else {
out.width = parseInt(opts.width);
}
}
if (opts.height !== undefined) {
if (Number.isNaN(parseInt(opts.height))) {
throw new Error("height must be a number");
} else {
out.height = parseInt(opts.height);
}
}
if (opts.stretch) {
out.preserveAspectRatio = !opts.stretch;
}
}
const filePath = program.args[0];
if (!fs.existsSync(filePath)) {
throw new Error("file does not exist");
}

terminalKittyImage(filePath, out);
Binary file added example.png
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 128dc5a

Please sign in to comment.