Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed Aug 25, 2020
0 parents commit b22b4fe
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
21 changes: 21 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: check

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Setup latest deno version
uses: denolib/setup-deno@v2
with:
deno-version: v1.x

- name: Run deno fmt
run: deno fmt --check

- name: Run deno lint
run: deno lint --unstable
20 changes: 20 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: release

on:
push:
tags:
- "*.*.*"
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# OS files
.DS_Store
.cache

# IDE
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-present the denosaurs team

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 rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# tty

[![Tags](https://img.shields.io/github/release/denosaurs/tty)](https://github.com/denosaurs/tty/releases)
[![CI Status](https://img.shields.io/github/workflow/status/denosaurs/tty/check)](https://github.com/denosaurs/tty/actions)
[![License](https://img.shields.io/github/license/denosaurs/tty)](https://github.com/denosaurs/tty/blob/master/LICENSE)

```typescript
import * as tty from "https://deno.land/x/tty/mod.ts";

await tty.hideCursor();

setInterval(() => {
tty.clearScreenSync();
tty.goHomeSync();
console.log("HELLO WORLD");
}, 200);

await tty.showCursor();
```

## other

### contribution

Pull request, issues and feedback are very welcome. Code style is formatted with deno fmt and commit messages are done following Conventional Commits spec.

### licence

Copyright 2020-present, the denosaurs team. All rights reserved. MIT license.
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { encode, decode } from "https://deno.land/std@0.66.0/encoding/utf8.ts";
11 changes: 11 additions & 0 deletions examples/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as tty from "../mod.ts";

await tty.hideCursor();

setInterval(() => {
tty.clearScreenSync();
tty.goHomeSync();
console.log("HELLO WORLD");
}, 200);

await tty.showCursor();
41 changes: 41 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { showCursorSync } from "./tty_sync.ts";

const mac =
(await Deno.permissions.query({ name: "env" })).state === "granted"
? Deno.env.get("TERM_PROGRAM") === "Apple_Terminal"
: false;

export const ESC = "\u001B[";

export const SAVE = mac ? "\u001B7" : ESC + "s";
export const RESTORE = mac ? "\u001B8" : ESC + "u";
export const POSITION = "6n";
export const HIDE = "?25l";
export const SHOW = "?25h";
export const SCROLL_UP = "T";
export const SCROLL_DOWN = "S";

export const UP = "A";
export const DOWN = "B";
export const RIGHT = "C";
export const LEFT = "D";

export const CLEAR_RIGHT = "0K";
export const CLEAR_LEFT = "1K";
export const CLEAR_LINE = "2K";

export const CLEAR_DOWN = "0J";
export const CLEAR_UP = "1J";
export const CLEAR_SCREEN = "2J";
export const CLEAR = "\u001Bc";

export const NEXT_LINE = "1E";
export const PREV_LINE = "1F";
export const COLUMN = "1G"; // left?
export const HOME = "H";

export type SyncStream = Deno.WriterSync;
export type AsyncStream = Deno.Writer;

export * from "./tty_async.ts";
export * from "./tty_sync.ts";
160 changes: 160 additions & 0 deletions tty_async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { encode } from "./deps.ts";

import {
AsyncStream,
RESTORE,
ESC,
POSITION,
HIDE,
SHOW,
SCROLL_UP,
SCROLL_DOWN,
CLEAR_UP,
CLEAR_DOWN,
CLEAR_LEFT,
CLEAR_RIGHT,
CLEAR_LINE,
CLEAR_SCREEN,
NEXT_LINE,
PREV_LINE,
HOME,
UP,
DOWN,
LEFT,
RIGHT,
} from "./mod.ts";

export async function write(str: string, writer: AsyncStream): Promise<void> {
await writer.write(encode(str));
}

export async function restore(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await write(RESTORE, writer);
}

export async function cursor(
action: string,
writer: AsyncStream = Deno.stdout
): Promise<void> {
await write(ESC + action, writer);
}

export async function position(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(POSITION, writer);
}

export async function hideCursor(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(HIDE, writer);
}

export async function showCursor(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(SHOW, writer);
}

export async function scrollUp(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(SCROLL_UP, writer);
}

export async function scrollDown(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(SCROLL_DOWN, writer);
}

export async function clearUp(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(CLEAR_UP, writer);
}

export async function clearDown(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(CLEAR_DOWN, writer);
}

export async function clearLeft(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(CLEAR_LEFT, writer);
}

export async function clearRight(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(CLEAR_RIGHT, writer);
}

export async function clearLine(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(CLEAR_LINE, writer);
}

export async function clearScreen(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(CLEAR_SCREEN, writer);
}

export async function nextLine(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(NEXT_LINE, writer);
}

export async function prevLine(
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(PREV_LINE, writer);
}

export async function goHome(writer: AsyncStream = Deno.stdout): Promise<void> {
await cursor(HOME, writer);
}

export async function goUp(
y = 1,
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(y + UP, writer);
}

export async function goDown(
y = 1,
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(y + DOWN, writer);
}

export async function goLeft(
x = 1,
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(x + LEFT, writer);
}

export async function goRight(
x = 1,
writer: AsyncStream = Deno.stdout
): Promise<void> {
await cursor(x + RIGHT, writer);
}

export async function goTo(
x: number,
y: number,
writer: AsyncStream = Deno.stdout
): Promise<void> {
await write(ESC + y + ";" + x + HOME, writer);
}

0 comments on commit b22b4fe

Please sign in to comment.