Skip to content

Commit

Permalink
Fix Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 14, 2024
1 parent de2de20 commit 72b512c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,21 @@ Provides methods for styling text in the console:

### Cursor

Provides chainable methods for controlling the cursor in the console:
Provides methods for controlling the cursor in the console, use together with
`console.log`:

```js
console.log(Cursor.clearScreen());
console.log("Hello!");
```

Or to re-use the previous row

```js
console.log("Hello");
console.log(Cursor.up() + Cursor.clearLine() + "World!");
// - Hello will be instantly overwritten, leaving only "World!"
```

- **Cursor.up(lines?: number): Cursor** - Moves the cursor up (default: 1 line).
- **Cursor.down(lines?: number): Cursor** - Moves the cursor down (default: 1
Expand Down Expand Up @@ -92,7 +106,7 @@ const allArgs = args(true);
console.log(Colors.bold(Colors.bgGreen("Hello, world!")));

// Move the cursor and clear the line
Cursor.up(2).clearLine();
console.log(Cursor.up(2).clearLine());

// If an unknown argument is passed, exit with an error
if (!["hello", "goodbye"].includes(allArgs[1])) {
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/utils",
"version": "0.1.4",
"version": "0.1.5",
"exports": {
".": "./mod.ts",
"./ansi": "./utils/ansi.ts",
Expand Down
Empty file added test.ts
Empty file.
58 changes: 24 additions & 34 deletions utils/ansi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,77 +255,68 @@ export class Cursor {
/**
* Moves the cursor up a specified number of lines.
* @param {number} lines The number of lines to move up (default: 1).
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static up(lines: number = 1): Cursor {
console.log(`\x1b[${lines}A`);
return this;
static up(lines: number = 1): string {
return `\x1b[${lines}A`;
}

/**
* Moves the cursor down a specified number of lines.
* @param {number} lines The number of lines to move down (default: 1).
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static down(lines: number = 1): Cursor {
console.log(`\x1b[${lines}B`);
return this;
static down(lines: number = 1): string {
return `\x1b[${lines}B`;
}

/**
* Moves the cursor right a specified number of columns.
* @param {number} columns The number of columns to move right (default: 1).
* @returns {Cursor} Returns the current Cursor instance.
*/
static right(columns: number = 1): Cursor {
console.log(`\x1b[${columns}C`);
return this;
static right(columns: number = 1): string {
return `\x1b[${columns}C`;
}

/**
* Moves the cursor left a specified number of columns.
* @param {number} columns The number of columns to move left (default: 1).
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static left(columns: number = 1): Cursor {
console.log(`\x1b[${columns}D`);
return this;
static left(columns: number = 1): string {
return `\x1b[${columns}D`;
}

/**
* Hides the cursor.
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static hide(): Cursor {
console.log("\x1b[?25l");
return this;
static hide(): string {
return "\x1b[?25l";
}

/**
* Shows the cursor.
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static show(): Cursor {
console.log("\x1b[?25h");
return this;
static show(): string {
return "\x1b[?25h";
}

/**
* Clears the entire screen.
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static clearScreen(): Cursor {
console.log("\x1b[2J\x1b[0;0H");
return this;
static clearScreen(): string {
return "\x1b[2J\x1b[0;0H";
}

/**
* Clears the current line.
* @returns {Cursor} Returns the current Cursor instance.
* @returns {string} Returns a string with the control character.
*/
static clearLine(): Cursor {
console.log("\x1b[K");
return this;
static clearLine(): string {
return "\x1b[K";
}

/**
Expand All @@ -335,8 +326,7 @@ export class Cursor {
* @returns {Cursor} Returns the current Cursor instance.
*/
static moveTo(x: number, y: number): Cursor {
console.log(`\x1b[${y + 1};${x + 1}H`);
return this;
return `\x1b[${y + 1};${x + 1}H`;
}
}

Expand Down

0 comments on commit 72b512c

Please sign in to comment.