Commit graph

81 commits

Author SHA1 Message Date
Ryan Liptak
d750a78b2c std.Progress: Fix Windows console API implementation
3a3d2187f9 unintentionally broke some of the Windows console API implementation.

- The 'marker' character was no longer being written at all
- The ANSI escape codes for syncing were being written unconditionally
2024-05-28 22:46:12 -04:00
Andrew Kelley
3a3d2187f9 std.Progress: better Windows support
* Merge a bunch of related state together into TerminalMode. Windows
  sometimes follows the same path as posix via ansi_escape_codes,
  sometimes not.
* Use a different thread entry point for Windows API but share the same
  entry point on Windows when the terminal is in ansi_escape_codes mode.
* Only clear the terminal when the stderr lock is held.
* Don't try to clear the terminal when nothing has been written yet.
* Don't try to clear the terminal in IPC mode.
* Fix size detection logic bug under error conditions.
2024-05-28 12:31:10 -07:00
Ryan Liptak
40afac40b8 std.Progress: Use Windows console API calls when ANSI escape codes are not supported 2024-05-28 10:41:07 -07:00
Andrew Kelley
65a0e14e4f std.Progress: relax some of the atomic orderings
Generates better machine code, particularly on ARM
2024-05-27 20:56:49 -07:00
Andrew Kelley
aca7feb8fa std.Progress: fix race condition with setIpcFd
The update thread was sometimes reading the special state and then
incorrectly getting 0 for the file descriptor, making it hang since it
tried to read from stdin.
2024-05-27 20:56:49 -07:00
Andrew Kelley
44389253c2 fix zig translate-c creating root progress node twice 2024-05-27 20:56:49 -07:00
Andrew Kelley
2367a1ff84 std.Progress: handle short writes 2024-05-27 20:56:49 -07:00
Andrew Kelley
dcf9cae256 std.Progress: handle big-endian targets
We cannot rely on host endianness because the parent or child process
may be executing inside QEMU.
2024-05-27 20:56:49 -07:00
Andrew Kelley
eea7e5e554 std.Progress: adjust the timings a little bit
Slightly slower refresh rate. It's still updating very quickly.

Lower the initial delay so that CLI applications feel more responsive.
Even though the application is doing work for the full 500ms until
something is displayed on the screen, it feels nicer to get the progress
earlier.
2024-05-27 20:56:49 -07:00
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 cb257d59f9, reversing
changes made to f5f28e0d2c.

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

This reverts commit cd3d8f3a4e.
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