From c3fc1b88fea5e4f4aee2e02ee7426180031a0081 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sat, 19 Dec 2020 10:21:53 +0100 Subject: [PATCH 1/3] replace_nonprintable: Keep \n around Since it has a functional role, we can not just replace it, we must keep it around. This also allows us to simplify the code slightly. We must fix this before we fix #1438 since otherwise the \n will be missing with --style=plain, since we will stop adding it if it is missing. --- src/preprocessor.rs | 2 +- src/printer.rs | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/preprocessor.rs b/src/preprocessor.rs index eb878890..7001ec88 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -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 diff --git a/src/printer.rs b/src/printer.rs index ba74872a..b240f83f 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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)? }; From 68d525cd8b350b40e2c990c70625eba4e82f746d Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Wed, 16 Dec 2020 19:22:31 +0100 Subject: [PATCH 2/3] Don't add artificial newline to last line if --style=plain This fixes #1438. Note however, that using a pager such as less will add a newline itself. So to actually not print a newline for such files, you need to either disable paging: bat --style=plain --paging=never no-newline-at-end-of-file.txt or use a "pager" that does not add a newline: bat --style=plain --pager=cat no-newline-at-end-of-file.txt Note that we also update syntax tests file since a bunch of them had missing newlines on the last lines. --- CHANGELOG.md | 2 ++ src/printer.rs | 2 +- .../highlighted/Plaintext/plaintext.txt | 2 +- .../syntax-tests/source/ActionScript/test.as | 2 +- tests/syntax-tests/source/Batch/build.bat | 2 +- tests/syntax-tests/source/Clojure/test.clj | 2 +- .../syntax-tests/source/Dockerfile/Dockerfile | 2 +- .../Git Attributes/example.gitattributes | 2 +- .../source/Git Config/text.gitconfig | 2 +- tests/syntax-tests/source/Hosts/hosts | 2 +- tests/syntax-tests/source/Makefile/Makefile | 2 +- tests/syntax-tests/source/PHP/test.php | 2 +- .../source/Plaintext/plaintext.txt | Bin 436 -> 437 bytes tests/syntax-tests/source/PowerShell/test.ps1 | Bin 1430 -> 1434 bytes .../source/RequirementsTXT/requirements.txt | 2 +- tests/syntax-tests/source/YAML/example.yaml | 2 +- .../source/reStructuredText/reference.rst | 2 +- 17 files changed, 16 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ef17a38..05540511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ## Bugfixes +- If the last line doesn't end with a newline character, don't add it if `--style=plain`, see #1438 (@Enselic) + ## Other ## Syntaxes diff --git a/src/printer.rs b/src/printer.rs index b240f83f..d588083e 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -460,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 { diff --git a/tests/syntax-tests/highlighted/Plaintext/plaintext.txt b/tests/syntax-tests/highlighted/Plaintext/plaintext.txt index 807728e1..1f27a79f 100644 --- a/tests/syntax-tests/highlighted/Plaintext/plaintext.txt +++ b/tests/syntax-tests/highlighted/Plaintext/plaintext.txt @@ -175,4 +175,4 @@ \u{ad}␊ \u{ae}␊ ␊ -Here's·a·line·with·multiple·characters. +Here's·a·line·with·multiple·characters.␊ diff --git a/tests/syntax-tests/source/ActionScript/test.as b/tests/syntax-tests/source/ActionScript/test.as index 1f7a1936..f1828bbc 100644 --- a/tests/syntax-tests/source/ActionScript/test.as +++ b/tests/syntax-tests/source/ActionScript/test.as @@ -72,4 +72,4 @@ package TestSyntax { var sndChannel:SoundChannel = mySound.play(); } } -} \ No newline at end of file +} diff --git a/tests/syntax-tests/source/Batch/build.bat b/tests/syntax-tests/source/Batch/build.bat index 56a262e2..acf9f7cd 100644 --- a/tests/syntax-tests/source/Batch/build.bat +++ b/tests/syntax-tests/source/Batch/build.bat @@ -56,4 +56,4 @@ set LDLIBS= ^ @set "LINK_FILES=%LINK_FILES% %%~f" ) -lld-link.exe %LINK_FILES% -out:"%OUTPUT%" %LDFLAGS% %LDLIBS% \ No newline at end of file +lld-link.exe %LINK_FILES% -out:"%OUTPUT%" %LDFLAGS% %LDLIBS% diff --git a/tests/syntax-tests/source/Clojure/test.clj b/tests/syntax-tests/source/Clojure/test.clj index ea24e822..b4010da1 100644 --- a/tests/syntax-tests/source/Clojure/test.clj +++ b/tests/syntax-tests/source/Clojure/test.clj @@ -55,4 +55,4 @@ (println (factorial 5)) (log) (log "Message")) - \ No newline at end of file + diff --git a/tests/syntax-tests/source/Dockerfile/Dockerfile b/tests/syntax-tests/source/Dockerfile/Dockerfile index 54a3b2c8..b38bce26 100644 --- a/tests/syntax-tests/source/Dockerfile/Dockerfile +++ b/tests/syntax-tests/source/Dockerfile/Dockerfile @@ -16,4 +16,4 @@ EXPOSE 80/tcp VOLUME [/var/lib/mysql/data] -ENTRYPOINT ["/usr/bin/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/usr/bin/entrypoint.sh"] diff --git a/tests/syntax-tests/source/Git Attributes/example.gitattributes b/tests/syntax-tests/source/Git Attributes/example.gitattributes index 9b8e58b8..8b3502b7 100644 --- a/tests/syntax-tests/source/Git Attributes/example.gitattributes +++ b/tests/syntax-tests/source/Git Attributes/example.gitattributes @@ -13,4 +13,4 @@ *.patch -text .gitattributes linguist-language=gitattributes -.gitkeep export-ignore \ No newline at end of file +.gitkeep export-ignore diff --git a/tests/syntax-tests/source/Git Config/text.gitconfig b/tests/syntax-tests/source/Git Config/text.gitconfig index 45de0fe5..47224f0b 100644 --- a/tests/syntax-tests/source/Git Config/text.gitconfig +++ b/tests/syntax-tests/source/Git Config/text.gitconfig @@ -104,4 +104,4 @@ [user] email = f.nord@example.com name = Frank Nord - signingkey = AAAAAAAAAAAAAAAA \ No newline at end of file + signingkey = AAAAAAAAAAAAAAAA diff --git a/tests/syntax-tests/source/Hosts/hosts b/tests/syntax-tests/source/Hosts/hosts index d7d4307d..e9d24c2d 100644 --- a/tests/syntax-tests/source/Hosts/hosts +++ b/tests/syntax-tests/source/Hosts/hosts @@ -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 \ No newline at end of file +::1 localhost.try ip6-localhost diff --git a/tests/syntax-tests/source/Makefile/Makefile b/tests/syntax-tests/source/Makefile/Makefile index 7acc5b2d..87379733 100644 --- a/tests/syntax-tests/source/Makefile/Makefile +++ b/tests/syntax-tests/source/Makefile/Makefile @@ -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)} \ No newline at end of file + rm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME)} diff --git a/tests/syntax-tests/source/PHP/test.php b/tests/syntax-tests/source/PHP/test.php index 26413597..1f0a581f 100644 --- a/tests/syntax-tests/source/PHP/test.php +++ b/tests/syntax-tests/source/PHP/test.php @@ -108,4 +108,4 @@ $doe->setName('John Doe'); $ending = 2 > 3 ? "yep" : "nah"; -?> \ No newline at end of file +?> diff --git a/tests/syntax-tests/source/Plaintext/plaintext.txt b/tests/syntax-tests/source/Plaintext/plaintext.txt index 42da1207d3c89516d0bc2968d328f229d1f5eb79..112ef7e923db5de2ac88034bbacf859bdad295fa 100644 GIT binary patch delta 9 QcmdnOyp?&w7Dh%c01?Ro-~a#s delta 7 OcmdnWyoGtg7DfOJ6#~@& diff --git a/tests/syntax-tests/source/PowerShell/test.ps1 b/tests/syntax-tests/source/PowerShell/test.ps1 index 434414df4059c3b1eac568be7515ce1c43aea629..315149a8dde78ec1d02444b3ce5e17965a66269f 100755 GIT binary patch delta 12 TcmbQnJ&Sw8G*%V{UIs1z7x4nE delta 7 OcmbQmJ&k+AG*$o#+5(aQ diff --git a/tests/syntax-tests/source/RequirementsTXT/requirements.txt b/tests/syntax-tests/source/RequirementsTXT/requirements.txt index 83a41dc8..83d71e0e 100644 --- a/tests/syntax-tests/source/RequirementsTXT/requirements.txt +++ b/tests/syntax-tests/source/RequirementsTXT/requirements.txt @@ -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 \ No newline at end of file +#this is another comment diff --git a/tests/syntax-tests/source/YAML/example.yaml b/tests/syntax-tests/source/YAML/example.yaml index 1b215705..d7b4c912 100644 --- a/tests/syntax-tests/source/YAML/example.yaml +++ b/tests/syntax-tests/source/YAML/example.yaml @@ -31,4 +31,4 @@ emails: - bob@example.com - bill@example.com supervisors: - - george@example.com \ No newline at end of file + - george@example.com diff --git a/tests/syntax-tests/source/reStructuredText/reference.rst b/tests/syntax-tests/source/reStructuredText/reference.rst index e07f54c4..5c42e540 100644 --- a/tests/syntax-tests/source/reStructuredText/reference.rst +++ b/tests/syntax-tests/source/reStructuredText/reference.rst @@ -317,4 +317,4 @@ blank lines before and after.) .. So this block is not "lost", - despite its indentation. \ No newline at end of file + despite its indentation. From 3e9afe297446e449f3b05df32d4731c78ee14ce3 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Mon, 21 Dec 2020 08:34:22 +0100 Subject: [PATCH 3/3] Add integration test for nonexisting newline --- tests/integration_tests.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index ac11efb4..d0217021 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -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"); +} +