72 Commits

Author SHA1 Message Date
Andrew Kelley
64c6a5092c std.Progress: elide root node if empty
when the root progress node has a zero length name, the sub-tree is
flattened one layer, reducing visual noise, as well as bytes written to
the terminal.
2024-05-27 20:56:48 -07:00
Andrew Kelley
dc3a192ae8 std.Progress: count newlines more accurately
Split newline_count into written_newline_count and
accumulated_newline_count. This handle the case when the tryLock() fails
to obtain the lock, because in such case there would not be any newlines
written to the terminal but the system would incorrectly think there
were. Now, written_newline_count is only adjusted when the write() call
succeeds.

Furthermore, write() call failure is handled by exiting the update
thread.
2024-05-27 20:56:48 -07:00
Andrew Kelley
6145819c0b std.Progress: handle when terminal write buffer too small 2024-05-27 20:56:48 -07:00
Andrew Kelley
52ffdec74b std.Progress: keep cursor on newline
Don't truncate trailing newline. This better handles stray writes to
stderr that are not std.Progress-aware, such as from non-zig child
processes.

This commit also makes `Node.start` and `Node.end` bail out early with a
comptime branch when it is known the target will not be spawning an
update thread.
2024-05-27 20:56:48 -07:00
Andrew Kelley
0ca2b4e0f1 std.Progress: use std.log.debug rather than warn
when the errors could possibly be spammed many times
2024-05-27 20:56:48 -07:00
Andrew Kelley
ea7d8ec147 std.Progress: smaller type for parents and robustify
Switch Node.Parent, Node.Index, and Node.OptionalIndex to be backed by
u8 rather than u16. This works fine since we use 200 as the preallocated
node buffer. This has the nice property that scanning the entire parents
array for allocated nodes fits in 4 cache lines, even if we bumped the
200 up to 254 (leaving room for the two special states).

The thread that reads progress updates from the pipe now handles short
reads by ignoring messages that are sent in multiple reads.

When checking the terminal size, if there is a failure, fall back to a
conservative guess of 80x25 rather than panicking. A debug message is
also emitted which would be displayed only in a debug build.
2024-05-27 20:56:48 -07:00
Andrew Kelley
11f894702b std.Progress: avoid scrolling the PS1 off the terminal 2024-05-27 20:56:48 -07:00
Andrew Kelley
52ed54d1e7 std.Progress: truncate IPC data exceeding preallocated buffers
This accomplishes 2 things simultaneously:

1. Don't trust child process data; if the data is outside the expected
   range, ignore the data.
2. If there is too much data to fit in the preallocated buffers, drop
   the data.
2024-05-27 20:56:48 -07:00
Andrew Kelley
807b613f71 std.Progress: move more global preallocations to thread memory
Same idea as previous commit
2024-05-27 20:56:48 -07:00
Andrew Kelley
7fe72d560d std.Progress: move global preallocations to thread memory
Instead of making static buffers configurable, let's pick strong
defaults and then use the update thread's stack memory to store the
preallocations. The thread uses a fairly shallow stack so this memory is
otherwise unused. This also makes the data section of the executable
smaller since it runtime allocates the memory when a `std.Progress`
instance is allocated, and in the case that the process is not connected
to a terminal, it never allocates the memory.
2024-05-27 20:56:48 -07:00
Andrew Kelley
e8907f9e9c std.Progress: correct the top level doc comments 2024-05-27 20:56:48 -07:00
Jacob Young
d77f5e7aaa Progress: fix compile errors on windows
Works for `zig build-exe`, IPC still not implemented yet.
2024-05-27 20:56:48 -07:00
Andrew Kelley
ca03c9c512 std.Progress: fix race condition with IPC nodes
It stored some metadata into the canonical node storage data but that is
a race condition because another thread recycles those nodes.

Also, keep the parent name for empty child root node names.
2024-05-27 20:56:48 -07:00
Andrew Kelley
516366f78f std.Progress: skip printing root node when it is empty 2024-05-27 20:56:48 -07:00
Andrew Kelley
70e39c1a20 std.Progress: fixes
* bump default statically allocated resources
* debug help when multiple instances of std.Progress are initialized
* only handle sigwinch on supported operating systems
* handle when reading from the pipe returns 0 bytes
* avoid printing more lines than rows
2024-05-27 20:56:48 -07:00
Andrew Kelley
f97c2f28fd update the codebase for the new std.Progress API 2024-05-27 20:56:48 -07:00
Andrew Kelley
f6873c6b00 std.Progress: fix using saved IPC data
also fix handling of BrokenPipe

also fix continuing wrong loop in error conditions
2024-05-27 20:56:48 -07:00
Andrew Kelley
3a768bd6ce std.Progress: save a copy of IPC data
so that the previous message can be used when the pipe is empty.

prevents flickering
2024-05-27 20:56:48 -07:00
Andrew Kelley
df46f5af69 std.Progress: include subtrees from child processes 2024-05-27 20:56:48 -07:00
Andrew Kelley
f07116404a std.Progress: child process sends updates via IPC 2024-05-27 20:56:48 -07:00
Andrew Kelley
ed36470af1 std.Progress: truncate trailing newline 2024-05-27 20:56:48 -07:00
Andrew Kelley
67e08e7b3c fix clearing and sibling iteration 2024-05-27 20:56:48 -07:00
Andrew Kelley
582acdf721 keep the cursor at the end instead of beginning 2024-05-27 20:56:48 -07:00
Andrew Kelley
66c3b6ac65 fix terminal repainting
the clear, save, restore thing doesn't work when the terminal is at the
bottom
2024-05-27 20:56:48 -07:00
Andrew Kelley
a3c9511ab9 rework std.Progress again
This time, we preallocate a fixed set of nodes and have the user-visible
Node only be an index into them. This allows for lock-free management of
the node storage.

Only the parent indexes are stored, and the update thread makes a
serialized copy of the state before trying to compute children lists.

The update thread then walks the tree and outputs an entire tree of
progress rather than only one line.

There is a problem with clearing from the cursor to the end of the
screen when the cursor is at the bottom of the terminal.
2024-05-27 20:56:48 -07:00
Andrew Kelley
e1e4de2776 progress progress
Move the mutex into the nodes

Track the whole tree instead of only recently activated node
2024-05-27 20:56:48 -07:00
Andrew Kelley
d6e8ba3f97 start reworking std.Progress
New design ideas:
* One global instance, don't try to play nicely with other instances
  except via IPC.
* One process owns the terminal and the other processes communicate via
  IPC.
* Clear the whole terminal and use multiple lines.

What's implemented so far:
* Query the terminal for size.
* Register a SIGWINCH handler.
* Use a thread for redraws.

To be done:
* IPC
* Handling single threaded targets
* Porting to Windows
* More intelligent display of the progress tree rather than only using
  one line.
2024-05-27 20:56:48 -07:00
Tristan Ross
6067d39522
std.builtin: make atomic order fields lowercase 2024-03-11 07:09:10 -07:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
JustinWayland
b37fdf387a Fix minor typo in the description of Progress 2023-10-13 00:44:30 +03:00
mlugg
06e50e9aa7
std.Progress: add optional unit to progress indicator 2023-07-25 13:51:22 +01:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
7db74009db std.Progress.Node: add a setName method
This can be used to update an existing node's label rather than indicate
that more things have been accomplished.
2023-03-15 10:48:14 -07:00
Andrew Kelley
7ebaa05bb1 std.Progress: add lock_stderr and unlock_stderr
API users can take advantage of these to freely write to the terminal
which has an ongoing progress display, similar to what Ninja does when
compiling C/C++ objects and a warning or error message is printed.
2023-03-15 10:48:12 -07:00
r00ster91
d8b4588d5f fix(terminal): handle some possible errors and resolve TODOs 2022-12-13 09:11:30 +01:00
Andrew Kelley
1952dd6437 Revert recent std.Progress implementation changes
I have noticed this causing my terminal to stop accepting input
sometimes. The previous implementation with all of its flaws was better
in the sense that it never caused this to happen.

This commit has multiple reverts in it:

Revert "Merge pull request #13148 from r00ster91/progressfollowup"

This reverts commit cb257d59f97ea5655bf453d8e7f07bbfb0a88e58, reversing
changes made to f5f28e0d2c49d5c62914edf0bff8f1941eef721f.

Revert "`std.Progress`: fix inaccurate line truncation and use optimal
max terminal width (#12079)"

This reverts commit cd3d8f3a4ee22a41098b1daf2a36d7fbb342d0fa.
2022-10-18 18:53:44 -07:00
Ali Chraghi
ca27055cda all: rename @maximum to @max and @minimum to @min 2022-10-18 14:15:16 +03:00
r00ster91
ab4e696e1f fix: handle larger window sizes more robustly
We should now be able to handle virtually any window size gracefully.
2022-10-14 09:38:09 +02:00
r00ster91
4ae8717fb3 test: uncomment print
For general output testing, this shouldn't always be required and is only sometimes useful.
2022-10-13 16:06:51 +02:00
r00ster91
cbe6872518 refactor: max_width calculation
I think this may be helpful in the future when we might want to calculate this again at some other point.
It also makes it more clear that the other two functions below it are only required for this calculation.
2022-10-13 16:06:51 +02:00
r00ster91
e484e75969 docs: add notes 2022-10-13 16:06:46 +02:00
zooster
cd3d8f3a4e
std.Progress: fix inaccurate line truncation and use optimal max terminal width (#12079)
* prep: output_buffer -> output_buffer_slice

* fix: truncate lines accurately

Currently, the code assumes a terminal width of 100.

If we look at what's printed for the last test:
```
Test [1/1] test "basic functionality"... [101/100] this is a really long name designed to activate the truncation code. let's fi...
```
No, it does not really work because the relevant part here is `"[101/100] this is a really long name designed to activate the truncation code. let's fi... "`,
which is 90 characters, but we expect 100 because that's the width that is assumed.
The reason is that it also measures **unprintable characters** (escape sequences) at least non-Windows systems.
With this commit the output is now:
```
Test [1/1] test "basic functionality"... [101/100] this is a really long name designed to activate the truncation code. let's find out if...
```
Of which `"[101/100] this is a really long name designed to activate the truncation code. let's find out if... "`
is the actual output of *our* `std.Progress` (remember that `zig test` has an `std.Progress` and our test itself does).
The length of that string is 100. Now the length is consistent with Windows where we don't use escape sequences. This issue was only present on non-Windows systems.

* feat: decide optimal maximum width

This is done by 1. getting the current terminal width and 2. subtracting that by the current cursor column. This accounts for previous output from someone else.

* test: add more tests

They make it easier to see how the progress line is printed in different cases.

* style: fix typo and improve docs

It also expands an acronym used as a variable name. It confused me.

* cleanup: import std.time

* test: add test

* fix: limit termios usage to Linux only for now

* fix: missing cast on Windows

* test: try to debug failure

* fix: fix off-by-one and disable tests

* docs: make comment clearer

* fix: more durability

* fix(getTerminalWidth): change order
2022-10-13 06:39:24 -04:00
r00ster91
697e22caa4 fix: resolve data race in std.Progress.maybeRefresh()
It seems we can simply lock the update mutex a little earlier.
2022-10-11 17:46:39 +02:00
Andrew Kelley
f2e8c79763 std.Progress.log: adjust API
Now it will fall back to std.debug.print if there is no tty.
2022-05-26 21:58:19 -07:00
Andrew Kelley
c30edd78f9 std.Progress: activate() calls maybeRefresh()
This makes the progress bar display the ongoing operation in the case
that the API user calls activate().
2022-05-13 14:31:30 -07:00
erikarvstedt
8ed792b640
std.Progress: fix suffix printing
Previously, `suffix` was copied to `output_buffer` at position
`max_end`, thereby writing into reserved space after `max_end`.
This only worked because `suffix` was not larger than
`bytes_needed_for_esc_codes_at_end` (otherwise there'd be a potential
buffer overrun) and no escape codes at end are actually written.

Since 2d5b2bf1c986d037ef965bf8c9b4d8dfd5967478, escape codes are no
longer written to the end of the buffer. They are now written
exclusively to the front of the buffer.
This allows removing `bytes_needed_for_esc_codes_at_end` and
simplifying the suffix printing logic.

This also fixes the bug that the ellipse suffix was not printed in
Windows terminals because `end.* > max_end` was never true.
2022-02-14 12:14:50 +02:00
Sebsatian Keller
f22443bb05
Fixed progress indicator for zig test (#10859)
Previously the progress displayed the first item as [0/x]. This was
misleading when x is the number of items. The first item should be
displayed as [1/x]
2022-02-13 15:19:33 +02:00
Andrew Kelley
5a00e24963 std.Progress: make the API infallible
by handling `error.TimerUnsupported`. In this case, only explicit calls
to refresh() will cause the progress line to be printed.
2022-02-08 17:26:55 -07:00
Jeremy Fillingim
fffa89700e Fix test label off-by-one error (#10277).
The console test# label [test#/#tests] was being generated inside
refreshWithHeldLock (in lib/std/Progress.zig), using the number of
completed items. This was being incremented by 1 when displayed,
which is not required.
2021-12-06 12:18:41 -08:00