Skip to content
← All examples

Example 13

Open chart

open a new chart in the terminal, at a chosen timeframe, and

Category
Workspace: reads or changes charts and panels
Permissions
workspace.readworkspace.write
Before you run it
a terminal window; at least one symbol on offer
What it does
CHANGES YOUR WORKSPACE — opens one chart panel (of the
How to run it
in TTT, open "T0 Lang", open this file, grant workspace.read and
Language
>=0.1.1, <0.2.0
Host
>=0.1.3, <0.2.0
Needs
workspace >=0.1.2, <0.2.0

Source

// Purpose: open a new chart in the terminal, at a chosen timeframe, and
// check it is really there; show that refused requests open nothing.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: a terminal window; at least one symbol on offer
// Side effects: CHANGES YOUR WORKSPACE — opens one chart panel (of the
// first symbol the terminal offers, at 15m) and puts it in focus.
// Run: in TTT, open "T0 Lang", open this file, grant workspace.read and
// workspace.write, Run.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.3, <0.2.0";
requires "workspace" ">=0.1.2, <0.2.0";

fn main() -> Result<Unit, Error> {
    // The rule: the first symbol the terminal offers. No symbol is assumed
    // to exist in every installation.
    let symbol = workspace.symbols().first().expect("the terminal offers no symbol to chart");
    let before = workspace.charts().len();

    match workspace.open_chart("NOT-A-SYMBOL", "15m") {
        Ok(_) => print("unexpected: an unknown symbol was charted"),
        Err(e) => print("refused as it should be: " + e.code),
    }
    match workspace.open_chart(symbol, "fortnight") {
        Ok(_) => print("unexpected: an unknown timeframe was accepted"),
        Err(e) => print("refused as it should be: " + e.code),
    }
    print("charts after the refusals: " + to_string(workspace.charts().len() - before) + " new");

    let opened = workspace.open_chart(symbol, "15m")?;
    // Not trusted from the answer alone: read back from the terminal.
    let now = workspace.chart(opened.chart)?;
    print("opened " + now.symbol + " " + now.timeframe + (if now.active { ", in focus" } else { ", NOT in focus" }));
    print("charts now: " + to_string(workspace.charts().len() - before) + " new");
    let mut shown = false;
    for p in workspace.panels() {
        match p.chart {
            Some(c) => {
                if c == opened.chart {
                    shown = true;
                }
            }
            None => {}
        }
    }
    print(if shown { "it has a panel of its own" } else { "it has NO panel" });
    return Ok(());
}