Skip to content

Commit

Permalink
Add ansi colors and cursor control functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 14, 2024
1 parent 8b40c8f commit b809abe
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 32 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ jobs:
- name: Run linter
run: deno lint

# - name: Check types
# run: deno check mod.ts
- name: Check types
run: deno check mod.ts

- name: Run tests
run: deno test
89 changes: 61 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# @cross/utils

A collection of useful routines to simplify cross runtime (Node, Deno and Bun)
development.
A collection of useful routines to simplify cross-runtime (Node, Deno, and Bun) development.

Available for Node, Deno Bun and Browser at
[jsr.io/@cross/utils](https://jsr.io/@cross/utils), and works seamlessly with
both JavaScript and TypeScript.
Available for Node, Deno, Bun and Browser at [jsr.io/@cross/utils](https://jsr.io/@cross/utils), and works seamlessly with both JavaScript and TypeScript.

## Installation

Full instructions available at at [jsr.io](https://jsr.io/@cross/utils), short
version:
Full instructions available at [jsr.io](https://jsr.io/@cross/utils), short version:

```bash
# Deno
Expand All @@ -25,37 +21,74 @@ bunx jsr add @cross/utils

## Methods

### exit
* **exit(code?: number): void**
Terminates the current process with a provided exit code (defaults to 0).

Terminates the current process with a provided exit code.
* **args(all?: boolean): string[]**
Extracts command-line arguments in a cross-runtime compatible manner.
* `all` (optional): If true, includes the executable path and script name.

```js
import { exit } from "@cross/utils";
* **stripAnsi(text: string): string**
Removes all ANSI control codes from a given string.

console.log("Will show");
## Classes

exit(); // Exit with error code 0
### Colors

console.log("Will not show");
```
Provides methods for styling text in the console:

**Parameters**
* **Colors.bold(text: string): string** - Applies bold formatting.
* **Colors.dim(text: string): string** - Applies dim formatting.
* **Colors.italic(text: string): string** - Applies italic formatting.
* **Colors.underline(text: string): string** - Applies underline formatting.
* **Colors.inverse(text: string): string** - Applies inverse formatting.
* **Colors.hidden(text: string): string** - Applies hidden formatting.
* **Colors.strikethrough(text: string): string** - Applies strikethrough formatting.
* **Colors.rgb(r: number, g: number, b: number, text: string): string** - Sets the foreground color using 24-bit RGB values.
* **Colors.bgBlack(text: string): string** - Sets the background color to black.
* **Colors.bgRed(text: string): string** - Sets the background color to red.
* **Colors.bgGreen(text: string): string** - Sets the background color to green.
* **Colors.bgYellow(text: string): string** - Sets the background color to yellow.
* **Colors.bgBlue(text: string): string** - Sets the background color to blue.
* **Colors.bgMagenta(text: string): string** - Sets the background color to magenta.
* **Colors.bgCyan(text: string): string** - Sets the background color to cyan.
* **Colors.bgWhite(text: string): string** - Sets the background color to white.

- `code` (number, optional): The exit code for the process. Defaults to 0
(success).
### Cursor

### args
Provides chainable methods for controlling the cursor in the console:

Extracts command-line arguments in a cross-runtime compatible manner.
* **Cursor.up(lines?: number): Cursor** - Moves the cursor up (default: 1 line).
* **Cursor.down(lines?: number): Cursor** - Moves the cursor down (default: 1 line).
* **Cursor.right(columns?: number): Cursor** - Moves the cursor right (default: 1 column).
* **Cursor.left(columns?: number): Cursor** - Moves the cursor left (default: 1 column).
* **Cursor.hide(): Cursor** - Hides the cursor.
* **Cursor.show(): Cursor** - Shows the cursor.
* **Cursor.clearScreen(): Cursor** - Clears the entire screen.
* **Cursor.clearLine(): Cursor** - Clears the current line.
* **Cursor.moveTo(x: number, y: number): Cursor** - Moves the cursor to a specific position.

```js
import { args } from "@cross/utils";
## Example Usage

console.log("These are the arguments", args()); // Default behavior
console.log("All arguments (including executable and script)", args(true));
```
```javascript
import { exit, args, Colors, Cursor, stripAnsi } from "@cross/utils";

// Get all command-line arguments
const allArgs = args(true);

**Parameters**
// Color some output
console.log(Colors.bold(Colors.bgGreen("Hello, world!")));

- `all` (boolean, optional): When true, includes the executable path and script
name at the beginning of the returned array. Defaults to false.
// Move the cursor and clear the line
Cursor.up(2).clearLine();

// If an unknown argument is passed, exit with an error
if (!["hello", "goodbye"].includes(allArgs[1])) {
console.log(Colors.red("Invalid argument"));
exit(1);
}

// Remove ANSI codes from input for clean logging
const userInput = stripAnsi(await prompt("Enter some text: "));
console.log("You entered (without formatting):", userInput);
```
8 changes: 6 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "@cross/utils",
"version": "0.0.2",
"version": "0.1.3",
"exports": "./mod.ts",
"imports": { "@cross/runtime": "jsr:@cross/runtime@^0.0.16" }
"imports": {
"@cross/runtime": "jsr:@cross/runtime@^0.0.16",
"@cross/test": "jsr:@cross/test@^0.0.8",
"@std/assert": "jsr:@std/assert@^0.219.1"
}
}
10 changes: 10 additions & 0 deletions utils/ansi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";

import { stripAnsi } from "./ansi.ts";

test("Strip ansi characters", () => {
const text = "\x1b[31mThis is colored\x1b[0m and \x1b[2J\x1b[1;20Hsome more text";
const strippedText = stripAnsi(text);
assertEquals(strippedText, "This is colored and some more text");
});
Loading

0 comments on commit b809abe

Please sign in to comment.