Commit 9e40b49e5e

9e40b49e5ee761825bd07fc64e02531d1a389e69

parent: 33d3407694

Unregistered key

VAWVAW <git@nlih.de> · 2025-06-17 01:30 UTC
committer: <noreply@github.com>

Fix display of tabs in code blocks (#463)

Layout: unified · split

docs/iamb.5 +4
@@ -231,6 +231,10 @@ Possible values are
231231Specify the width of the column where usernames are displayed in a room.
232232Usernames that are too long are truncated.
233233Defaults to 30.
234
235.It Sy tabstop
236Number of spaces that a <Tab> counts for.
237Defaults to 4.
234238.El
235239
236240.Ss Example 1: Avoid showing Emojis (useful for terminals w/o support)
src/config.rs +4
@@ -574,6 +574,7 @@ pub struct TunableValues {
574574 pub image_preview: Option<ImagePreviewValues>,
575575 pub user_gutter_width: usize,
576576 pub external_edit_file_suffix: String,
577 pub tabstop: usize,
577578}
578579
579580#[derive(Clone, Default, Deserialize)]
@@ -600,6 +601,7 @@ pub struct Tunables {
600601 pub image_preview: Option<ImagePreview>,
601602 pub user_gutter_width: Option<usize>,
602603 pub external_edit_file_suffix: Option<String>,
604 pub tabstop: Option<usize>,
603605}
604606
605607impl Tunables {
@@ -632,6 +634,7 @@ impl Tunables {
632634 external_edit_file_suffix: self
633635 .external_edit_file_suffix
634636 .or(other.external_edit_file_suffix),
637 tabstop: self.tabstop.or(other.tabstop),
635638 }
636639 }
637640
@@ -660,6 +663,7 @@ impl Tunables {
660663 external_edit_file_suffix: self
661664 .external_edit_file_suffix
662665 .unwrap_or_else(|| ".md".to_string()),
666 tabstop: self.tabstop.unwrap_or(4),
663667 }
664668 }
665669}
src/message/html.rs +18 −3
@@ -1415,13 +1415,14 @@ pub mod tests {
14151415 let s = concat!(
14161416 "<pre><code class=\"language-rust\">",
14171417 "fn hello() -&gt; usize {\n",
1418 " \t// weired\n",
14181419 " return 5;\n",
14191420 "}\n",
14201421 "</code></pre>\n"
14211422 );
14221423 let tree = parse_matrix_html(s);
14231424 let text = tree.to_text(25, Style::default(), true, &settings);
1424 assert_eq!(text.lines.len(), 5);
1425 assert_eq!(text.lines.len(), 6);
14251426 assert_eq!(
14261427 text.lines[0],
14271428 Line::from(vec![
@@ -1452,6 +1453,20 @@ pub mod tests {
14521453 );
14531454 assert_eq!(
14541455 text.lines[2],
1456 Line::from(vec![
1457 Span::raw(line::VERTICAL),
1458 Span::raw(" "),
1459 Span::raw(" "),
1460 Span::raw("/"),
1461 Span::raw("/"),
1462 Span::raw(" "),
1463 Span::raw("weired"),
1464 Span::raw(" "),
1465 Span::raw(line::VERTICAL)
1466 ])
1467 );
1468 assert_eq!(
1469 text.lines[3],
14551470 Line::from(vec![
14561471 Span::raw(line::VERTICAL),
14571472 Span::raw(" "),
@@ -1464,7 +1479,7 @@ pub mod tests {
14641479 ])
14651480 );
14661481 assert_eq!(
1467 text.lines[3],
1482 text.lines[4],
14681483 Line::from(vec![
14691484 Span::raw(line::VERTICAL),
14701485 Span::raw("}"),
@@ -1473,7 +1488,7 @@ pub mod tests {
14731488 ])
14741489 );
14751490 assert_eq!(
1476 text.lines[4],
1491 text.lines[5],
14771492 Line::from(vec![
14781493 Span::raw(line::BOTTOM_LEFT),
14791494 Span::raw(line::HORIZONTAL.repeat(23)),
src/message/printer.rs +9 −1
@@ -216,6 +216,8 @@ impl<'a> TextPrinter<'a> {
216216 return;
217217 }
218218
219 let tabstop = self.settings().tunables.tabstop;
220
219221 for mut word in UnicodeSegmentation::split_word_bounds(s) {
220222 if let "\n" | "\r\n" = word {
221223 if self.literal {
@@ -232,11 +234,17 @@ impl<'a> TextPrinter<'a> {
232234 continue;
233235 }
234236
235 let cow = if self.emoji_shortcodes() {
237 let mut cow = if self.emoji_shortcodes() {
236238 Cow::Owned(replace_emojis_in_str(word))
237239 } else {
238240 Cow::Borrowed(word)
239241 };
242
243 if cow == "\t" {
244 let tablen = tabstop - (self.curr_width % tabstop);
245 cow = Cow::Owned(" ".repeat(tablen));
246 }
247
240248 let sw = UnicodeWidthStr::width(cow.as_ref());
241249
242250 if sw > self.width {
src/tests.rs +1
@@ -198,6 +198,7 @@ pub fn mock_tunables() -> TunableValues {
198198 },
199199 image_preview: None,
200200 user_gutter_width: 30,
201 tabstop: 4,
201202 }
202203}
203204