|
1 | | -import DraftLog from "draftlog"; |
2 | | -import chalk from "chalk"; |
3 | | -import chalkTable from "chalk-table"; |
4 | | -import readline from "readline"; |
5 | 1 | import database from "./../database.json" assert { type: "json" }; |
6 | 2 | import Person from "./persons.js"; |
| 3 | +import { save } from "./repository.js"; |
| 4 | +import TerminalController from "./terminal.controller.js"; |
7 | 5 |
|
8 | | -DraftLog(console).addLineListener(process.stdin); |
| 6 | +const DEFAULT_LANG = "pt-BR"; |
| 7 | +const STOP_TERM = ":q"; |
9 | 8 |
|
10 | | -const options = { |
11 | | - leftPad: 2, |
12 | | - columns: [ |
13 | | - { field: "id", name: chalk.cyan("ID") }, |
14 | | - { field: "vehicles", name: chalk.magenta("vehicles") }, |
15 | | - { field: "kmTraveled", name: chalk.green("km Traveled") }, |
16 | | - { field: "from", name: chalk.cyan("From") }, |
17 | | - { field: "to", name: chalk.cyan("To") }, |
18 | | - ], |
19 | | -}; |
| 9 | +const terminalController = new TerminalController(); |
| 10 | +terminalController.initializeTerminal(database, DEFAULT_LANG); |
20 | 11 |
|
21 | | -const table = chalkTable( |
22 | | - options, |
23 | | - database.map((element) => new Person(element).formatted()) |
24 | | -); |
25 | | -const print = console.draft(table); |
| 12 | +async function mainLoop() { |
| 13 | + try { |
| 14 | + const answer = await terminalController.question(); |
| 15 | + if (answer === STOP_TERM) { |
| 16 | + terminalController.closeTerminal(); |
| 17 | + console.log("process finished!"); |
| 18 | + return; |
| 19 | + } |
| 20 | + const person = Person.generateInstanceFromString(answer); |
| 21 | + terminalController.updateTable(person.formatted(DEFAULT_LANG)); |
| 22 | + await save(person); |
26 | 23 |
|
27 | | -const terminal = readline.createInterface({ |
28 | | - input: process.stdin, |
29 | | - output: process.stdout, |
30 | | -}); |
| 24 | + return mainLoop(); |
| 25 | + } catch (error) { |
| 26 | + console.error("DEU RUIM**", error); |
| 27 | + return mainLoop(); |
| 28 | + } |
| 29 | +} |
31 | 30 |
|
32 | | -// terminal.question("Qual é o seu nome?", (msg) => { |
33 | | -// console.log("msg", msg.toString()); |
34 | | -// }); |
| 31 | +await mainLoop(); |
0 commit comments