-
Notifications
You must be signed in to change notification settings - Fork 29
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
example improvements #184
Draft
JeromeSchmied
wants to merge
3
commits into
jugglerchris:main
Choose a base branch
from
JeromeSchmied:example-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
example improvements #184
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,88 +7,91 @@ use log::trace; | |
use std::io; | ||
use std::io::Write; | ||
|
||
#[cfg(unix)] | ||
use html2text::render::RichAnnotation; | ||
#[cfg(unix)] | ||
fn default_colour_map( | ||
annotations: &[RichAnnotation], | ||
s: &str, | ||
use_css_colours: bool, | ||
no_default_colours: bool, | ||
hyperlinks: bool, | ||
) -> String { | ||
use termion::color::*; | ||
use yansi::{hyperlink::HyperlinkExt, Paint}; | ||
use RichAnnotation::*; | ||
// Explicit CSS colours override any other colours | ||
let mut have_explicit_colour = no_default_colours; | ||
let mut start = Vec::new(); | ||
let mut finish = Vec::new(); | ||
let mut styled = s.to_string(); | ||
trace!("default_colour_map: str={s}, annotations={annotations:?}"); | ||
for annotation in annotations.iter() { | ||
match annotation { | ||
Default => {} | ||
Link(_) => { | ||
start.push(format!("{}", termion::style::Underline)); | ||
finish.push(format!("{}", termion::style::Reset)); | ||
} | ||
Image(_) => { | ||
if !have_explicit_colour { | ||
start.push(format!("{}", Fg(Blue))); | ||
finish.push(format!("{}", Fg(Reset))); | ||
styled = match annotation { | ||
Default => styled, | ||
Link(url) => { | ||
if hyperlinks { | ||
styled.link(url).blue().underline().to_string() | ||
} else if !have_explicit_colour { | ||
styled.blue().underline().to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
Emphasis => { | ||
start.push(format!("{}", termion::style::Bold)); | ||
finish.push(format!("{}", termion::style::Reset)); | ||
Image(img) => { | ||
if hyperlinks { | ||
styled.underline().blue().italic().link(img).to_string() | ||
} else if !have_explicit_colour { | ||
styled.yellow().italic().to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
Emphasis => styled.italic().to_string(), | ||
Strong => { | ||
if !have_explicit_colour { | ||
start.push(format!("{}", Fg(LightYellow))); | ||
finish.push(format!("{}", Fg(Reset))); | ||
styled.bold().to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
Strikeout => { | ||
if !have_explicit_colour { | ||
start.push(format!("{}", Fg(LightBlack))); | ||
finish.push(format!("{}", Fg(Reset))); | ||
styled.strike().to_string() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The text should already be struck out already by this point (using U+336). |
||
} else { | ||
styled | ||
} | ||
} | ||
Code => { | ||
if !have_explicit_colour { | ||
start.push(format!("{}", Fg(Blue))); | ||
finish.push(format!("{}", Fg(Reset))); | ||
styled.blue().to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
Preformat(_) => { | ||
if !have_explicit_colour { | ||
start.push(format!("{}", Fg(Blue))); | ||
finish.push(format!("{}", Fg(Reset))); | ||
styled.blue().to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
Colour(c) => { | ||
if use_css_colours { | ||
start.push(format!("{}", Fg(Rgb(c.r, c.g, c.b)))); | ||
finish.push(format!("{}", Fg(Reset))); | ||
have_explicit_colour = true; | ||
styled.rgb(c.r, c.g, c.b).to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
BgColour(c) => { | ||
if use_css_colours { | ||
start.push(format!("{}", Bg(Rgb(c.r, c.g, c.b)))); | ||
finish.push(format!("{}", Bg(Reset))); | ||
styled.on_rgb(c.r, c.g, c.b).to_string() | ||
} else { | ||
styled | ||
} | ||
} | ||
_ => {} | ||
_ => styled, | ||
} | ||
} | ||
// Reverse the finish sequences | ||
finish.reverse(); | ||
let mut result = start.join(""); | ||
result.push_str(s); | ||
for s in finish { | ||
result.push_str(&s); | ||
} | ||
trace!("default_colour_map: output={result}"); | ||
result | ||
trace!("default_colour_map: output={styled}"); | ||
styled | ||
} | ||
|
||
fn update_config<T: TextDecorator>(mut config: Config<T>, flags: &Flags) -> Config<T> { | ||
|
@@ -106,7 +109,6 @@ fn translate<R>(input: R, flags: Flags, literal: bool) -> String | |
where | ||
R: io::Read, | ||
{ | ||
#[cfg(unix)] | ||
{ | ||
if flags.use_colour { | ||
let conf = config::rich(); | ||
|
@@ -121,7 +123,7 @@ where | |
let use_only_css = false; | ||
return conf | ||
.coloured(input, flags.width, move |anns, s| { | ||
default_colour_map(anns, s, use_css_colours, use_only_css) | ||
default_colour_map(anns, s, use_css_colours, use_only_css, flags.hyperlinks) | ||
}) | ||
.unwrap(); | ||
} | ||
|
@@ -173,6 +175,7 @@ struct Flags { | |
show_render: bool, | ||
#[cfg(feature = "css")] | ||
show_css: bool, | ||
hyperlinks: bool, | ||
} | ||
|
||
fn main() { | ||
|
@@ -195,6 +198,7 @@ fn main() { | |
show_render: false, | ||
#[cfg(feature = "css")] | ||
show_css: false, | ||
hyperlinks: false, | ||
}; | ||
let mut literal: bool = false; | ||
|
||
|
@@ -225,7 +229,6 @@ fn main() { | |
StoreTrue, | ||
"Output only literal text (no decorations)", | ||
); | ||
#[cfg(unix)] | ||
ap.refer(&mut flags.use_colour).add_option( | ||
&["--colour"], | ||
StoreTrue, | ||
|
@@ -259,6 +262,11 @@ fn main() { | |
StoreTrue, | ||
"Show the parsed CSS instead of rendered output", | ||
); | ||
ap.refer(&mut flags.hyperlinks).add_option( | ||
&["--hyperlinks"], | ||
StoreTrue, | ||
"Show clickable, proper hyperlinks", | ||
); | ||
ap.parse_args_or_exit(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should remove the termion dependency if it's no longer used.