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

Adding more styling options for users #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
Cargo.lock
.idea
24 changes: 24 additions & 0 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ impl Characters {
}
}

pub fn unicode_straight() -> Self {
Self {
hbar: '─',
vbar: '│',
xbar: '┼',
vbar_break: '┆',
vbar_gap: '┆',
uarrow: '▲',
rarrow: '▶',
ltop: '┌',
mtop: '┬',
rtop: '┐',
lbot: '└',
mbot: '┴',
rbot: '┘',
lbox: '[',
rbox: ']',
lcross: '├',
rcross: '┤',
underbar: '┬',
underline: '─',
}
}

pub fn ascii() -> Self {
Self {
hbar: '-',
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ pub enum LabelAttach {
pub enum CharSet {
/// Unicode characters (an attempt is made to use only commonly-supported characters).
Unicode,
/// Unicode characters for box drawing, using straight lines instead of curved ones.
UnicodeStraight,
/// ASCII-only characters.
Ascii,
}
Expand Down
48 changes: 48 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl<S: Span> Report<'_, S> {
) -> io::Result<()> {
let draw = match self.config.char_set {
CharSet::Unicode => draw::Characters::unicode(),
CharSet::UnicodeStraight => draw::Characters::unicode_straight(),
CharSet::Ascii => draw::Characters::ascii(),
};

Expand Down Expand Up @@ -1184,4 +1185,51 @@ mod tests {
---'
"###)
}

#[test]
fn unicode_style() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(Config::default().with_char_set(CharSet::Unicode).with_color(false))
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
.with_label(Label::new(9..15).with_message("This is an orange"))
.finish()
.write_to_string(Source::from(source));
assert_snapshot!(msg, @r###"
Error: can't compare apples with oranges
╭─[<unknown>:1:1]
1 │ apple == orange;
│ ──┬── ───┬──
│ ╰────────────── This is an apple
│ │
│ ╰──── This is an orange
───╯
"###)
}

#[test]
fn unicode_stright_style() {
let source = "apple == orange;";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(Config::default().with_char_set(CharSet::UnicodeStraight).with_color(false))
.with_message("can't compare apples with oranges")
.with_label(Label::new(0..5).with_message("This is an apple"))
.with_label(Label::new(9..15).with_message("This is an orange"))
.finish()
.write_to_string(Source::from(source));
assert_snapshot!(msg, @r###"
Error: can't compare apples with oranges
┌─[<unknown>:1:1]
1 │ apple == orange;
│ ──┬── ───┬──
│ └────────────── This is an apple
│ │
│ └──── This is an orange
───┘
"###)
}
}

Loading