본문으로 건너뛰기
← 모든 예제

예제 18

Close by prefix

close every chart whose symbol starts with a prefix (here

분류
Workspace: 차트와 패널을 읽거나 변경
권한
workspace.readworkspace.write
실행하기 전에
a terminal window
하는 일
CHANGES YOUR WORKSPACE — closes the charts and panels it
실행 방법
in TTT, open the T0 Editor, open this file, grant workspace.read
언어
>=0.1.1, <0.2.0
호스트
>=0.1.4, <0.2.0
필요한 것
workspace >=0.1.3, <0.2.0

소스

// Purpose: close every chart whose symbol starts with a prefix (here
// BTCUSDT), and every other panel whose title does. Says what it closed
// and what it had to keep — the terminal always keeps one chart open.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: a terminal window
// Side effects: CHANGES YOUR WORKSPACE — closes the charts and panels it
// names, with whatever is drawn on them. Nothing is saved first.
// Run: in TTT, open the T0 Editor, open this file, grant workspace.read
// and workspace.write, Run. Change `prefix` to close another family.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.4, <0.2.0";
requires "workspace" ">=0.1.3, <0.2.0";
import std.text;

fn main() -> Result<Unit, Error> {
    let prefix = "BTCUSDT";
    let mut closed = 0;
    let mut kept = 0;

    // A chart's panel closes with it, so charts first.
    for c in workspace.charts() {
        if text.starts_with(c.symbol, prefix) {
            match workspace.close_chart(c.chart) {
                Ok(_) => {
                    closed = closed + 1;
                    print("closed the " + c.symbol + " " + c.timeframe + " chart");
                }
                Err(e) => {
                    kept = kept + 1;
                    print("kept the " + c.symbol + " " + c.timeframe + " chart: " + e.code);
                }
            }
        }
    }

    // What is left: the panels that are not charts. Read again, because
    // the closed charts took their panels with them.
    for p in workspace.panels() {
        if p.kind != "Chart" && text.starts_with(p.title, prefix) {
            match workspace.close_panel(p.panel) {
                Ok(_) => {
                    closed = closed + 1;
                    print("closed the panel " + p.title);
                }
                Err(e) => {
                    kept = kept + 1;
                    print("kept the panel " + p.title + ": " + e.code);
                }
            }
        }
    }

    print(to_string(closed) + " closed, " + to_string(kept) + " kept; " + to_string(workspace.charts().len()) + " chart(s) open now");
    return Ok(());
}