Skip to content

Commit

Permalink
Version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mjrlowe committed Nov 22, 2020
1 parent 0801bd9 commit 81c72ed
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Currently only PNG and JPG images are supported.
To install the CLI tool, run the following from the command line:

```shell
deno install --allow-read --allow-net --unstable --force https://x.nest.land/terminal_images@2.0.0/cli.ts
deno install --allow-read --allow-net --unstable --force https://x.nest.land/terminal_images@2.1.0/cli.ts
```

Then run
Expand Down Expand Up @@ -52,7 +52,7 @@ _Note that the size might be different, as by default it adapts to the size of y
Here is an example of how you can use it:

```ts
import { printImageString } from "https://x.nest.land/terminal_images@2.0.0/mod.ts";
import { printImageString } from "https://x.nest.land/terminal_images@2.1.0/mod.ts";

printImageString({

Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"entry": "./mod.ts",
"description": "A Deno module and CLI tool for displaying images in the terminal",
"homepage": "https://github.com/TheWizardBear/terminal_images",
"version": "2.0.0",
"version": "2.1.0",
"files": [
"./README.md",
"./mod.ts",
Expand Down
38 changes: 21 additions & 17 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface imageSettings {
/** The raw data of a PNG or JPG image */
rawFile?: Uint8Array;
/** The raw data for the image: the rgb(a) array as well as the height and width */
rawPixels?: {data: Uint8Array, width: number, height: number};
rawPixels?: { data: Uint8Array; width: number; height: number };
/** The character map to use when outputting the image */
characterMap?: string | string[];
/** The number of characters wide the output image is */
Expand Down Expand Up @@ -35,40 +35,39 @@ async function getImageString(settings: imageSettings): Promise<string> {
const color = settings.color ?? false;

let raw;
if(typeof settings.path !== "undefined"){

if (typeof settings.path !== "undefined") {
const path = settings.path;

//external file on the internet (requires --allow-net)
if (path.startsWith("https://") || path.startsWith("http://")) {
const response = await fetch(path);
raw = new Uint8Array(await response.arrayBuffer());

//local file (requires --allow-read)
//local file (requires --allow-read)
} else {
raw = await Deno.readFile(path);
}

}else if(typeof settings.rawFile !== "undefined"){
} else if (typeof settings.rawFile !== "undefined") {
raw = settings.rawFile;
}else if(typeof settings.rawPixels !== "undefined"){
} else if (typeof settings.rawPixels !== "undefined") {
raw = settings.rawPixels;
} else{
throw new Error("No file path or raw data specified.")
} else {
throw new Error("No file path or raw data specified.");
}

//@ts-ignore
let imageFileType = typeof settings.rawPixels !== "undefined" ? "raw" : getFileType(raw);
let imageFileType = typeof settings.rawPixels !== "undefined"
? "raw"
: getFileType(raw);
if (imageFileType === "unknown") {
if(settings.path){
if (settings.path) {
const fileExtension = settings.path.substr(
settings.path.lastIndexOf(".") + 1,
).toLowerCase();
throw new Error(`Image file type not supported. (${fileExtension})`);
}else{
throw new Error(`Image file type not supported.`)
} else {
throw new Error(`Image file type not supported.`);
}

}

const decodedImage = decodeImage(raw, imageFileType);
Expand All @@ -91,7 +90,9 @@ async function getImageString(settings: imageSettings): Promise<string> {

let outputString = "";
for (
let y = resolution; y < imagePixelHeight - resolution; y += resolution * 2
let y = resolution;
y < imagePixelHeight - resolution;
y += resolution * 2
) {
for (
let x: number = resolution / 2;
Expand Down Expand Up @@ -290,7 +291,10 @@ async function printImageString(settings: imageSettings): Promise<void> {
console.log(await getImageString(settings));
}

function decodeImage(raw: Uint8Array | {data: Uint8Array, width: number, height: number}, format: string) {
function decodeImage(
raw: Uint8Array | { data: Uint8Array; width: number; height: number },
format: string,
) {
let decodedImage;
switch (format) {
case "jpg":
Expand All @@ -302,7 +306,7 @@ function decodeImage(raw: Uint8Array | {data: Uint8Array, width: number, height:
//@ts-ignore
decodedImage = decodePng(raw);
break;

case "raw":
decodedImage = raw;
break;
Expand Down
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default "2.0.0";
export default "2.1.0";

0 comments on commit 81c72ed

Please sign in to comment.