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
231Specify the width of the column where usernames are displayed in a room. 231Specify the width of the column where usernames are displayed in a room.
232Usernames that are too long are truncated. 232Usernames that are too long are truncated.
233Defaults to 30. 233Defaults to 30.
234
235.It Sy tabstop
236Number of spaces that a <Tab> counts for.
237Defaults to 4.
234.El 238.El
235 239
236.Ss Example 1: Avoid showing Emojis (useful for terminals w/o support) 240.Ss Example 1: Avoid showing Emojis (useful for terminals w/o support)
src/config.rs +4
@@ -574,6 +574,7 @@ pub struct TunableValues {
574 pub image_preview: Option<ImagePreviewValues>, 574 pub image_preview: Option<ImagePreviewValues>,
575 pub user_gutter_width: usize, 575 pub user_gutter_width: usize,
576 pub external_edit_file_suffix: String, 576 pub external_edit_file_suffix: String,
577 pub tabstop: usize,
577} 578}
578 579
579#[derive(Clone, Default, Deserialize)] 580#[derive(Clone, Default, Deserialize)]
@@ -600,6 +601,7 @@ pub struct Tunables {
600 pub image_preview: Option<ImagePreview>, 601 pub image_preview: Option<ImagePreview>,
601 pub user_gutter_width: Option<usize>, 602 pub user_gutter_width: Option<usize>,
602 pub external_edit_file_suffix: Option<String>, 603 pub external_edit_file_suffix: Option<String>,
604 pub tabstop: Option<usize>,
603} 605}
604 606
605impl Tunables { 607impl Tunables {
@@ -632,6 +634,7 @@ impl Tunables {
632 external_edit_file_suffix: self 634 external_edit_file_suffix: self
633 .external_edit_file_suffix 635 .external_edit_file_suffix
634 .or(other.external_edit_file_suffix), 636 .or(other.external_edit_file_suffix),
637 tabstop: self.tabstop.or(other.tabstop),
635 } 638 }
636 } 639 }
637 640
@@ -660,6 +663,7 @@ impl Tunables {
660 external_edit_file_suffix: self 663 external_edit_file_suffix: self
661 .external_edit_file_suffix 664 .external_edit_file_suffix
662 .unwrap_or_else(|| ".md".to_string()), 665 .unwrap_or_else(|| ".md".to_string()),
666 tabstop: self.tabstop.unwrap_or(4),
663 } 667 }
664 } 668 }
665} 669}
src/message/html.rs +18 −3
@@ -1415,13 +1415,14 @@ pub mod tests {
1415 let s = concat!( 1415 let s = concat!(
1416 "<pre><code class=\"language-rust\">", 1416 "<pre><code class=\"language-rust\">",
1417 "fn hello() -&gt; usize {\n", 1417 "fn hello() -&gt; usize {\n",
1418 " \t// weired\n",
1418 " return 5;\n", 1419 " return 5;\n",
1419 "}\n", 1420 "}\n",
1420 "</code></pre>\n" 1421 "</code></pre>\n"
1421 ); 1422 );
1422 let tree = parse_matrix_html(s); 1423 let tree = parse_matrix_html(s);
1423 let text = tree.to_text(25, Style::default(), true, &settings); 1424 let text = tree.to_text(25, Style::default(), true, &settings);
1424 assert_eq!(text.lines.len(), 5); 1425 assert_eq!(text.lines.len(), 6);
1425 assert_eq!( 1426 assert_eq!(
1426 text.lines[0], 1427 text.lines[0],
1427 Line::from(vec![ 1428 Line::from(vec![
@@ -1452,6 +1453,20 @@ pub mod tests {
1452 ); 1453 );
1453 assert_eq!( 1454 assert_eq!(
1454 text.lines[2], 1455 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],
1455 Line::from(vec![ 1470 Line::from(vec![
1456 Span::raw(line::VERTICAL), 1471 Span::raw(line::VERTICAL),
1457 Span::raw(" "), 1472 Span::raw(" "),
@@ -1464,7 +1479,7 @@ pub mod tests {
1464 ]) 1479 ])
1465 ); 1480 );
1466 assert_eq!( 1481 assert_eq!(
1467 text.lines[3], 1482 text.lines[4],
1468 Line::from(vec![ 1483 Line::from(vec![
1469 Span::raw(line::VERTICAL), 1484 Span::raw(line::VERTICAL),
1470 Span::raw("}"), 1485 Span::raw("}"),
@@ -1473,7 +1488,7 @@ pub mod tests {
1473 ]) 1488 ])
1474 ); 1489 );
1475 assert_eq!( 1490 assert_eq!(
1476 text.lines[4], 1491 text.lines[5],
1477 Line::from(vec![ 1492 Line::from(vec![
1478 Span::raw(line::BOTTOM_LEFT), 1493 Span::raw(line::BOTTOM_LEFT),
1479 Span::raw(line::HORIZONTAL.repeat(23)), 1494 Span::raw(line::HORIZONTAL.repeat(23)),
src/message/printer.rs +9 −1
@@ -216,6 +216,8 @@ impl<'a> TextPrinter<'a> {
216 return; 216 return;
217 } 217 }
218 218
219 let tabstop = self.settings().tunables.tabstop;
220
219 for mut word in UnicodeSegmentation::split_word_bounds(s) { 221 for mut word in UnicodeSegmentation::split_word_bounds(s) {
220 if let "\n" | "\r\n" = word { 222 if let "\n" | "\r\n" = word {
221 if self.literal { 223 if self.literal {
@@ -232,11 +234,17 @@ impl<'a> TextPrinter<'a> {
232 continue; 234 continue;
233 } 235 }
234 236
235 let cow = if self.emoji_shortcodes() { 237 let mut cow = if self.emoji_shortcodes() {
236 Cow::Owned(replace_emojis_in_str(word)) 238 Cow::Owned(replace_emojis_in_str(word))
237 } else { 239 } else {
238 Cow::Borrowed(word) 240 Cow::Borrowed(word)
239 }; 241 };
242
243 if cow == "\t" {
244 let tablen = tabstop - (self.curr_width % tabstop);
245 cow = Cow::Owned(" ".repeat(tablen));
246 }
247
240 let sw = UnicodeWidthStr::width(cow.as_ref()); 248 let sw = UnicodeWidthStr::width(cow.as_ref());
241 249
242 if sw > self.width { 250 if sw > self.width {
src/tests.rs +1
@@ -198,6 +198,7 @@ pub fn mock_tunables() -> TunableValues {
198 }, 198 },
199 image_preview: None, 199 image_preview: None,
200 user_gutter_width: 30, 200 user_gutter_width: 30,
201 tabstop: 4,
201 } 202 }
202} 203}
203 204