-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Enhance the help command to print info about a given unit #267
Conversation
dbe6533
to
d280b29
Compare
The second commit adds support for attaching metadata to local variables and into the
|
8b35edc
to
f10c430
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice — thank you very much!
I added some additional metadata for math constants.
81b412b
to
fcbd5ce
Compare
Ok, given that I didn't see any objections to the approach taken here, I think this is ready for a fuller review. I want to take another pass at the output formatting of the help output (I welcome any and all comments on this) |
pub fn get_defining_unit(&self, unit_name: &str) -> Option<&Unit> { | ||
self.unit_name_to_constant_index | ||
.get(unit_name) | ||
.and_then(|idx| self.vm.constants.get(*idx as usize)) | ||
.and_then(|constant| match constant { | ||
Constant::Unit(u) => Some(u), | ||
_ => None, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
pub fn contains_aliases_with_prefixes(decorates: &[Decorator]) -> bool { | ||
for decorator in decorates { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor:
pub fn contains_aliases_with_prefixes(decorates: &[Decorator]) -> bool { | |
for decorator in decorates { | |
pub fn contains_aliases_with_prefixes(decorators: &[Decorator]) -> bool { | |
for decorator in decorators { |
or is "decorates" the correct term here? :-)
return help; | ||
} | ||
|
||
m::text("Not found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be fantastic if we could also use this for types, i.e. do info ElectricCharge
and get information about how it is defined and what the base representation is. But I guess we can leave this for another PR. I think it's also far less important than units and constants.
numbat/src/lib.rs
Outdated
let mut help = | ||
m::text("Unit: ") + m::unit(md.name.as_deref().unwrap_or(keyword)) + m::nl(); | ||
if let Some(url) = &md.url { | ||
help += m::string(url) + m::nl(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could maybe put the URL after the name? And similar for variables.
>>> info C
Unit: Coulomb (https://en.wikipedia.org/wiki/Coulomb)
Aliases: coulomb, coulombs, C
A unit of [ElectricCharge]
1 coulomb = 1 A x s
numbat/src/lib.rs
Outdated
if !prefix.is_none() { | ||
help += m::nl() | ||
+ m::value("1 ") | ||
+ m::type_identifier(keyword) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
+ m::type_identifier(keyword) | |
+ m::unit(keyword) |
numbat/src/lib.rs
Outdated
+ m::text(" = ") | ||
+ m::value(prefix.factor().pretty_print()) | ||
+ m::space() | ||
+ m::type_identifier(&full_name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here:
+ m::type_identifier(&full_name); | |
+ m::unit(&full_name); |
numbat/src/lib.rs
Outdated
if let Some(BaseUnitAndFactor(prod, num)) = x { | ||
help += m::nl() | ||
+ m::value("1 ") | ||
+ m::type_identifier(&full_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+ m::type_identifier(&full_name) | |
+ m::unit(&full_name) |
numbat/src/lib.rs
Outdated
+ m::text(" = ") | ||
+ m::value(num.pretty_print()) | ||
+ m::space() | ||
+ prod.pretty_print_with(|f| f.exponent, 'x', '/', true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this always uses type_identifier
for the markup? In that case, maybe we can pass the markup style as an argument (see FormatType
)? Or pass m::type_identifier/m:unit as a function argument?
numbat/src/lib.rs
Outdated
+ prod.pretty_print_with(|f| f.exponent, 'x', '/', true); | ||
} else { | ||
help += | ||
m::nl() + m::type_identifier(&full_name) + m::text(" is a base unit"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m::nl() + m::type_identifier(&full_name) + m::text(" is a base unit"); | |
m::nl() + m::unit(&full_name) + m::text(" is a base unit"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really cool. We should also think about the web version. I can also make the necessary changes if you can't run it, for example.
Maybe we can also add a simple integration test in numbat-cli/tests/integration.rs
(see similar test for help
: help_text
).
Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
ab8d8be
to
22c373c
Compare
22c373c
to
71ec1fb
Compare
Ok I think I addressed all the review comments. Here's a screenshot showing how it looks now: (please also make sure 341ca6d is OK) |
👍 |
Don't print the metadata name in addition to the variable name
New commits: a cleanup on how variables are shown, an integration test, and support in the wasm version |
This PR starts to implement the enhanced help suggestion in #238.
At the moment, it looks like this:
CC #238