Skip to content

Latest commit

 

History

History
122 lines (92 loc) · 2.92 KB

sdk.md

File metadata and controls

122 lines (92 loc) · 2.92 KB

Snail SDK

Snail is a SDK for displaying rich content in the Snail Terminal.

Install Snail and SDK

  1. Download the Snail Terminal
  2. Install Snail SDK into your project

Node

npm i snail-sdk

Python

pip install snail-sdk

Display some web content

The Snail SDK can display web content inline in the snail terminal. First create a .js file

web.js

document.body.textContent = 'Hello World!';
snail.setHeight(document.body.offsetHeight);

Then use the sdk to display the .js file

Node

const { display } = require('snail-sdk');
display(require.resolve('./web.js'));

Python

from pathlib import Path
from snail_sdk import display
display(Path(__file__).parent / "web.js")

Send a message to web content

web.js

const data = await snail.waitForMessage();
document.body.textContent = `${data.greeting} ${data.name}!`;
snail.setHeight(document.body.offsetHeight);

Then use the sdk to display the .js file and send it a message.

Node

const { display, send } = require('snail-sdk');
display(require.resolve('./web.js'));
send({ name: 'Joel', greeting: 'Hey' });

Python

from pathlib import Path
from snail_sdk import display, send
display(Path(__file__).parent / "web.js")
send({ 'name': 'Joel', 'greeting': 'Hey' })

Progress Bar

Snail's progress bars won't distrupt other text from your program. They stick in place no matter how many times they are updated. And you can see their status in the app icon.

Node

const { setProgress } = require('snail-sdk');
setProgress({ progress: 0.3, leftText: 'left', rightText: 'right'});

Python

from snail_sdk import set_progress
set_progress(0.3, left_text="left", right_text="right")

Charts

Charts will appear inline in the terminal and update in real time.

Node

const { chart } = require('snail-sdk');
for (let i = 0; i < 100; i++)
  chart({ foo: Math.sin(i/10) });

Python

from snail_sdk import chart
import math
for i in range(100):
    chart({ 'foo': math.sin(i/10) })

Web API

When displaying web content, there is a global snail object which communicates with the terminal.

snail.waitForMessage(): Promise;

Waits for any returns any value sent from the send method in the sdk.

snail.setHeight(height: number): void;

Sets the height of the web content. This must be called with a non-zero value in order for the web content be visible when not fullscreen.

snail.setIsFullscreen(isFullscreen: boolean): void;

Set's the web content to take up the whole terminal screen. Fullscreen content will automatically close if the program which displayed that content closes.

snail.sendInput(input: string): void;

Sends some data over stdin which could be read by the controlling program.

snail.close(): void;

Remove the web content from the terminal