Skip to content

Commit

Permalink
feat!: Tui 2.0 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-Beast authored May 4, 2023
2 parents f8967e1 + 5d3fd82 commit 2c1bed9
Show file tree
Hide file tree
Showing 66 changed files with 4,043 additions and 3,771 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
deno run -A --no-check https://deno.land/x/[email protected]/src/cli.ts \
-i ./ "/.+\.ts/" \
-e "deps.ts" \
-l "// Copyright 2022 Im-Beast. All rights reserved. MIT license." \
-l "// Copyright 2023 Im-Beast. All rights reserved. MIT license." \
-p
- name: Push changes
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

## Copyright © 2021-2022 Im-Beast
## Copyright © 2021-2023 Im-Beast

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
Expand Down
76 changes: 45 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,33 @@ Simple [Deno](https://github.com/denoland/deno/) module that allows easy creatio

## 🖥️ OS Support

| Operating system | Linux | macOS | Windows¹<sup>,</sup | WSL |
| -------------------- | ----- | ----- | ----------------------- | ---- |
| Base | ✔️ | ✔️ | ✔️ | ✔️ |
| Keyboard support | ✔️ | ✔️ | ✔️ | ✔️ |
| Mouse support | ✔️ | ✔️ | | ✔️ |
| Required permissions | none | none | --unstable --allow-ffi³ | none |
| Operating system | Linux | macOS | Windows¹<sup>,</sup| WSL |
| -------------------- | ----- | ----- | --------------------- | ---- |
| Base | ✔️ | ✔️ | ✔️ | ✔️ |
| Keyboard support | ✔️ | ✔️ | ✔️ | ✔️ |
| Mouse support | ✔️ | ✔️ | ✔️ | ✔️ |
| Required permissions | none | none | none | none |

¹ - [WSL](https://docs.microsoft.com/en-us/windows/wsl/install) is a heavily recommended way to run Tui on Windows, if
you need to stick to clean Windows, please consider using [Windows Terminal](https://github.com/Microsoft/Terminal).
Windows without WSL is slower at writing to the console, so performance might be worse on it.

² - If unicode characters are displayed incorrectly type `chcp 65001` into the console to change active console code
page to use UTF-8 encoding.

³ - Related to [this issue](https://github.com/denoland/deno/issues/5945), in order to recognize all pressed keys
(including arrows etc.) on Windows Tui uses `C:\Windows\System32\msvcrt.dll` to read pressed keys via `_getch` function,
see code [here](./src/key_reader.ts?plain=1#L116).

## 🎓 Get started

#### Replace {version} with relevant module versions

1. Create Tui instance

```ts
import { crayon } from "https://deno.land/x/crayon@3.3.2/mod.ts";
import { crayon } from "https://deno.land/x/crayon@version/mod.ts";
import { Canvas, Tui } from "https://deno.land/x/tui@version/mod.ts";

const tui = new Tui({
style: crayon.bgBlue,
canvas: new Canvas({
refreshRate: 1000 / 60, // Run in 60FPS
stdout: Deno.stdout,
}),
style: crayon.bgBlack, // Make background black
refreshRate: 1000 / 60, // Run in 60FPS
});

tui.dispatch(); // Close Tui on CTRL+C
Expand All @@ -61,43 +57,61 @@ tui.dispatch(); // Close Tui on CTRL+C
2. Enable interaction using keyboard and mouse

```ts
import { handleKeyboardControls, handleKeypresses, handleMouseControls } from "https://deno.land/x/tui@version/mod.ts";

import { handleInput, handleKeyboardControls, handleMouseControls } from "https://deno.land/x/tui@version/mod.ts";
...

handleKeypresses(tui);
handleInput(tui);
handleMouseControls(tui);
handleKeyboardControls(tui);
```

3. Add some components

```ts
import { ButtonComponent } from "https://deno.land/x/tui@version/src/components/mod.ts";
import { Button } from "https://deno.land/x/tui@version/src/components/mod.ts";

...

let value = 0;
const button = new ButtonComponent({
tui,
// Create signal to make number automatically reactive
const number = new Signal(0);

const button = new Button({
parent: tui,
zIndex: 0,
label: {
text: new Computed(() => number.value.toString()), // cast number to string
},
theme: {
base: crayon.bgRed,
focused: crayon.bgLightRed,
active: crayon.bgYellow,
},
rectangle: {
column: 15,
row: 3,
column: 1,
row: 1,
height: 5,
width: 10,
},
label: String(value),
});

button.on("stateChange", () => {
if (button.state !== "active") return;
button.label = String(++value);
})
// Subscribe for button state changes
button.state.subscribe((state) => {
// If button is active (pressed) make number bigger by one
if (state === "active") {
++number.value;
}
});

// Listen to mousePress event
button.on("mousePress", ({ drag, movementX, movementY }) => {
if (!drag) return;

// Use peek() to get signal's value when it happens outside of Signal/Computed/Effect
const rectangle = button.rectangle.peek();
// Move button by how much mouse has moved while dragging it
rectangle.column += movementX;
rectangle.row += movementY;
});
```

4. Run Tui
Expand All @@ -115,7 +129,7 @@ tui.run();
<br /> Code should be well document and easy to follow what's going on.

This project follows [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) spec.
<br /> If your pull request's code could introduce understandability trouble, please add comments to it.
<br /> If your pull request's code can be hard to understand, please add comments to it.

## 📝 Licensing

Expand Down
8 changes: 2 additions & 6 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"tasks": {
"demo": "deno run --watch --unstable -A ./examples/demo.ts"
"demo": "deno run --watch --allow-hrtime ./examples/demo.ts"
},
"fmt": {
"options": {
"lineWidth": 120
}
},
"lint": {
"rules": {
"exclude": ["no-control-regex"]
}
}
"lock": false
}
9 changes: 0 additions & 9 deletions deno.lock

This file was deleted.

51 changes: 0 additions & 51 deletions examples/bouncing_box.ts

This file was deleted.

Loading

0 comments on commit 2c1bed9

Please sign in to comment.