all repos — kokyo @ d3e94c1b0d41b8bf8530ecb6d38c41166dc8f01f

Chatbot and CLI tool for Swiss public transports

cli.ts (view raw)

 1import { Command } from "@cliffy/command";
 2import { Input, Select } from "@cliffy/prompt";
 3import { colors } from "@cliffy/ansi/colors";
 4import { Table } from "@cliffy/table";
 5import { findStopByName } from "./api/locationInformationRequest.ts";
 6import { getNextDepartures } from "./api/stopEvent.ts";
 7
 8await new Command()
 9  .name("tccli")
10  .option("-s, --stop <name>", "Nom de l'arrêt")
11  .action(async options => {
12    let stopInput = options.stop;
13    if (!stopInput)
14      stopInput = await Input.prompt({
15        message: "Nom de l'arrêt",
16      });
17
18    const stopLists = await findStopByName(stopInput);
19    const stopRef = await Select.prompt({
20      message: "Sélectionner le stop",
21      options: stopLists.map(item => ({
22        name: item.name,
23        value: item.stopRef,
24      })),
25    });
26    const nextDepartures = await getNextDepartures(stopRef);
27
28    const tableRows = nextDepartures.map(item => [
29      item.departure,
30      `${item.departureIn} min`,
31      item.serviceName,
32      item.serviceType,
33      item.to,
34    ]);
35    const table: Table = new Table()
36      .header(
37        ["Départ", "Dans", "Ligne", "Type", "Direction"].map(colors.bold.blue)
38      )
39      .body(tableRows)
40      .columns([
41        { minWidth: 10 },
42        { minWidth: 10 },
43        { minWidth: 10 },
44        { minWidth: 10 },
45      ]);
46    console.log("");
47    table.render();
48  })
49  .parse();