Repository: mitsuhiko/insta (language: rust)

Issue:
Newline gets escaped in `assert_snapshot` if the string contains '\t'
### What happened?

#713 changes the behaviour of `assert_snapshot` so that whenever there are control characters in the string, it gets printed using `format!` which will instead of literally.
When the string contains just `\n` and `\t`, this makes the diff [less readable](https://github.com/jj-vcs/jj/pull/5540/files/01502cb2ac687453cdf4db8e7bc373ab00afedae#r1938256985):
```rs
insta::assert_snapshot!(stdout, @"immutable_bookmark\timmutable\nmutable_bookmark\tmutable\nk\tworking_copy\ny\tmutable\nq\timmutable\nzq\tremote_commit\nzz\t(no description set)\nremote_bookmark@origin\tremote_commit\nalias_with_newline\t roots(\nsiblings\t@-+ ~@");
```

This could be fixed by treating `\t` as `\n` in `has_control_chars`:
https://github.com/mitsuhiko/insta/pull/713/files#diff-2666420e8389f1d842ce108018b05f3f68fa4b920f8c1793a3d4d01f21443544R696-R698

---

Personally I would go even further and prefer the old handling for  `\x1b` (used for ANSI escape codes) as well.
Here's the diff for applying insta 1.42.1 on [jj](https://github.com/jj-vcs/jj) https://github.com/jakobhellermann/jj/commit/7fdb9784e27f02aa9d676b16c09f3f400240fc95 and in my opinion it is much less readable.

<details>

![in vscode](https://github.com/user-attachments/assets/ad907268-fb5d-4d2d-9966-327ca0246d79)

![in vim](https://github.com/user-attachments/assets/a8b82544-c9bc-4657-884b-5859585b548a)
</details>

### Reproduction steps

_No response_

### Insta Version

1.42.1

### rustc Version

rustc 1.85.0-nightly (45d11e51b 2025-01-01)

### What did you expect?

_No response_


Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a1895834..dd08914b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
 
 All notable changes to insta and cargo-insta are documented here.
 
+## 1.42.2
+
+- Stop `\t` and `\x1b` (ANSI color escape) from causing snapshots to be escaped.  #715
+
 ## 1.42.1
 
 - Improved handling of control characters in inline snapshots.  #713
diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs
index 1d01d517..8b72a686 100644
--- a/insta/src/snapshot.rs
+++ b/insta/src/snapshot.rs
@@ -691,11 +691,10 @@ impl TextSnapshotContents {
 
         // Some characters can't be escaped in a raw string literal, so we need
         // to escape the string if it contains them. We prefer escaping control
-        // characters which except for newlines, which we prefer to see as
-        // actual newlines.
+        // characters except for newlines, tabs, and ESC.
         let has_control_chars = contents
             .chars()
-            .any(|c| c != '\n' && c.is_control() || c == '\0');
+            .any(|c| c.is_control() && !['\n', '\t', '\x1b'].contains(&c));
 
         // We prefer raw strings for strings containing a quote or an escape
         // character, and for strings containing newlines (which reduces diffs).
@@ -987,13 +986,15 @@ b
     // Test control and special characters
     assert_eq!(
         TextSnapshotContents::new("a\tb".to_string(), TextSnapshotKind::Inline).to_inline(0),
-        r##""a\tb""##
+        r##""a	b""##
     );
 
     assert_eq!(
         TextSnapshotContents::new("a\t\nb".to_string(), TextSnapshotKind::Inline).to_inline(0),
-        // No block mode for control characters
-        r##""a\t\nb""##
+        r##"r"
+a	
+b
+""##
     );
 
     assert_eq!(


Test patch: