Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding errors on Windows with certain languages. #115

Merged
merged 1 commit into from
Apr 14, 2023

Conversation

realRurichan
Copy link
Contributor

Description

When running hyfetch in Windows systems installed from images with certain languages (such as Simplified Chinese and Traditional Chinese), an encoding error may occur.

Error: 'utf-8' codec can't decode byte 0xb1 in position 12: invalid start byte
Traceback (most recent call last):
  File "C:\Users\ruri\AppData\Local\Programs\Python\Python311\Lib\site-packages\hyfetch\main.py", line 389, in run
    asc = get_distro_ascii() if not args.ascii_file else Path(args.ascii_file).read_text("utf-8")
          ^^^^^^^^^^^^^^^^^^
  File "C:\Users\ruri\AppData\Local\Programs\Python\Python311\Lib\site-packages\hyfetch\neofetch_util.py", line 315, in get_distro_ascii
    det = distro_detector.detect(distro or get_distro_name())
                                           ^^^^^^^^^^^^^^^^^
  File "C:\Users\ruri\AppData\Local\Programs\Python\Python311\Lib\site-packages\hyfetch\neofetch_util.py", line 336, in get_distro_name
    return run_neofetch_cmd('ascii_distro_name', True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ruri\AppData\Local\Programs\Python\Python311\Lib\site-packages\hyfetch\neofetch_util.py", line 296, in run_neofetch_cmd
    return check_output(full_cmd).decode().strip()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 12: invalid start byte

This is because the system name returned by the "wmic.exe os get Caption" command uses the "ISO-8859-1" encoding, which cannot encode Chinese characters. Regardless of how it is decoded, it will be garbled. Hyfetch itself uses utf-8 for decoding, but the error occurs due to inconsistent encoding.

Features

I switched to using the registry to obtain the system name in order to avoid compatibility issues on Windows systems installed from images with certain languages.

Issues

TODO

@hykilpikonna hykilpikonna merged commit 5c22c75 into hykilpikonna:master Apr 14, 2023
@hykilpikonna
Copy link
Owner

Great work, thanks!

@slackingfred
Copy link
Contributor

I want to add something about the cause of the original issue.

The wmic command uses a tricky check to determine which encoding to use: UTF-16 if writing to a file, or the OEM codepage (system locale) if writing to a terminal. (see here) When using bash's $() syntax to capture command output, wmic behaves as if stdout is a terminal. I can confirm that the output is in CP936, aka GBK, on a Simplified Chinese system.

I tried to integrate the chcp command to get UTF-16 output (see here), but it didn't work.

I think we need to replace every wmic command in neofetch to avoid further issues. Or implement a function to get the system locale, and interpret the output accordingly.

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.

3 participants