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
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
12 changes: 1 addition & 11 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ prettytable-rs = {version="0.8.0", default-features=false}
tempfile = "3.1.0"
codespan-reporting = "0.11"
termcolor = "1.1.0"
crossterm = "0.19.0"
async-listen = "0.2.0"
sha1 = "0.6.0"
hex = "0.4.3"
Expand Down Expand Up @@ -79,7 +80,6 @@ toml = "0.5.8"
termimad = "0.10.2"
minimad = "0.7.0"
edgedb-cli-derive = { path="edgedb-cli-derive" }
edgedb-cli-md = { path="edgedb-cli-md" }
fs-err = "2.6.0"
pem = "0.8"
rustls = {version="0.19.1", features=["dangerous_configuration"]}
Expand Down
1 change: 0 additions & 1 deletion edgedb-cli-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["EdgeDB Inc. <[email protected]>"]
edition = "2018"

[dependencies]
edgedb-cli-md = { path="../edgedb-cli-md" }
clap = { git="https://github.com/clap-rs/clap" }
clap_generate = { git="https://github.com/clap-rs/clap" }
termimad = "0.10.2"
Expand Down
20 changes: 0 additions & 20 deletions edgedb-cli-derive/src/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use syn::parse::{Parse, Parser, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::Paren;

use edgedb_cli_md as mdstyle;
use crate::kw;


Expand Down Expand Up @@ -591,25 +590,6 @@ impl Markdown {
};
parser.parse2(attr.tokens.clone()).unwrap_or_abort();
}
pub fn clap_text(&self) -> syn::LitStr {
let text = self.source.value();
syn::LitStr::new(&mdstyle::format_markdown(&text), self.source.span())
}
pub fn formatted_title(&self) -> syn::LitStr {
let text = self.source.value();
let text = mdstyle::prepare_markdown(&text);
let mut text = mdstyle::parse_markdown(&text);
if !text.lines.is_empty() {
text.lines.drain(1..);
}
let skin = mdstyle::make_skin();
let fmt = termimad::FmtText::from_text(
&skin,
text,
None,
);
syn::LitStr::new(fmt.to_string().trim(), self.source.span())
}
}

impl TryFrom<syn::Expr> for Case {
Expand Down
33 changes: 25 additions & 8 deletions edgedb-cli-derive/src/into_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub fn structure(s: &types::Struct) -> TokenStream {
let propagate_args = mk_struct_propagate(&s, &dest, &matches);

let help = s.attrs.help.as_ref().or(s.attrs.doc.as_ref())
.map(|text| text.clap_text().value()).unwrap_or_else(String::new);
.map(|text| text.source.value().to_string()).unwrap_or_else(String::new);
let help_title = s.attrs.help.as_ref().or(s.attrs.doc.as_ref())
.map(|text| text.formatted_title().value())
.map(|text| text.source.value().to_string())
.unwrap_or_else(String::new);
let subcmds = if let Some(sub) =
s.fields.iter().find(|s| s.attrs.subcommand)
Expand Down Expand Up @@ -227,9 +227,12 @@ fn mk_arg(field: &types::Field, case: &Case) -> TokenStream {
}

if let Some(text) = field.attrs.help.as_ref().or(field.attrs.doc.as_ref()) {
let formatted = text.clap_text();
let source = &text.source;
modifiers.extend(quote! {
#arg = #arg.about(#formatted);
static ABOUT: ::once_cell::sync::Lazy<String> =
::once_cell::sync::Lazy::new(
|| crate::markdown::format_markdown(#source));
#arg = #arg.about((&ABOUT).as_str());
});
}
if let Some(name) = field.attrs.name.as_ref() {
Expand Down Expand Up @@ -294,6 +297,15 @@ fn mk_struct(s: &types::Struct, app: &syn::Ident,
#app = #app.#name(#value);
});
}
if let Some(doc) = &s.attrs.doc {
let source = &doc.source;
output.extend(quote! {
static ABOUT: ::once_cell::sync::Lazy<String> =
::once_cell::sync::Lazy::new(
|| crate::markdown::format_markdown(#source));
#app = #app.about((&ABOUT).as_str());
});
}
let (subcmd_interface, flat_interface) = if inheritance {
(quote!(clap::Subcommand), quote!(clap::IntoApp))
} else {
Expand Down Expand Up @@ -424,9 +436,14 @@ fn mk_subcommand(s: &types::Subcommand, sub: &syn::Ident)
let mut modifiers = TokenStream::new();

if let Some(text) = s.attrs.about.as_ref().or(s.attrs.doc.as_ref()) {
let formatted = text.clap_text();
let source = &text.source;
modifiers.extend(quote! {
#sub = #sub.about(#formatted);
{
static ABOUT: ::once_cell::sync::Lazy<String> =
::once_cell::sync::Lazy::new(
|| crate::markdown::format_markdown(#source));
#sub = #sub.about((&ABOUT).as_str());
}
});
}
for (name, value) in &s.attrs.options {
Expand Down Expand Up @@ -830,12 +847,12 @@ fn subcmd_to_desc(sub: &types::Subcommand, e: &types::Enum) -> TokenStream {
});
let about = sub.attrs.about.as_ref()
.or(sub.attrs.doc.as_ref())
.map(|a| a.clap_text())
.map(|a| a.source.clone())
.map(|v| quote!(Some(#v)))
.unwrap_or_else(|| quote!(None));
let title = sub.attrs.about.as_ref()
.or(sub.attrs.doc.as_ref())
.map(|a| a.formatted_title())
.map(|a| a.source.value().to_string())
.map(|v| quote!(Some(#v)))
.unwrap_or_else(|| quote!(None));
let hidden = sub.attrs.hidden;
Expand Down
3 changes: 0 additions & 3 deletions edgedb-cli-md/.gitignore

This file was deleted.

11 changes: 0 additions & 11 deletions edgedb-cli-md/Cargo.toml

This file was deleted.

1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod highlight;
mod hint;
mod interactive;
mod log_levels;
mod markdown;
mod migrations;
mod non_interactive;
mod options;
Expand Down
34 changes: 28 additions & 6 deletions edgedb-cli-md/src/lib.rs → src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub fn prepare_markdown(text: &str) -> String {
use once_cell::sync::Lazy;

fn prepare_markdown(text: &str) -> String {
let mut min_indent = text.len();
for line in text.lines() {
let stripped = line.trim_start();
Expand All @@ -23,18 +25,25 @@ pub fn prepare_markdown(text: &str) -> String {
return buf;
}

pub fn make_skin() -> termimad::MadSkin {
static MADSKIN: Lazy<termimad::MadSkin> = Lazy::new(|| {
use crossterm::style::{Color, Attribute};

if !atty::is(atty::Stream::Stdout) {
return termimad::MadSkin::no_style();
}

let mut skin = termimad::MadSkin::default();
skin.bold.set_fg(Color::Reset);
skin.inline_code.set_fg(Color::Reset);
skin.inline_code.set_bg(Color::Reset);
skin.inline_code.add_attr(Attribute::Bold);
skin.code_block.set_fg(Color::Reset);
skin.code_block.set_bg(Color::Reset);
skin.code_block.add_attr(Attribute::Bold);
skin
}
});

pub fn parse_markdown(text: &str) -> minimad::Text {
fn parse_markdown(text: &str) -> minimad::Text {
use minimad::{Text, Composite};
use minimad::Line::*;
use minimad::CompositeStyle::*;
Expand Down Expand Up @@ -81,11 +90,24 @@ pub fn parse_markdown(text: &str) -> minimad::Text {
pub fn format_markdown(text: &str) -> String {
let text = prepare_markdown(&text);
let text = parse_markdown(&text);
let skin = make_skin();
let fmt = termimad::FmtText::from_text(
&skin,
&MADSKIN,
text,
None,
);
fmt.to_string()
}

pub fn format_title(text: &str) -> String {
let text = prepare_markdown(&text);
let mut text = parse_markdown(&text);
if !text.lines.is_empty() {
text.lines.drain(1..);
}
let fmt = termimad::FmtText::from_text(
&MADSKIN,
text,
None,
);
fmt.to_string().trim().to_string()
}
25 changes: 13 additions & 12 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use edgedb_client::Builder;
use edgedb_cli_derive::EdbClap;
use fs_err as fs;

use edgedb_cli_md as mdstyle;

use crate::cli;
use crate::cli::options::CliCommand;
use crate::commands::parser::Common;
Expand All @@ -25,6 +23,7 @@ use crate::hint::HintExt;
use crate::project;
use crate::repl::OutputFormat;
use crate::server;
use crate::markdown;

pub mod describe;

Expand All @@ -38,12 +37,6 @@ static CONNECTION_ARG_HINT: &str = "\
const CONN_OPTIONS_GROUP: &str =
"CONNECTION OPTIONS (`edgedb --help-connect` to see the full list)";

const EDGEDB_ABOUT: &str = "\
Use the `edgedb` command-line tool to spin up local instances, \
manage EdgeDB projects, create and apply migrations, and more. \
\n\n\
Running `edgedb` without a subcommand opens an interactive shell.";

pub trait PropagateArgs {
fn propagate_args(&self, dest: &mut AnyMap, matches: &clap::ArgMatches);
}
Expand Down Expand Up @@ -150,6 +143,10 @@ pub struct ConnectionOptions {
pub connect_timeout: Option<Duration>,
}

/// Use the `edgedb` command-line tool to spin up local instances,
/// manage EdgeDB projects, create and apply migrations, and more.
///
/// Running `edgedb` without a subcommand opens an interactive shell.
#[derive(EdbClap, Debug)]
#[edb(main)]
#[clap(setting=clap::AppSettings::DisableVersionFlag)]
Expand Down Expand Up @@ -338,15 +335,15 @@ fn make_subcommand_help<T: describe::Describe>() -> String {
}
writeln!(&mut buf, " {:padding$} {}",
format!("{} {}", cmd.name, subcmd.name),
wrap(sdescr.help_title),
wrap(&markdown::format_title(sdescr.help_title)),
padding=padding
).unwrap();
}
buf.push('\n');
empty_line = true;
} else {
writeln!(&mut buf, " {:padding$} {}",
cmd.name, wrap(cdescr.help_title),
cmd.name, wrap(&markdown::format_title(cdescr.help_title)),
padding=padding
).unwrap();
empty_line = false;
Expand All @@ -368,6 +365,12 @@ fn update_main_help(mut app: clap::App) -> clap::App {
let mut help = help[..subcmd_index].replacen("edgedb", "EdgeDB CLI", 1);
help.push_str(&sub_cmd);

help = help.replacen(
CONN_OPTIONS_GROUP,
&markdown::format_markdown(CONN_OPTIONS_GROUP).trim(),
1
);

let help = std::str::from_utf8(Vec::leak(help.into())).unwrap();
return app.override_help(help);
}
Expand Down Expand Up @@ -502,10 +505,8 @@ fn term_width() -> usize {

impl Options {
pub fn from_args_and_env() -> anyhow::Result<Options> {
let about = mdstyle::format_markdown(&EDGEDB_ABOUT);
let app = <RawOptions as clap::IntoApp>::into_app()
.name("edgedb")
.about(about.as_str())
.term_width(term_width());
let app = update_main_help(app);
let matches = get_matches(app);
Expand Down