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

Move markdown formatting from derive to runtime #455

Merged
merged 5 commits into from
Aug 11, 2021
Merged

Move markdown formatting from derive to runtime #455

merged 5 commits into from
Aug 11, 2021

Conversation

1st1
Copy link
Member

@1st1 1st1 commented Aug 10, 2021

This eliminates the newly added edgedb-cli-md local crate. It also
allows to disable styling if stdout isn't tty and makes formatting more
flexible overall.

@1st1 1st1 requested review from tailhook, elprans and fantix August 10, 2021 18:39
@1st1
Copy link
Member Author

1st1 commented Aug 10, 2021

Continuation of #453

1st1 added 3 commits August 10, 2021 19:31
This eliminates the newly added `edgedb-cli-md` local crate. It also
allows to disable styling if stdout isn't tty and makes formatting more
flexible overall.
Copy link
Contributor

@tailhook tailhook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally good. But I feel anxious about so many leak calls. Technically they are fine, as called once, but still not a good pattern. We can use static variables instead:

-            let about = Box::new(crate::markdown::format_markdown(#source));
-            #app = #app.about(Box::leak(about).as_str());
+            static ABOUT: ::once_cell::sync::Lazy<String> = ::once_cell::sync::Lazy::new(|| crate::markdown::format_markdown(#source)));
+            #app = #app.about(&ABOUT);

Difference is that if the function is called twice we will get a cached value.

src/markdown.rs Outdated
if !atty::is(atty::Stream::Stdout) {
return termimad::MadSkin::no_style();
}

use crossterm::style::{Color, Attribute};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move use at the top of the block. And keep a line separating use from code. Or just move it to the module level. We usually import at function level either enums like Color::* or platform-specific stuff, or ocasionally if we need to override something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, moved to the top of the block. I'm also fine to move it to the top of the module, my motivation to keep it in the function was that those names are only used in it, and the function is like 10% of the file. But again, I'm all for consistent code style so let me know if you want to move it to the module level.

src/markdown.rs Outdated
@@ -23,18 +23,24 @@ pub fn prepare_markdown(text: &str) -> String {
return buf;
}

pub fn make_skin() -> termimad::MadSkin {
fn make_skin() -> termimad::MadSkin {
Copy link
Contributor

@tailhook tailhook Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's store MadSkin in a once_cell::sync::Lazy. As even if MadSkin constructor is fast enough, we do a syscall in the function (atty::is). Doing that per each piece of text may not be very noticeable on fast desktop CPUs, but may be slow on Raspberry PIs or cheap VMs exhausted their burst quota.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've only discovered Lazy when I was fixing the test suite last night. Super cool and makes a lot of sense. Fixed.

@1st1
Copy link
Member Author

1st1 commented Aug 11, 2021

Generally good. But I feel anxious about so many leak calls. Technically they are fine, as called once, but still not a good pattern. We can use static variables instead:

Sure thing, fixed.

@1st1 1st1 requested a review from tailhook August 11, 2021 15:32
@1st1 1st1 merged commit 4ccea4d into master Aug 11, 2021
@1st1 1st1 deleted the runtimemd branch August 11, 2021 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants