better control over encoding used in preview #2895
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ISSUE TYPE
RUNTIME ENVIRONMENT
CHECKLIST
CONTRIBUTING
document has been read [REQUIRED]DESCRIPTION
I have added two new settings:
preferred_encoding = utf-8
preferred_encoding_required_confidence = 0.5
preferred_encoding is used if chardet thinks it's plausible that the file to be previewed is encoded in preferred_encoding.
The required confidence can be configured with preferred_encoding_required_confidence.
Otherwise chardet's best guess is used.
This is based on a proposal by toonn but I have set a lower required confidence because 0.9 still lead to utf-8 files not being detected correctly (see erzoe@c430efc).
The previous behavior can be retained by setting preferred_encoding_required_confidence to a value greater than 1.
(I have also added a third setting
show_encoding
which I have used for testing.)MOTIVATION AND CONTEXT
Before there was no way to control which encoding ranger should use when displaying a text file in the preview.
Instead chardet was used to guess the file encoding which in many cases guessed a wrong encoding leading to non-ASCII characters not being displayed correctly.
see #1948
TESTING
I have tested that utf-8 files are now correclty recognized as utf-8 and that non-utf-8 files (1 euc-jp and 1 iso-20220jp file) are also correctly recognized.