Commit graph

38 commits

Author SHA1 Message Date
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
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 2d5b2bf1c9, 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
Andrew Kelley
008b0ec5e5 std.Thread.Mutex: change API to lock() and unlock()
This is a breaking change. Before, usage looked like this:

```zig
const held = mutex.acquire();
defer held.release();
```

Now it looks like this:

```zig
mutex.lock();
defer mutex.unlock();
```

The `Held` type was an idea to make mutexes slightly safer by making it
more difficult to forget to release an aquired lock. However, this
ultimately caused more problems than it solved, when any data structures
needed to store a held mutex. Simplify everything by reducing the API
down to the primitives: lock() and unlock().

Closes #8051
Closes #8246
Closes #10105
2021-11-09 18:31:03 -07:00
Andrew Kelley
6115cf2240 migrate from std.Target.current to @import("builtin").target
closes #9388
closes #9321
2021-10-04 23:48:55 -07:00
Andrew Kelley
d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00
Andrew Kelley
f7ee3b4ca5 std.Progress: revert to the older strategy
This reverts the most recent big changes to `std.Progress` changing the
strategy for printing. Before the changes, it would leave the cursor after
the progress line, having better behavior when a stray print happened,
and supporting sub-process progress without any coordination.

After the changes, the cursor was left at the beginning of the line,
making any prints print garbage and often interfering with stack traces
or other debug information.

This commit reverts to before the changes.

Revert "std: Use more common escape sequences in Progress"
This reverts commit 8ebb18d9da.

Revert "Handle some weird edge cases of Win32 API"
This reverts commit b0724a350f.

Revert "Fix many thinkos"
This reverts commit b5a50a26eb.

Revert "Fix Progress printing on Windows systems"
This reverts commit 3010bfb08a.

Revert "std: Better handling of line-wrapping in Progress"
This reverts commit 4fc2e92876.
2021-07-20 19:11:47 -07:00
Jacob G-W
9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Matt Chudleigh
24b1a0027f Fix crash when compiling with cygwin/msys on windows 2021-06-18 16:19:20 -07:00
viri
13dc34f779 std.Progress: use *W functions on windows
Closes #534.
See: https://source.winehq.org/git/wine.git/blob/refs/heads/stable:/dlls/kernelbase/console.c#l520
2021-06-09 19:10:28 +03:00
LemonBoy
8ebb18d9da std: Use more common escape sequences in Progress
This should fix the badly-rendered progress message when run in
Terminal.app.
2021-03-12 19:31:59 -05:00
LemonBoy
b0724a350f Handle some weird edge cases of Win32 API
Sometimes the viewport srWindow may report an invalid rectangle where
the top row is below the bottom one.
2021-03-12 17:14:50 +01:00
LemonBoy
b5a50a26eb Fix many thinkos
Somehow I forgot to save after copy-pasting some code and changing it.
2021-03-12 15:08:48 +01:00
LemonBoy
3010bfb08a Fix Progress printing on Windows systems
The cursor must be restored after the line is printed, not before.
Take into account the visible viewport to correctly compute the terminal
size.
2021-03-12 10:15:38 +01:00
LemonBoy
4fc2e92876 std: Better handling of line-wrapping in Progress
In order to update the printed progress string the code tried to move
the cursor N cells to the left, where N is the number of written bytes,
and then clear the remaining part of the line.
This strategy has two main issues:
 - Is only valid if the number of characters is equal to the number of
   written bytes,
 - Is only valid if the line doesn't get too long.

The second point is the main motivation for this change, when the line
becomes too long the terminal wraps it to a new physical line. This
means that moving the cursor to the left won't be enough anymore as once
the left border is reached it cannot move anymore.

The wrapped line is still stored by the terminal as a single line,
despite now taking more than a single one when displayed. If you try to
resize the terminal you'll notice how the contents are reflowed and are
essentially illegible.

Querying the cursor position on non-Windows systems (plot twist,
Microsoft suggests using VT escape sequences on newer systems) is
extremely cumbersome so let's do something different.

Before printing anything let's save the cursor position and clear the
screen below the cursor, this way we ensure there's absolutely no trace
of stale data on screen, and after the message is printed we simply
restore it.
2021-03-07 15:23:20 +01:00
jacob gw
1bd434fd18 std.Progress: improve support for "dumb" terminals 2021-02-21 12:12:17 +02:00
Andrew Kelley
2b321c25ce std.Progress: call refreshWithHeldLock as appropriate 2021-01-24 12:22:17 -07:00
Timon Kruiper
4f7d76f19c fix windows bug in Progress.zig
This bug caused the compiler to deadlock when multiple c objects
were build in parallel.

Thanks @kprotty for finding this bug!
2021-01-24 12:20:51 -07:00
Andrew Kelley
a9667b5a85 organize std lib concurrency primitives and add RwLock
* move concurrency primitives that always operate on kernel threads to
   the std.Thread namespace
 * remove std.SpinLock. Nobody should use this in a non-freestanding
   environment; the other primitives are always preferable. In
   freestanding, it will be necessary to put custom spin logic in there,
   so there are no use cases for a std lib version.
 * move some std lib files to the top level fields convention
 * add std.Thread.spinLoopHint
 * add std.Thread.Condition
 * add std.Thread.Semaphore
 * new implementation of std.Thread.Mutex for Windows and non-pthreads Linux
 * add std.Thread.RwLock

Implementations provided by @kprotty
2021-01-14 20:41:37 -07:00
Jay Petacat
a9b505fa77 Reduce use of deprecated IO types
Related: #4917
2021-01-07 23:48:58 -08:00
Frank Denis
6c2e0c2046 Year++ 2020-12-31 15:45:24 -08:00
Andrew Kelley
f4d82f0ad6 std.Progress: work around time going backwards 2020-12-23 13:36:21 -08:00
Andrew Kelley
fbcffe9d5d std.Progress: fix atomic ordering semantics
thx king protty
2020-12-20 15:08:59 -07:00
Andrew Kelley
aa6ef10cc6 std.Progress: make the API thread-safe
We generally get away with atomic primitives, however a lock is required
around the refresh function since it traverses the Node graph, and we
need to be sure no references to Nodes remain after end() is called.
2020-12-20 15:08:59 -07:00
Andrew Kelley
b2f8631a3c ThreadPool: delete dead code
If this errdefer did get run it would constitute a race condition. So I
deleted the dead code for clarity.
2020-12-20 15:08:59 -07:00
Renamed from lib/std/progress.zig (Browse further)