Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better list command #248

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion book/src/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ There is a set of special commands that only work in interactive mode:

| Command | Action |
|---------|--------|
| `list`, `ll`, `ls` | List all constants, units, and dimensions |
| `list`, `ls` | List all constants, units, and dimensions |
| `clear` | Clear screen |
| `help`, `?` | View short help text |
| `quit`, `exit` | Quit the session |
Expand Down
2 changes: 1 addition & 1 deletion book/src/web-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ There is a set of special commands that only work in the web version:

| Command | Action |
|---------|--------|
| `list`, `ll`, `ls` | List all constants, units, and dimensions |
| `list`, `ls` | List all constants, units, and dimensions |
| `help`, `?` | View short help text |
| `reset` | Reset state (clear constants, functions, units, …) |
| `clear` | Clear screen |
Expand Down
1 change: 1 addition & 0 deletions numbat-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ colored = "2"
itertools = "0.11"
toml = { version = "0.8.6", features = ["parse"] }
serde = { version = "1.0.190", features = ["derive"] }
terminal_size = "0.3.0"

[dependencies.clap]
version = "4"
Expand Down
1 change: 1 addition & 0 deletions numbat-cli/src/ansi_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Formatter for ANSIFormatter {
) -> String {
(match format_type {
FormatType::Whitespace => text.normal(),
FormatType::Emphasized => text.bold(),
FormatType::Dimmed => text.dimmed(),
FormatType::Text => text.normal(),
FormatType::String => text.green(),
Expand Down
21 changes: 21 additions & 0 deletions numbat-cli/src/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ impl Completer for NumbatCompleter {
.filter(|p| p.replacement.starts_with(line))
.collect(),
));
} else if line.starts_with("list ") || line.starts_with("ls ") {
let command = if line.starts_with("list ") {
"list"
} else {
"ls"
};

return Ok((
0,
["functions", "dimensions", "units", "variables"]
.iter()
.map(|category| {
let line = format!("{command} {category}");
Pair {
display: category.to_string(),
replacement: line,
}
})
.filter(|p| p.replacement.starts_with(line))
.collect(),
));
}

let (pos_word, word_part) = extract_word(line, pos, None, |c| {
Expand Down
50 changes: 45 additions & 5 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ impl Cli {
let mut context = Context::new(importer);
context.set_debug(args.debug);

context.set_terminal_width(
terminal_size::terminal_size().map(|(terminal_size::Width(w), _)| w as usize),
);

Ok(Self {
context: Arc::new(Mutex::new(context)),
config,
Expand Down Expand Up @@ -309,11 +313,47 @@ impl Cli {
rl.add_history_entry(&line)?;

match line.trim() {
"list" | "ls" | "ll" => {
let ctx = self.context.lock().unwrap();

let markup = ctx.print_environment();
println!("{}", ansi_format(&markup, false));
"list" | "ls" => {
println!(
"{}",
ansi_format(
&self.context.lock().unwrap().print_environment(),
false
)
);
}
"list functions" | "ls functions" => {
println!(
"{}",
ansi_format(
&self.context.lock().unwrap().print_functions(),
false
)
);
}
"list dimensions" | "ls dimensions" => {
println!(
"{}",
ansi_format(
&self.context.lock().unwrap().print_dimensions(),
false
)
);
}
"list variables" | "ls variables" => {
println!(
"{}",
ansi_format(
&self.context.lock().unwrap().print_variables(),
false
)
);
}
"list units" | "ls units" => {
println!(
"{}",
ansi_format(&self.context.lock().unwrap().print_units(), false)
);
}
"clear" => {
rl.clear_screen()?;
Expand Down
18 changes: 18 additions & 0 deletions numbat-wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions numbat-wasm/src/jquery_terminal_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Formatter for JqueryTerminalFormatter {
) -> String {
let css_class = match format_type {
FormatType::Whitespace => None,
FormatType::Emphasized => Some("emphasized"),
FormatType::Dimmed => Some("dimmed"),
FormatType::Text => None,
FormatType::String => Some("string"),
Expand Down
38 changes: 27 additions & 11 deletions numbat-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Numbat {
pub fn new() -> Self {
let mut ctx = Context::new(BuiltinModuleImporter::default());
let _ = ctx.interpret("use prelude", CodeSource::Internal).unwrap();
ctx.set_terminal_width(Some(84)); // terminal width with current layout
Numbat { ctx }
}

Expand All @@ -64,13 +65,16 @@ impl Numbat {
.unwrap();
}

fn jq_format(&self, markup: &numbat::markup::Markup, indent: bool) -> String {
let fmt = JqueryTerminalFormatter {};
fmt.format(&markup, indent).into()
}

pub fn interpret(&mut self, code: &str) -> InterpreterOutput {
let mut output = String::new();

let registry = self.ctx.dimension_registry().clone();

let fmt = JqueryTerminalFormatter {};

let to_be_printed: Arc<Mutex<Vec<m::Markup>>> = Arc::new(Mutex::new(vec![]));
let to_be_printed_c = to_be_printed.clone();
let mut settings = InterpreterSettings {
Expand All @@ -87,20 +91,20 @@ impl Numbat {
// Pretty print
output.push_str("\n");
for statement in &statements {
output.push_str(&fmt.format(&statement.pretty_print(), true));
output.push_str(&self.jq_format(&statement.pretty_print(), true));
output.push_str("\n");
}
output.push_str("\n");

// print(…) and type(…) results
let to_be_printed = to_be_printed.lock().unwrap();
for content in to_be_printed.iter() {
output.push_str(&fmt.format(content, true));
output.push_str(&self.jq_format(content, true));
output.push_str("\n");
}

let result_markup = result.to_markup(statements.last(), &registry, true);
output.push_str(&fmt.format(&result_markup, true));
output.push_str(&self.jq_format(&result_markup, true));

InterpreterOutput {
output,
Expand All @@ -118,15 +122,27 @@ impl Numbat {
}

pub fn print_environment(&self) -> JsValue {
let markup = self.ctx.print_environment();
let fmt = JqueryTerminalFormatter {};
fmt.format(&markup, false).into()
self.jq_format(&self.ctx.print_environment(), false).into()
}

pub fn print_functions(&self) -> JsValue {
self.jq_format(&self.ctx.print_functions(), false).into()
}

pub fn print_dimensions(&self) -> JsValue {
self.jq_format(&self.ctx.print_dimensions(), false).into()
}

pub fn print_variables(&self) -> JsValue {
self.jq_format(&self.ctx.print_variables(), false).into()
}

pub fn print_units(&self) -> JsValue {
self.jq_format(&self.ctx.print_units(), false).into()
}

pub fn help(&self) -> JsValue {
let markup = help_markup();
let fmt = JqueryTerminalFormatter {};
fmt.format(&markup, true).into()
self.jq_format(&help_markup(), true).into()
}

pub fn get_completions_for(&self, input: &str) -> Vec<JsValue> {
Expand Down
10 changes: 9 additions & 1 deletion numbat-wasm/www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ function interpret(input) {
combined_input = "";
updateUrlQuery(null);
this.clear();
} else if (input_trimmed == "list" || input_trimmed == "ll" || input_trimmed == "ls") {
} else if (input_trimmed == "list" || input_trimmed == "ls") {
output = numbat.print_environment();
} else if (input_trimmed == "list functions" || input_trimmed == "ls functions") {
output = numbat.print_functions();
} else if (input_trimmed == "list dimensions" || input_trimmed == "ls dimensions") {
output = numbat.print_dimensions();
} else if (input_trimmed == "list variables" || input_trimmed == "ls variables") {
output = numbat.print_variables();
} else if (input_trimmed == "list units" || input_trimmed == "ls units") {
output = numbat.print_units();
} else if (input_trimmed == "help" || input_trimmed == "?") {
output = numbat.help();
} else {
Expand Down
4 changes: 4 additions & 0 deletions numbat-wasm/www/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ p.links a {
color: #888 !important;
}

.hl-emphasized {
font-weight: bold;
}

.hl-string {
color: #59f78d !important;
}
Expand Down
Loading