Skip to content

Commit

Permalink
Fix missing open/close at first indentation level (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk authored Mar 9, 2021
1 parent 40a6827 commit 169685a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/basic.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1:mainbasic::hierarchical-example version=0.1
1:main┐basic::hierarchical-example version=0.1
1:main├┐basic::hierarchical-example version=0.1
1:main│└┐basic::server host="localhost", port=8080
1:main│ ├─ms INFO basic starting
Expand Down Expand Up @@ -38,4 +38,4 @@
1:main│ ├─ms INFO basic exit
1:main│┌┘basic::server host="localhost", port=8080
1:main├┘basic::hierarchical-example version=0.1
1:mainbasic::hierarchical-example version=0.1
1:main┘basic::hierarchical-example version=0.1
4 changes: 2 additions & 2 deletions examples/quiet.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1:mainquiet::hierarchical-example version=0.1
1:main┐quiet::hierarchical-example version=0.1
1:main├─┐quiet::server host="localhost", port=8080
1:main│ ├─ms INFO quiet starting
1:main│ ├─ms INFO quiet listening
Expand All @@ -25,4 +25,4 @@
1:main│ ├─ms WARN quiet internal error
1:main│ ├─ms INFO quiet exit
1:main├─┘
1:main
1:main
4 changes: 2 additions & 2 deletions examples/stderr.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fibonacci_seq{to=5}
fibonacci_seq{to=5}
├─ms DEBUG Pushing 0 fibonacci
├─┐nth_fibonacci{n=0}
│ ├─ms DEBUG Base case
Expand Down Expand Up @@ -107,5 +107,5 @@ fibonacci_seq{to=5}
│ │ ├─┘
│ ├─┘
├─┘

INFO The first 5 fibonacci numbers are [1, 1, 2, 3, 5, 8]
28 changes: 18 additions & 10 deletions examples/wraparound.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
1:mainwraparound::recurse i=0
1:main┐wraparound::recurse i=0
1:main├─ms WARN wraparound boop
1:main├─┐wraparound::recurse i=1
1:main│ ├─ms WARN wraparound boop
Expand All @@ -7,8 +7,9 @@
1:main│ │ ├─┐wraparound::recurse i=3
1:main│ │ │ ├─ms WARN wraparound boop
1:main│ │ │ ├─┐wraparound::recurse i=4
1:main────────┘
1:mainms WARN wraparound boop
1:mainwraparound::recurse i=5
1:main┐wraparound::recurse i=5
1:main├─ms WARN wraparound boop
1:main├─┐wraparound::recurse i=6
1:main│ ├─ms WARN wraparound boop
Expand All @@ -17,8 +18,9 @@
1:main│ │ ├─┐wraparound::recurse i=8
1:main│ │ │ ├─ms WARN wraparound boop
1:main│ │ │ ├─┐wraparound::recurse i=9
1:main────────┘
1:mainms WARN wraparound boop
1:mainwraparound::recurse i=10
1:main┐wraparound::recurse i=10
1:main├─ms WARN wraparound boop
1:main├─┐wraparound::recurse i=11
1:main│ ├─ms WARN wraparound boop
Expand All @@ -27,8 +29,9 @@
1:main│ │ ├─┐wraparound::recurse i=13
1:main│ │ │ ├─ms WARN wraparound boop
1:main│ │ │ ├─┐wraparound::recurse i=14
1:main────────┘
1:mainms WARN wraparound boop
1:mainwraparound::recurse i=15
1:main┐wraparound::recurse i=15
1:main├─ms WARN wraparound boop
1:main├─┐wraparound::recurse i=16
1:main│ ├─ms WARN wraparound boop
Expand All @@ -37,16 +40,18 @@
1:main│ │ ├─┐wraparound::recurse i=18
1:main│ │ │ ├─ms WARN wraparound boop
1:main│ │ │ ├─┐wraparound::recurse i=19
1:main────────┘
1:mainms WARN wraparound boop
1:mainwraparound::recurse i=20
1:main┐wraparound::recurse i=20
1:main├─ms WARN wraparound boop
1:main├─┐wraparound::recurse i=21
1:main│ ├─ms WARN wraparound boop
1:main│ ├─ms WARN wraparound bop
1:main├─┘
1:main├─ms WARN wraparound bop
1:main
1:main
1:mainms WARN wraparound bop
1:main────────┐
1:main│ │ │ ├─┘
1:main│ │ │ ├─ms WARN wraparound bop
1:main│ │ ├─┘
Expand All @@ -55,8 +60,9 @@
1:main│ ├─ms WARN wraparound bop
1:main├─┘
1:main├─ms WARN wraparound bop
1:main
1:main
1:mainms WARN wraparound bop
1:main────────┐
1:main│ │ │ ├─┘
1:main│ │ │ ├─ms WARN wraparound bop
1:main│ │ ├─┘
Expand All @@ -65,8 +71,9 @@
1:main│ ├─ms WARN wraparound bop
1:main├─┘
1:main├─ms WARN wraparound bop
1:main
1:main
1:mainms WARN wraparound bop
1:main────────┐
1:main│ │ │ ├─┘
1:main│ │ │ ├─ms WARN wraparound bop
1:main│ │ ├─┘
Expand All @@ -75,8 +82,9 @@
1:main│ ├─ms WARN wraparound bop
1:main├─┘
1:main├─ms WARN wraparound bop
1:main
1:main
1:mainms WARN wraparound bop
1:main────────┐
1:main│ │ │ ├─┘
1:main│ │ │ ├─ms WARN wraparound bop
1:main│ │ ├─┘
Expand All @@ -85,4 +93,4 @@
1:main│ ├─ms WARN wraparound bop
1:main├─┘
1:main├─ms WARN wraparound bop
1:main
1:main
48 changes: 47 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) const LINE_BRANCH: &str = "├";
pub(crate) const LINE_CLOSE: &str = "┘";
pub(crate) const LINE_OPEN: &str = "┐";

#[derive(Copy, Clone)]
pub(crate) enum SpanMode {
PreOpen,
Open { verbose: bool },
Expand Down Expand Up @@ -167,17 +168,53 @@ impl Buffers {

pub(crate) fn indent_current(&mut self, indent: usize, config: &Config, style: SpanMode) {
self.current_buf.push('\n');
let prefix = config.prefix();

// Render something when wraparound occurs so the user is aware of it
if config.indent_lines {
match style {
SpanMode::Close { .. } | SpanMode::PostClose => {
if indent > 0 && (indent + 1) % config.wraparound == 0 {
self.indent_buf.push_str(&prefix);
for _ in 0..(indent % config.wraparound * config.indent_amount) {
self.indent_buf.push_str(LINE_HORIZ);
}
self.indent_buf.push_str(LINE_OPEN);
self.indent_buf.push('\n');
}
}
_ => {}
}
}

indent_block(
&mut self.current_buf,
&mut self.indent_buf,
indent % config.wraparound,
config.indent_amount,
config.indent_lines,
&config.prefix(),
&prefix,
style,
);
self.current_buf.clear();
self.flush_indent_buf();

// Render something when wraparound occurs so the user is aware of it
if config.indent_lines {
match style {
SpanMode::PreOpen | SpanMode::Open { .. } => {
if indent > 0 && (indent + 1) % config.wraparound == 0 {
self.current_buf.push_str(&prefix);
for _ in 0..(indent % config.wraparound * config.indent_amount) {
self.current_buf.push_str(LINE_HORIZ);
}
self.current_buf.push_str(LINE_CLOSE);
self.current_buf.push('\n');
}
}
_ => {}
}
}
}
}

Expand Down Expand Up @@ -235,6 +272,15 @@ fn indent_block_with_lines(
} else if indent_spaces == 0 {
for line in lines {
buf.push_str(prefix);
// The first indent is special, we only need to print open/close and nothing else
if indent == 0 {
match style {
SpanMode::Open { .. } => buf.push_str(LINE_OPEN),
SpanMode::Close { .. } => buf.push_str(LINE_CLOSE),
SpanMode::PreOpen | SpanMode::PostClose => {}
SpanMode::Event => {}
}
}
buf.push_str(line);
buf.push('\n');
}
Expand Down

0 comments on commit 169685a

Please sign in to comment.