← Todos los ejemplos
Ejemplo 18
Close by prefix
close every chart whose symbol starts with a prefix (here
- Categoría
- Espacio de trabajo: lee o cambia gráficos y paneles
- Permisos
- workspace.readworkspace.write
- Antes de ejecutarlo
- a terminal window
- Qué hace
- CHANGES YOUR WORKSPACE — closes the charts and panels it
- Cómo ejecutarlo
- in TTT, open the T0 Editor, open this file, grant workspace.read
- Lenguaje
>=0.1.1, <0.2.0- Host
>=0.1.4, <0.2.0- Necesita
- workspace >=0.1.3, <0.2.0
Código
// 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(()); }