Skip to content

Commit 46ea6ad

Browse files
committed
Feat: added colorized general metta output formatter
1 parent 59ff910 commit 46ea6ad

File tree

7 files changed

+173
-14
lines changed

7 files changed

+173
-14
lines changed

metta-run/2024-08-16.log

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
Start time: 2024-08-16 16-08-1723816322
3+
Elapsed time: 0.141
4+
[(Hello World!)]
5+
6+
Start time: 2024-08-16 16-08-1723816322
7+
Elapsed time: 0.132
8+
[(TreeNode (Value Nil False ROOT) Nil (Cons NilNode (Cons (TreeNode (Value Nil True OR) Nil (Cons (TreeNode (Value a False LITERAL) Nil Nil) (Cons (TreeNode (Value Nil True AND) Nil (Cons (TreeNode (Value b False LITERAL) Nil Nil) (Cons (TreeNode (Value c False LITERAL) Nil Nil) Nil))) Nil))) Nil)))]
9+
10+
Start time: 2024-08-16 16-08-1723816322
11+
Elapsed time: 0.147
12+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil Nil) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
13+
14+
Start time: 2024-08-16 16-08-1723816322
15+
Elapsed time: 0.153
16+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil (Cons (TreeNode (Value F False LITERAL) Nil Nil) Nil)) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
17+
18+
Start time: 2024-08-16 16-08-1723816516
19+
Elapsed time: 0.080
20+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil (Cons (TreeNode (Value F False LITERAL) Nil Nil) Nil)) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
21+
22+
Start time: 2024-08-16 16-08-1723816516
23+
Elapsed time: 0.085
24+
[(Hello World!)]
25+
26+
Start time: 2024-08-16 16-08-1723816516
27+
Elapsed time: 0.089
28+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil Nil) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
29+
30+
Start time: 2024-08-16 16-08-1723816516
31+
Elapsed time: 0.096
32+
[(TreeNode (Value Nil False ROOT) Nil (Cons NilNode (Cons (TreeNode (Value Nil True OR) Nil (Cons (TreeNode (Value a False LITERAL) Nil Nil) (Cons (TreeNode (Value Nil True AND) Nil (Cons (TreeNode (Value b False LITERAL) Nil Nil) (Cons (TreeNode (Value c False LITERAL) Nil Nil) Nil))) Nil))) Nil)))]
33+
34+
Start time: 2024-08-16 17-08-1723816879
35+
Elapsed time: 0.156
36+
[(TreeNode (Value Nil False ROOT) Nil (Cons NilNode (Cons (TreeNode (Value Nil True OR) Nil (Cons (TreeNode (Value a False LITERAL) Nil Nil) (Cons (TreeNode (Value Nil True AND) Nil (Cons (TreeNode (Value b False LITERAL) Nil Nil) (Cons (TreeNode (Value c False LITERAL) Nil Nil) Nil))) Nil))) Nil)))]
37+
38+
Start time: 2024-08-16 17-08-1723816895
39+
Elapsed time: 0.070
40+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil Nil) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
41+
42+
Start time: 2024-08-16 17-08-1723816895
43+
Elapsed time: 0.071
44+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil (Cons (TreeNode (Value F False LITERAL) Nil Nil) Nil)) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
45+
46+
Start time: 2024-08-16 17-08-1723816895
47+
Elapsed time: 0.106
48+
[(TreeNode (Value Nil False ROOT) Nil (Cons NilNode (Cons (TreeNode (Value Nil True OR) Nil (Cons (TreeNode (Value a False LITERAL) Nil Nil) (Cons (TreeNode (Value Nil True AND) Nil (Cons (TreeNode (Value b False LITERAL) Nil Nil) (Cons (TreeNode (Value c False LITERAL) Nil Nil) Nil))) Nil))) Nil)))]
49+
50+
Start time: 2024-08-16 17-08-1723816895
51+
Elapsed time: 0.101
52+
[(Hello World!)]
53+
54+
Start time: 2024-08-16 17-08-1723816933
55+
Elapsed time: 0.085
56+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil (Cons (TreeNode (Value F False LITERAL) Nil Nil) Nil)) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
57+
58+
Start time: 2024-08-16 17-08-1723816933
59+
Elapsed time: 0.080
60+
[(Hello World!)]
61+
62+
Start time: 2024-08-16 17-08-1723816933
63+
Elapsed time: 0.084
64+
[(TreeNode (Value Nil False OR) Nil (Cons (TreeNode (Value A False LITERAL) Nil Nil) (Cons (TreeNode (Value B True LITERAL) Nil Nil) (Cons (TreeNode (Value C False LITERAL) Nil Nil) (Cons (TreeNode (Value D False LITERAL) Nil Nil) (Cons (TreeNode (Value E True LITERAL) Nil Nil) Nil))))))]
65+
66+
Start time: 2024-08-16 17-08-1723816986
67+
Elapsed time: 0.065
68+
Traceback (most recent call last):
69+
File "/home/zeamani/metta-bin/venv/bin/metta", line 8, in <module>
70+
sys.exit(main())
71+
File "/home/zeamani/metta-bin/venv/lib/python3.10/site-packages/hyperon/metta.py", line 35, in main
72+
for result in metta.run(program):
73+
File "/home/zeamani/metta-bin/venv/lib/python3.10/site-packages/hyperon/runner.py", line 210, in run
74+
raise RuntimeError(err_str)
75+
RuntimeError: Unexpected end of expression

metta-run/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ edition = "2021"
88
[dependencies]
99
chrono = "0.4.38"
1010
clap = { version = "4.5.13", features = ["derive"] }
11+
colored = "2.1.0"
12+
regex = "1.10.6"

metta-run/src/formatters/commands.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
use clap::Subcommand;
22

3-
use super::{binary_tree_formatter, constraint_tree_formatter};
3+
use super::{binary_tree_formatter, constraint_tree_formatter,output_formatter};
44

55
#[derive(Subcommand, Debug, Clone)]
66
pub enum FormatterCommands {
7+
78
#[command(name = "fbt", about = "Format binary tree")]
89
Fbt,
910

1011
#[command(name = "fct", about = "Format constraint tree")]
1112
Fct,
13+
#[command(name = "f", about = "Format metta output")]
14+
F,
1215
}
1316

14-
pub fn format(metta_output: String, command: FormatterCommands) {
17+
pub fn format(metta_output: (String , String), command: FormatterCommands) {
18+
let ( ref metta_output_stderr, ref metta_output_str) = metta_output;
19+
let metta_augmented =format!("{}{}", metta_output_stderr, metta_output_str);
1520
match command {
16-
FormatterCommands::Fbt => binary_tree_formatter::format(metta_output),
17-
FormatterCommands::Fct => constraint_tree_formatter::format(metta_output),
21+
FormatterCommands::F => output_formatter::format(metta_output),
22+
FormatterCommands::Fbt => binary_tree_formatter::format(metta_augmented),
23+
FormatterCommands::Fct => constraint_tree_formatter::format(metta_augmented),
1824
}
1925
}

metta-run/src/formatters/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod binary_tree_formatter;
22
pub mod commands;
33
pub mod constraint_tree_formatter;
4+
pub mod output_formatter;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// use crate::runners;
2+
use colored::Colorize;
3+
use regex::Regex;
4+
5+
pub fn format(metta_output: (String, String)) {
6+
let mut indent_level = 0;
7+
let (metta_err, metta_str) = metta_output;
8+
9+
// println!("this is working ++++++++++++++++++++++++++++++++ {}",);
10+
11+
if !metta_err.is_empty() {
12+
if let Some(last_line) = metta_err.lines().last() {
13+
println!("{}", last_line.red());
14+
}
15+
}
16+
if !metta_str.is_empty() {
17+
selective_print(split_outputs(metta_str))
18+
}
19+
}
20+
21+
fn selective_print(outputs: Vec<String>) {
22+
for output in outputs {
23+
let formatted = prettify(&output);
24+
let flag = "Metta-Output";
25+
if output.contains("(Error") {
26+
println!("{} [{}]",flag.dimmed().bold().blue(), formatted.red());
27+
} else {
28+
println!("{} [{}]",flag.dimmed().bold().blue(), formatted.green());
29+
}
30+
}
31+
}
32+
33+
fn split_outputs(output: String) -> Vec<String> {
34+
// Define the regex pattern to match substrings in the form of [ SOMETHING]
35+
let re = Regex::new(r"\[([^\]]+)\]").unwrap();
36+
37+
// Find all matches and collect them into a vector
38+
let matches: Vec<String> = re
39+
.captures_iter(&output)
40+
.map(|caps| caps.get(1).map_or("", |m| m.as_str()).to_string())
41+
.collect();
42+
43+
return matches;
44+
}
45+
46+
fn prettify(input: &str) -> String {
47+
let mut prettified = String::new();
48+
let mut indent_level = 0;
49+
50+
for line in input.chars() {
51+
match line {
52+
'(' => {
53+
prettified.push(line);
54+
prettified.push('\n');
55+
indent_level += 2;
56+
prettified.push_str(&" ".repeat(indent_level));
57+
}
58+
')' => {
59+
prettified.push('\n');
60+
indent_level -= 2;
61+
prettified.push_str(&" ".repeat(indent_level));
62+
prettified.push(line);
63+
}
64+
';' => {
65+
prettified.push('\n');
66+
prettified.push_str(&" ".repeat(indent_level));
67+
prettified.push(line);
68+
}
69+
' ' if prettified.ends_with('\n') => {
70+
prettified.push_str(&" ".repeat(indent_level));
71+
}
72+
_ => {
73+
prettified.push(line);
74+
}
75+
}
76+
}
77+
78+
prettified
79+
}

metta-run/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ fn main() -> io::Result<()> {
2727

2828
let start_time = tools::logger::start_timer();
2929
let metta_output = runners::metta::run(file);
30+
let metta_augmented = format!("{}{}", metta_output.0, metta_output.1);
3031

31-
let _ = tools::logger::stop_timer(start_time, &metta_output);
32+
let _ = tools::logger::stop_timer(start_time, &metta_augmented);
3233

3334
if let Some(command) = args.commands {
3435
match command {
3536
Commands::Format(command) => format(metta_output, command),
3637
}
3738
} else {
38-
println!("{}", metta_output);
39+
println!("{}", metta_augmented);
3940
}
4041
Ok(())
4142
}

metta-run/src/runners/metta.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::process::{Command, Stdio};
22
use std::{env, fs};
33

4-
pub fn run(file_path: String) -> String {
4+
pub fn run(file_path: String) -> (String, String) {
55
// cehck if the file exists
66
if !fs::metadata(&file_path).is_ok() {
77
eprintln!("File not found: {}", file_path);
@@ -25,10 +25,6 @@ pub fn run(file_path: String) -> String {
2525
let metta_output_str = String::from_utf8_lossy(&metta_output.stdout);
2626
let metta_output_stderr = String::from_utf8_lossy(&metta_output.stderr);
2727

28-
if !metta_output.status.success() {
29-
eprintln!("{}", metta_output_stderr);
30-
std::process::exit(1);
31-
}
3228

3329
// Deactivate the virtual environment
3430
Command::new("bash")
@@ -37,7 +33,6 @@ pub fn run(file_path: String) -> String {
3733
.output()
3834
.expect("Failed to deactivate virtual environment");
3935

40-
format!("{}{}", metta_output_stderr, metta_output_str)
41-
.trim()
42-
.to_string()
36+
// format!("{}{}", metta_output_stderr, metta_output_str)
37+
(metta_output_stderr.trim().to_string() , metta_output_str.trim().to_string())
4338
}

0 commit comments

Comments
 (0)