Commit Graph

66 Commits

Author SHA1 Message Date
Simon Michael
01e0c34b85 dev: Hledger.Utils.IO: getFlag helper, like getOpt 2025-07-17 07:51:05 -07:00
Simon Michael
fc93a4c644 fix:add: show green prompts properly instead of ANSI codes [#2410] 2025-07-01 16:41:05 -07:00
Simon Michael
16fd84166a ;dev: cleanup 2025-06-12 18:48:48 -10:00
Simon Michael
57031b0e78 dev: rename exitOnError -> handleExit, improve doc 2025-06-12 18:48:10 -10:00
Simon Michael
0750a27d00 dev: ignore SIGPIPE errors from truncated output in a more robust way [#2405] 2025-06-12 18:36:41 -10:00
Simon Michael
eb6b48dded fix:cli: don't raise an error if output is truncated in a pipe [#2405]
This broke in 1.43.

Now we ignore any IOException whose message contains "broken pipe".
Hopefully this matches pre-1.43 behaviour and doesn't hide real errors.
2025-06-12 17:55:13 -10:00
Simon Michael
713773cfc3 imp: simplify the unicode decode error message; refactor 2025-05-30 09:43:30 -10:00
Simon Michael
4587eccdf4 ;fix: recognise another case of text decoding failure 2025-05-29 12:25:06 -10:00
Simon Michael
8f6a7c8a66 fix: web: don't hang when saving a large file [#2319] 2025-05-16 09:44:26 -10:00
Simon Michael
b0959cb23d lib:Hledger.Utils.IO: document some file reading utilities 2025-04-25 09:21:11 -10:00
Simon Michael
45d5fd7963 imp: show a clearer error message on unicode en/decoding failures [#73]
A very long-awaited improvement: for unicode exceptions, and I/O
exceptions which look like they were caused by a unicode error
(usually text decoding failure), our error message now includes
an explanation and advice on what to do.

Currently this uses the GHC.IO.Encoding API, which is not ideal:
"The API of this module is unstable and not meant to be consumed by
the general public. If you absolutely must depend on it, make sure to
use a tight upper bound, e.g., base < 4.X rather than base < 5,
because the interface can change rapidly without much warning."

Also it relies on scanning for patterns in GHC's various
unicode-related error messages, which may not be complete and could
change in future. To do: try the encoding package's IO helpers,
perhaps they give more specific exceptions.
2025-04-25 08:57:07 -10:00
Simon Michael
06cd0f9472 lib: exitOnExceptions -> exitOnError 2025-04-25 08:32:47 -10:00
Simon Michael
036be45c50 lib: make parseYN(A) total; export findPager 2025-04-23 21:43:30 -10:00
Simon Michael
f37a4a7dcb lib: refactor, export getHomeSafe 2025-04-20 10:52:50 -10:00
Simon Michael
9340b73aae imp: improve/format errors for various failures [#2367]
These now call error' and show errors in the standard style:

- reading a nonexistent data file
- reading an unsafe dotted file name on windows
- web: using --socket on windows
- demo: demo not found
- demo: error while running asciinema
- diff: bad arguments
- print --match: no match found
- register --match: no match found
- roi: no investment transactions found
2025-04-11 08:06:47 -10:00
Simon Michael
454c669fe4 fix:cli,ui,web: consistent console error output independent of GHC version [#2367]
Hledger.Utils.IO helpers have been updated and new ones have been
added (exitOnExceptions, exitWithError) to allow consistent display of
program errors whether compiled with GHC <9.10, GHC 9.10, or GHC >9.10.
The trailing newlines added by GHC 9.10 are gone,
and so is the "uncaught exception" output added by GHC 9.12.
2025-04-11 08:06:47 -10:00
Simon Michael
cb373447db fix:cli:paging: support older less, don't pass --use-color [#2335]
Older less versions (eg 551) require --use-colour, and break if --use-color is passed. So we no longer attempt to enable colour in less by default.
2025-03-04 16:38:25 -10:00
Simon Michael
d346e56b79 dev: fix warnings, update yaml file, edit [#2340] 2025-02-27 13:41:48 -10:00
gesh
a24c39f13f fix: Use POSIX API to compute tty width (#2332)
On some systems, TERM is set to a value that doesn't have a valid
terminfo entry. Rather than hackily fall back on a value for TERM that
appears to work in most contexts (TERM=dumb) but which isn't guaranteed
anywhere to be valid, use proper POSIX ioctls to get the tty width.

This has the added bonus of also working on Windows.

In fact, we already settled on computing the terminal size in this way
in hledger-lib, so this commit centralizes the choice of the logic
there.

Also added a note for alternative methods and their tradeoffs, in case
this turns out to be fragile on some systems.
2025-02-27 13:41:48 -10:00
Joschua Kesper
5114962b2a feat:csv: add an encoding rule, allowing non-UTF8 CSV to be read [#2319]
Previously, hledger could read CSV files containing non-ascii
characters only if they are UTF8-encoded.  Now there is a new CSV
rule, encoding ENCODING, which allows reading CSV files with other
encodings.

This adds a dependency on the encoding library, which supports fewer
encodings than text-icu but does not require a third-party C library.
To avoid build issues on various platforms, we require version 0.10+.

This adds some use of the ImplicitParams language extension, required
by encoding's API, but only in a small code region.

This also changes the type of Reader's rReadFn; it now takes
a `Handle` rather than a `Text`, allowing more flexibility.
2025-02-15 14:48:30 -10:00
Simon Michael
aefa4e8f20 imp:cli: show first line of error messages in red
error' and usageError now redden and bolden the first line of error
messages, when ANSI codes are supported and permitted.
And warn goldens and boldens the first line of warning messages.
2024-12-20 08:51:38 -10:00
Simon Michael
05ae105b42 dev: Hledger.Utils.IO: warn 2024-12-03 16:07:54 -10:00
Simon Michael
5d2259d942 dev: Hledger.Utils.IO: cleanup 2024-11-02 12:59:44 -10:00
Simon Michael
1dabccfb46 dev: Hledger.Utils.IO: more cleanup 2024-11-02 12:51:53 -10:00
Simon Michael
4351304f06 dev: Hledger.Utils.IO: cleanup 2024-11-02 12:04:19 -10:00
Simon Michael
9c81bb2a06 dev: Hledger.Utils.IO colour helpers: cleanup 2024-11-02 11:54:28 -10:00
Simon Michael
657fc1551f !fix: respect --color=yes in a few places that didn't; drop text-ansi dep
Hledger.Utils.IO's ansi style/color helpers now respect --color=yes,
so that eg `hledger --color=yes | less -R` shows bold headings as
you'd expect.

Hledger.Utils.IO.rgb' now takes Float arguments instead of Word8.
2024-11-01 10:40:25 -10:00
Simon Michael
234b4879c2 imp: less pager will use HLEDGER_LESS instead, if defined
When using `less` as pager, if HLEDGER_LESS is defined, it will
provide the options (instead of LESS + hledger's extra options).
You can set your own preferred options here, or you can set it
equal to LESS to use exactly those options.

[#2272]-related
2024-11-01 08:02:03 -10:00
Simon Michael
5bab9600e4 imp: simplify pager setup: just customise LESS, not MORE
[#2272]-related
2024-11-01 08:01:44 -10:00
Simon Michael
1011029356 imp: pager set extra LESS options for a nice experience when using less
Currently this sets a rather ambitious set of options,
and overrides the existing LESS:

  --chop-long-lines
  --hilite-unread
  --ignore-case
  --mouse
  --no-init
  --QUIT-AT-EOF
  --quit-if-one-screen
  --RAW-CONTROL-CHARS
  --squeeze-blank-lines
  --use-backslash
  --use-color
2024-10-31 19:13:39 -10:00
Simon Michael
30086ae249 imp: run pager more robustly; drop pager lib [#2272]
This fixes the error displayed when quitting the pager with long output.
It also replaces the pager lib with more robust homegrown pager utilities,
which should prevent a number of failure modes.
2024-10-31 18:55:09 -10:00
Simon Michael
f8cc9cdb4d ;dev: document runPager's behaviour precisely 2024-10-20 08:10:07 -10:00
Simon Michael
f5b10b2d40 dev: cleanups 2024-10-19 16:56:14 -10:00
Simon Michael
b940254025 imp:cli: detect --color more safely in most cases
--color now also works in a config file, like --pager, except for two
cases: it does not affect colouring of debug output, or the colouring
helpers used in the check recentassertions error message.
2024-10-19 16:28:36 -10:00
Simon Michael
711d921774 imp:cli: --no-pager -> --pager, make this and --color safe options 2024-10-19 16:28:36 -10:00
Simon Michael
3e48451c3f imp: note unsafe args / config file limitation; make --no-pager safe 2024-10-19 16:28:36 -10:00
Simon Michael
ba786752d1 imp📟 add a --no-pager general flag 2024-10-19 16:28:36 -10:00
Simon Michael
db748465a8 fix📟 when the $PAGER is not in PATH, print instead of failing 2024-10-19 16:28:36 -10:00
Simon Michael
4441680d7b fix:lib:Hledger.Utils.IO.pager: avoid a crash; avoid an extra newline 2024-10-19 16:28:36 -10:00
Simon Michael
3b7420b97c feat: use a pager for all large terminal output, not just for help 2024-10-19 16:28:36 -10:00
Simon Michael
49c4ccd0b7 dev: Hledger.Utils.IO: fix hlint warnings 2024-06-25 07:45:27 +01:00
Simon Michael
1fe7e7af8c imp: cli: --color now also accepts y or n, like --pretty 2024-05-29 09:37:20 -10:00
Simon Michael
e6b3fa0f54 dev:IO: comments 2024-02-22 07:32:10 -10:00
Simon Michael
d1635a55f8 lib: move readFileStrictly to Hledger.Utils.IO 2023-12-07 03:41:31 -10:00
Simon Michael
7899b992b7 fix: prevent a Glob/filemanip package conflict in IO.hs 2023-12-05 17:43:04 -10:00
Simon Michael
ddae3af8a3 lib: Hledger.Utils.IO: added expandGlob, sortByModTime 2023-05-19 09:09:21 -10:00
Simon Michael
3d2ef21081 lib: Hledger.Utils.IO: more robust monochrome pretty/debug printing 2023-05-18 07:19:36 -10:00
Simon Michael
83de76b67a lib: Hledger.Utils.IO: avoid excessively wide "compact" prettyprinting 2023-05-18 07:19:36 -10:00
Simon Michael
1de8600067 imp: cli: try to ensure less (and its more mode) show ANSI (fix #2015)
If you use some other $PAGER, you will have to configure it to show
ANSI yourself (or disable ANSI, eg by setting NO_COLOR=1).
2023-04-06 11:13:00 -10:00
Simon Michael
c661fa7763 dev: lib: refactor pager 2023-04-06 11:12:56 -10:00