本文へ移動
← すべてのサンプル

サンプル 13

Open chart

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

カテゴリ
Workspace:チャートやパネルを読む・変更する
権限
workspace.readworkspace.write
実行する前に
a terminal window; at least one symbol on offer
何をするか
CHANGES YOUR WORKSPACE — opens one chart panel (of the
実行方法
in TTT, open "T0 Lang", open this file, grant workspace.read and
言語
>=0.1.1, <0.2.0
ホスト
>=0.1.3, <0.2.0
必要なもの
workspace >=0.1.2, <0.2.0

ソース

// 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(());
}