* Use `localectl list-locales` if possible.
* Otherwise list `/usr/lib/locale` and try to use `localedef` to list
`/usr/lib/locale/locale-archive`.
* Do not bother with `/usr/share/locale` as, it turns out, it is only used
for localisations by third-party programs on Linux. Strangely, macOS
seems to have actual locale definitions in there, but this tool is
mostly aimed at Linux, since macOS installations are pretty uniform.
The old interface only allowed one to perform terminal detection on a
handle and change it to UTF-8 if it was not a terminal. Turns our, it
makes sense to sometimes leave the original encoding on a handle even if
it does not point to a terminal.
* Change the interface to separate these two use-cases.
* Extract main-wrappers to a new `Main` module.
Previously the code was using stdout for testing the terminal detection
functionality. However, there is no guarantee that the tests will always
be running with stdout attached to a terminal.
Create a fresh pseudo-terminal instead and use it instead of stdout.
* Split out the actual encoding choice logic into a separate Internal
module to make it possible to cover it with tests.
* Make this function a little more efficient compared to the previous
purely pure function.
* Keep a completely pure function as a specficiation for ease of reading
and for testing by comparing the actualy function to the pure one.
* Add tests that check that the pure function makes sense.
* Add tests that compare the output of the real function to the output
of the pure function on some real handles.
* Add tests that check that functions that are supposed to actually
modify the encoding of a handle actually do modify it.
This function allows one to not only safely set the best encoding on a
file descriptor, but also restore the previous one. The idea is that it
will be used on file descriptors that come from the outside and so we
don’t know how they were used and will be used after us.
The previous version of hSetEncoding would always set UTF-8.
This can screw things up if it is accidentally called, for example,
on a terminal handle or a handle in binary mode.
Change it to use `hSetBestUtf8Enc` internally so that it now follows the
same loginc in determining the encoding to set.