Skip to content

Commit 5f9de2b

Browse files
authored
Merge pull request #12 from Amanuel-1/main
Colorized General MeTTa-run formatter
2 parents f0b6a33 + 860ff44 commit 5f9de2b

File tree

8 files changed

+103
-17
lines changed

8 files changed

+103
-17
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ After running the setup script, you can start using Metta directly from the term
3737
```bash
3838
metta-run example.metta fct
3939
```
40+
- To format any output you get from metta-run:
41+
42+
```bash
43+
metta-run example.metta f
44+
```
4045

4146
- To run Metta without automatically activating the Python environment (you need to activate it manually first):
4247

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use clap::Subcommand;
22

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

55
#[derive(Subcommand, Debug, Clone)]
66
pub enum FormatterCommands {
7+
#[command(name = "f", about = "Format metta outputs")]
8+
F,
79
#[command(name = "fbt", about = "Format binary tree")]
810
Fbt,
911

@@ -14,10 +16,13 @@ pub enum FormatterCommands {
1416
Fgt,
1517
}
1618

17-
pub fn format(metta_output: String, command: FormatterCommands) {
19+
pub fn format(metta_output: (String, String), command: FormatterCommands) {
20+
let (ref metta_output_stderr, ref metta_output_str) = metta_output;
21+
let metta_augmented = format!("{}{}", metta_output_stderr, metta_output_str);
1822
match command {
19-
FormatterCommands::Fbt => binary_tree_formatter::format(metta_output),
20-
FormatterCommands::Fct => constraint_tree_formatter::format(metta_output),
21-
FormatterCommands::Fgt => guardset_tree_formatter::format(metta_output),
23+
FormatterCommands::F => output_formater::format(metta_output),
24+
FormatterCommands::Fbt => binary_tree_formatter::format(metta_augmented),
25+
FormatterCommands::Fct => constraint_tree_formatter::format(metta_augmented),
26+
FormatterCommands::Fgt => guardset_tree_formatter::format(metta_augmented),
2227
}
2328
}

metta-run/src/formatters/mod.rs

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

metta-run/src/main.rs

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

2828
let start_time = tools::logger::start_timer();
2929
let metta_output = runners::metta::run(file);
30+
let (ref metta_output_stderr,ref metta_output_str) = metta_output;
31+
let metta_augmented_output = format!("{}{}",metta_output_stderr,metta_output_str);
3032

31-
let _ = tools::logger::stop_timer(start_time, &metta_output);
33+
let _ = tools::logger::stop_timer(start_time, &metta_augmented_output);
3234

3335
if let Some(command) = args.commands {
3436
match command {
3537
Commands::Format(command) => format(metta_output, command),
3638
}
3739
} else {
38-
println!("{}", metta_output);
40+
println!("{}", metta_augmented_output);
3941
}
4042
Ok(())
4143
}

metta-run/src/runners/metta.rs

Lines changed: 3 additions & 9 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,19 +25,13 @@ 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-
}
32-
3328
// Deactivate the virtual environment
3429
Command::new("bash")
3530
.arg("-c")
3631
.arg("deactivate")
3732
.output()
3833
.expect("Failed to deactivate virtual environment");
3934

40-
format!("{}{}", metta_output_stderr, metta_output_str)
41-
.trim()
42-
.to_string()
35+
( metta_output_stderr.trim().to_string(), metta_output_str.trim().to_string())
36+
4337
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
!(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)))
2-

0 commit comments

Comments
 (0)