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

Restyle markdown help formatting to not use background colors #453

Merged
merged 1 commit into from Aug 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion 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 Cargo.toml
Expand Up @@ -79,6 +79,7 @@ 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
2 changes: 1 addition & 1 deletion edgedb-cli-derive/Cargo.toml
Expand Up @@ -6,10 +6,10 @@ authors = ["EdgeDB Inc. <hello@edgedb.com>"]
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"
minimad = "0.7.0"
syn = {version="1.0.72", features=["extra-traits"]}
quote = "1.0.9"
proc-macro2 = "1.0.19"
Expand Down
88 changes: 7 additions & 81 deletions edgedb-cli-derive/src/attrib.rs
Expand Up @@ -8,6 +8,7 @@ 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 @@ -590,49 +591,18 @@ impl Markdown {
};
parser.parse2(attr.tokens.clone()).unwrap_or_abort();
}
pub fn markdown_text(&self) -> String {
let text = self.source.value();
let mut min_indent = text.len();
for line in text.lines() {
let stripped = line.trim_start();
if stripped.is_empty() {
continue;
}
let indent = line.len() - stripped.len();
if indent < min_indent {
min_indent = indent;
}
}
if min_indent == 0 {
return text;
}
let mut buf = String::with_capacity(text.len());
for line in text.lines() {
if line.len() > min_indent {
buf.push_str(&line[min_indent..]);
}
buf.push('\n');
}
return buf;
}
pub fn clap_text(&self) -> syn::LitStr {
let markdown = self.markdown_text();
let text = parse_markdown(&markdown);
let skin = termimad::MadSkin::default();
let fmt = termimad::FmtText::from_text(
&skin,
text,
None,
);
syn::LitStr::new(&fmt.to_string(), self.source.span())
let text = self.source.value();
syn::LitStr::new(&mdstyle::format_markdown(&text), self.source.span())
}
pub fn formatted_title(&self) -> syn::LitStr {
let markdown = self.markdown_text();
let mut text = parse_markdown(&markdown);
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 = termimad::MadSkin::default();
let skin = mdstyle::make_skin();
let fmt = termimad::FmtText::from_text(
&skin,
text,
Expand Down Expand Up @@ -718,47 +688,3 @@ impl CliParse {
!matches!(self.kind, FromOccurrences | FromFlag)
}
}

fn parse_markdown(text: &str) -> minimad::Text {
use minimad::{Text, Composite};
use minimad::Line::*;
use minimad::CompositeStyle::*;

let lines = Text::from(&text[..]).lines;
let mut text = Text { lines: Vec::with_capacity(lines.len()) };
for line in lines.into_iter() {
if let Normal(Composite { style, compounds: cmps }) = line {
if cmps.len() == 0 {
text.lines.push(
Normal(Composite { style, compounds: cmps })
);
continue;
}
match (style, text.lines.last_mut()) {
(_, Some(&mut Normal(Composite { ref compounds , ..})))
if compounds.len() == 0
=> {
text.lines.push(
Normal(Composite { style, compounds: cmps })
);
}
| (Paragraph, Some(&mut Normal(Composite {
style: Paragraph, ref mut compounds })))
| (Paragraph, Some(&mut Normal(Composite {
style: ListItem, ref mut compounds })))
| (Quote, Some(&mut Normal(Composite {
style: Quote, ref mut compounds })))
=> {
compounds.push(minimad::Compound::raw_str(" "));
compounds.extend(cmps);
}
_ => {
text.lines.push(
Normal(Composite { style, compounds: cmps })
);
}
}
}
}
return text;
}
3 changes: 3 additions & 0 deletions edgedb-cli-md/.gitignore
@@ -0,0 +1,3 @@
/Cargo.lock
/target

11 changes: 11 additions & 0 deletions edgedb-cli-md/Cargo.toml
@@ -0,0 +1,11 @@
[package]
name = "edgedb-cli-md"
license = "MIT/Apache-2.0"
version = "0.3.0"
authors = ["EdgeDB Inc. <hello@edgedb.com>"]
edition = "2018"

[dependencies]
termimad = "0.10.2"
minimad = "0.7.0"
crossterm = "0.19.0"
91 changes: 91 additions & 0 deletions edgedb-cli-md/src/lib.rs
@@ -0,0 +1,91 @@
pub fn prepare_markdown(text: &str) -> String {
let mut min_indent = text.len();
for line in text.lines() {
let stripped = line.trim_start();
if stripped.is_empty() {
continue;
}
let indent = line.len() - stripped.len();
if indent < min_indent {
min_indent = indent;
}
}
if min_indent == 0 {
return text.to_string();
}
let mut buf = String::with_capacity(text.len());
for line in text.lines() {
if line.len() > min_indent {
buf.push_str(&line[min_indent..]);
}
buf.push('\n');
}
return buf;
}

pub fn make_skin() -> termimad::MadSkin {
use crossterm::style::{Color, Attribute};
let mut skin = termimad::MadSkin::default();
skin.bold.set_fg(Color::Reset);
skin.inline_code.set_bg(Color::Reset);
skin.inline_code.add_attr(Attribute::Bold);
skin.code_block.set_bg(Color::Reset);
skin.code_block.add_attr(Attribute::Bold);
skin
}

pub fn parse_markdown(text: &str) -> minimad::Text {
use minimad::{Text, Composite};
use minimad::Line::*;
use minimad::CompositeStyle::*;

let lines = Text::from(&text[..]).lines;
let mut text = Text { lines: Vec::with_capacity(lines.len()) };
for line in lines.into_iter() {
if let Normal(Composite { style, compounds: cmps }) = line {
if cmps.len() == 0 {
text.lines.push(
Normal(Composite { style, compounds: cmps })
);
continue;
}
match (style, text.lines.last_mut()) {
(_, Some(&mut Normal(Composite { ref compounds , ..})))
if compounds.len() == 0
=> {
text.lines.push(
Normal(Composite { style, compounds: cmps })
);
}
| (Paragraph, Some(&mut Normal(Composite {
style: Paragraph, ref mut compounds })))
| (Paragraph, Some(&mut Normal(Composite {
style: ListItem, ref mut compounds })))
| (Quote, Some(&mut Normal(Composite {
style: Quote, ref mut compounds })))
=> {
compounds.push(minimad::Compound::raw_str(" "));
compounds.extend(cmps);
}
_ => {
text.lines.push(
Normal(Composite { style, compounds: cmps })
);
}
}
}
}
return 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,
text,
None,
);
fmt.to_string()
}
5 changes: 4 additions & 1 deletion src/options.rs
Expand Up @@ -13,6 +13,8 @@ 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 Down Expand Up @@ -500,9 +502,10 @@ 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(EDGEDB_ABOUT)
.about(about.as_str())
.term_width(term_width());
let app = update_main_help(app);
let matches = get_matches(app);
Expand Down