Skip to content

Fix encoding error on Windows when not outputting to terminal#143

Open
ryanbuck182 wants to merge 8 commits intodduan:mainfrom
ryanbuck182:fix/windows-special-characters
Open

Fix encoding error on Windows when not outputting to terminal#143
ryanbuck182 wants to merge 8 commits intodduan:mainfrom
ryanbuck182:fix/windows-special-characters

Conversation

@ryanbuck182
Copy link
Copy Markdown

Closes #136

On Windows, when piping the command output somewhere else, it would cause an encoding error where the tree lines would be encoded as undesired special characters. The built-in tree command on Windows gets around this by automatically switching to an ASCII version of the tree. This PR makes a similar change to tre.

Now, on Windows, the output of something like tre | clip will be something like:

\-- example
    |-- Cargo.lock
    |-- Cargo.toml
    \-- src
        \-- main.rs

Instead of:

└── example
    ├── Cargo.lock
    ├── Cargo.toml
    └── src
        └── main.rs

This successfully avoids the encoding errors.

Full List of Changes:

  • src/diagram_formatting.rs
    • added Charset enum for different sets of characters to render the diagram with (currently just Unicode and ASCII)
    • added PrefixSegment::as_str() method to return the appropriate string based on the Charset
    • make_prefix, format_file, and format_paths now accept a Charset parameter
    • updated formatting_works test to work with new function signatures
    • added formatting_ascii unit test for formatting with ASCII
    • extracted some shared code in both tests to a test_input() helper
  • src/tre.rs
    • added check in run function that switches the Charset if on Windows and not outputting to a TTY
  • tests/integration_tests.rs
    • added has_entry() helper so the tests check for the correct output characters (ASCII on Windows, Unicode otherwise)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not displaying correct characters when redirecting on Windows

1 participant