Skip to content

Commit

Permalink
Merge pull request #1440 from Enselic/fix-1438-newline-can-be-added-e…
Browse files Browse the repository at this point in the history
…ven-if-style-plain

Don't add artificial newline to last line if --style=plain
  • Loading branch information
sharkdp authored Dec 21, 2020
2 parents c1e4746 + 3e9afe2 commit cc7b89f
Show file tree
Hide file tree
Showing 19 changed files with 31 additions and 19 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

## Bugfixes

- only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
- If the last line doesn't end with a newline character, don't add it if `--style=plain`, see #1438 (@Enselic)
- Only print themes hint in interactive mode (`bat --list-themes`), see #1439 (@rsteube)
- Make ./tests/syntax-tests/regression_test.sh work on recent versions of macOS, see #1443 (@Enselic)

## Other
Expand Down
2 changes: 1 addition & 1 deletion src/preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
}
}
// line feed
'\x0A' => output.push('␊'),
'\x0A' => output.push_str("␊\x0A"),
// carriage return
'\x0D' => output.push('␍'),
// null
Expand Down
5 changes: 1 addition & 4 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ impl<'a> Printer for SimplePrinter<'a> {
if self.config.show_nonprintable {
let line = replace_nonprintable(line_buffer, self.config.tab_width);
write!(handle, "{}", line)?;
if line_buffer.last() == Some(&b'\n') {
writeln!(handle)?;
}
} else {
handle.write_all(line_buffer)?
};
Expand Down Expand Up @@ -463,7 +460,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
}
}

if line.bytes().next_back() != Some(b'\n') {
if !self.config.style_components.plain() && line.bytes().next_back() != Some(b'\n') {
writeln!(handle)?;
}
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,3 +813,17 @@ fn show_all_mode() {
.stdout("hello·world␊\n├──┤␍␀␇␈␛")
.stderr("");
}

#[test]
fn plain_mode_does_not_add_nonexisting_newline() {
bat()
.arg("--paging=never")
.arg("--color=never")
.arg("--decorations=always")
.arg("--style=plain")
.arg("single-line.txt")
.assert()
.success()
.stdout("Single Line");
}

2 changes: 1 addition & 1 deletion tests/syntax-tests/highlighted/Plaintext/plaintext.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@
\u{ad}␊
\u{ae}␊
␊
Here's·a·line·with·multiple·characters.
Here's·a·line·with·multiple·characters.␊
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/ActionScript/test.as
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ package TestSyntax {
var sndChannel:SoundChannel = mySound.play();
}
}
}
}
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/Batch/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ set LDLIBS= ^
@set "LINK_FILES=%LINK_FILES% %%~f"
)

lld-link.exe %LINK_FILES% -out:"%OUTPUT%" %LDFLAGS% %LDLIBS%
lld-link.exe %LINK_FILES% -out:"%OUTPUT%" %LDFLAGS% %LDLIBS%
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/Clojure/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
(println (factorial 5))
(log)
(log "Message"))


2 changes: 1 addition & 1 deletion tests/syntax-tests/source/Dockerfile/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ EXPOSE 80/tcp

VOLUME [/var/lib/mysql/data]

ENTRYPOINT ["/usr/bin/entrypoint.sh"]
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
*.patch -text

.gitattributes linguist-language=gitattributes
.gitkeep export-ignore
.gitkeep export-ignore
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/Git Config/text.gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
[user]
email = f.nord@example.com
name = Frank Nord
signingkey = AAAAAAAAAAAAAAAA
signingkey = AAAAAAAAAAAAAAAA
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/Hosts/hosts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
192.160.0.200 try.sample.test try #another comment
216.58.223.238 google.com

::1 localhost.try ip6-localhost
::1 localhost.try ip6-localhost
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/Makefile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,4 @@ install: all
@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME)

uninstall:
rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)}
rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)}
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/PHP/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ function setName($name) {

$ending = 2 > 3 ? "yep" : "nah";

?>
?>
Binary file modified tests/syntax-tests/source/Plaintext/plaintext.txt
Binary file not shown.
Binary file modified tests/syntax-tests/source/PowerShell/test.ps1
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/RequirementsTXT/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pywheels>=12.4 #a whitespace followed by comments
Nuitka<0.6.8.4
wxPython>=1.0, <=2.1

#this is another comment
#this is another comment
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/YAML/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ emails:
- bob@example.com
- bill@example.com
supervisors:
- george@example.com
- george@example.com
2 changes: 1 addition & 1 deletion tests/syntax-tests/source/reStructuredText/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,4 @@ blank lines before and after.)
..
So this block is not "lost",
despite its indentation.
despite its indentation.

0 comments on commit cc7b89f

Please sign in to comment.