Skip to content

Commit 622e425

Browse files
authored
Wrap back to zero after a specific number of indentation levels (#15)
1 parent 6a8c7fd commit 622e425

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

examples/wraparound.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use tracing::{instrument, warn};
2+
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
3+
use tracing_tree::HierarchicalLayer;
4+
5+
fn main() {
6+
let layer = HierarchicalLayer::default()
7+
.with_indent_lines(true)
8+
.with_indent_amount(2)
9+
.with_thread_names(true)
10+
.with_thread_ids(true)
11+
.with_targets(true)
12+
.with_wraparound(5);
13+
14+
let subscriber = Registry::default().with(layer);
15+
tracing::subscriber::set_global_default(subscriber).unwrap();
16+
17+
recurse(0);
18+
}
19+
20+
#[instrument]
21+
fn recurse(i: usize) {
22+
warn!("boop");
23+
if i > 20 {
24+
warn!("bop");
25+
return;
26+
} else {
27+
recurse(i + 1);
28+
}
29+
warn!("bop");
30+
}

src/format.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub struct Config {
2626
pub render_thread_ids: bool,
2727
/// Whether to show thread names.
2828
pub render_thread_names: bool,
29+
/// Specifies after how many indentation levels we will wrap back around to zero
30+
pub wraparound: usize,
2931
}
3032

3133
impl Config {
@@ -58,6 +60,10 @@ impl Config {
5860
}
5961
}
6062

63+
pub fn with_wraparound(self, wraparound: usize) -> Self {
64+
Self { wraparound, ..self }
65+
}
66+
6167
pub(crate) fn prefix(&self) -> String {
6268
let mut buf = String::new();
6369
if self.render_thread_ids {
@@ -90,6 +96,7 @@ impl Default for Config {
9096
targets: false,
9197
render_thread_ids: false,
9298
render_thread_names: false,
99+
wraparound: usize::max_value(),
93100
}
94101
}
95102
}
@@ -123,7 +130,7 @@ impl Buffers {
123130
indent_block(
124131
&mut self.current_buf,
125132
&mut self.indent_buf,
126-
indent,
133+
indent % config.wraparound,
127134
config.indent_amount,
128135
config.indent_lines,
129136
&config.prefix(),

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ where
139139
}
140140
}
141141

142+
/// Resets the indentation to zero after `wraparound` indentation levels.
143+
/// This is helpful if you expect very deeply nested spans as otherwise the indentation
144+
/// just runs out of your screen.
145+
pub fn with_wraparound(self, wraparound: usize) -> Self {
146+
Self {
147+
config: self.config.with_wraparound(wraparound),
148+
..self
149+
}
150+
}
151+
142152
fn styled(&self, style: Style, text: impl AsRef<str>) -> String {
143153
if self.config.ansi {
144154
style.paint(text.as_ref()).to_string()

0 commit comments

Comments
 (0)