Skip to main content

libutils_terminal/
mod.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> FEATURES
6#![feature(const_trait_impl)]
7#![feature(const_default)]
8#![feature(test)]
9#![feature(transmute_neo)]
10
11//> HEAD -> CRATES
12extern crate test;
13
14//> HEAD -> MODULES
15#[cfg(test)]
16mod benches;
17mod descriptor;
18mod metadata;
19mod problem;
20mod section;
21#[cfg(test)]
22mod tests;
23mod update;
24
25//> HEAD -> STD
26use std::{
27    env::args, 
28    fs::File, 
29    path::PathBuf, 
30    sync::LazyLock, 
31    time::Instant
32};
33
34//> HEAD -> CORE
35use core::fmt::{
36    Debug, 
37    Display
38};
39
40//> HEAD -> CAGELOCK
41use cagelock::Cage;
42
43//> HEAD -> UPDATE
44use update::Update;
45
46//> HEAD -> ISSUE
47use libutils_issue::{
48    Issue,
49    Severity
50};
51
52//> HEAD -> CONSOLE
53use libutils_console::{
54    Console,
55    Argument,
56    Update as UpdateTrait,
57    Descriptor as DescriptorTrait
58};
59
60//> HEAD -> SECTION
61use section::Section;
62
63//> HEAD -> PROBLEM
64use problem::Problem;
65
66//> HEAD -> DESCRIPTOR
67use descriptor::Descriptor;
68
69
70//^
71//^ TERMINAL
72//^
73
74//> TERMINAL -> DATA
75static ARGUMENTS: LazyLock<Vec<Argument>> = LazyLock::new(|| args().map(Argument::from).collect());
76static LAYOUT: Cage<Vec<Section>> = Cage::default();
77static OUTPUT: Cage<String> = Cage::default();
78
79//> TERMINAL -> STRUCT
80pub struct Terminal;
81
82//> TERMINAL -> IMPLEMENTATION
83impl Console for Terminal {
84    fn arguments() -> &'static [Argument] {return ARGUMENTS.as_slice()}
85    fn open(filename: &str) -> Result<impl DescriptorTrait, Issue> {
86        match File::open(PathBuf::from(filename)) {
87            Ok(file) => Ok(Descriptor {
88                file: file
89            }),
90            Err(error) => Err(Issue {
91                name: "Failed to open file",
92                description: Some(error.to_string()),
93                severity: Severity::Error
94            })
95        }
96    }
97    fn problem(issue: Issue, chain: &[&'static str]) -> impl UpdateTrait {
98        LAYOUT.write(|layout| layout.push(Section::Problem(Problem {
99            chain: Vec::from(chain),
100            issue: issue,
101            _at: Instant::now()
102        })));
103        return Update;
104    }
105    fn print(value: impl Display) -> impl UpdateTrait {
106        LAYOUT.write(|layout| layout.push(Section::Display(value.to_string())));
107        return Update;
108    }
109    fn debug(value: impl Debug) -> impl UpdateTrait {
110        LAYOUT.write(|layout| layout.push(Section::Debug(format!("{value:#?}"))));
111        return Update;
112    }
113    fn clear() -> impl UpdateTrait {
114        LAYOUT.write(Vec::clear);
115        return Update;
116    }
117}