Commit graph

177 commits

Author SHA1 Message Date
Daniel Eklöf
6a181c9f72
grid: performance: check for non-NULL before comparing with terminator
This should be slightly faster in the normal(?) case (no styled
underlines or OSC-8).
2025-02-10 13:19:57 +01:00
Daniel Eklöf
3d66db63cc
grid: refactor reflow
We've been trying to performance optimize reflow by "chunking" cells;
try to gather as many as possible, and memcpy a chunk at once.

The problem is that a) this quickly becomes very complex, and b) is
very hard to get right for multi-column characters, especially when we
need to truncate long ones due to the window being too small.

Refactor, and once again walk and copy all cells one by one. This is
slower, but at least it's correct.
2025-02-10 13:19:50 +01:00
Daniel Eklöf
26acf41d13
grid: pull in misc.h when TIME_REFLOW is defined 2025-02-10 09:08:14 +01:00
Daniel Eklöf
d7e8f29ee2
grid: reflow: get number of spacers to insert from the old grid
When checking if we're breaking in the middle of a multi-column
character, we counted spacers starting from the break point. But,
the character may be wider than that. Use the fact that the spacers
cells encode how many *more* there are after them; when we get to the
first one, we know exactly how wide the character is.
2025-02-06 14:02:38 +01:00
Daniel Eklöf
1111f7e918
grid: reflow: handle composed characters longer than 2 cells
The logic that tries to ensure we don't break a line in the middle of
a multi-cell character was flawed when the number of cells were larger
than 2.

In particular, if the number of cells to copy were limited by the
number of cells left on the current (new) line, and were less than the
length of the multi-cell character, then we failed to insert the
correct number of spacers, and also ended up misplacing the multi-cell
character; instead of pushing it to the next line, it was inserted on
the current line, even though it doesn't fit.

Also change how trailing SPACER cells are rendered (cells that are
"fillers" at then end of a line, when a multi-column character was
pushed over to the next line): don't copy the previous cell's
attributes (which may be wrong anyway), use default attributes
instead.
2025-02-06 07:42:38 +01:00
Daniel Eklöf
32919b1049
grid: typo 2025-02-05 13:35:16 +01:00
Daniel Eklöf
97385b007f
grid: reflow: regression: remove (truncate) SPACER cells at the end of line
When printing a double-width glyph at the end of the line, it will get
pushed to the next line if there's only one cell left on the current
line.

That last cell on the current line is filled with a SPACER value.

When reflowing the text, the SPACER cell should be "removed", so that
the double-width glyph continues directly after the text on the
previous line.

9567694bab fixed an issue where
reflowing e.g. neofetch output incorrectly removed spaces between the
logo, and the system info. But also introduced a regression where
SPACER values no longer are removed.

This patch tries to fix it, by adding back empty cells, but NOT SPACER
cells.
2025-01-25 08:46:21 +01:00
Craig Barnes
674a535fc3 Rename various uses of "curly" in the source code with "underline"
Since "curly" could make it seem as if all underline styles are curled
(to people unfamiliar with the codebase), whereas in reality only 1 is.
2024-07-01 20:00:16 +01:00
Daniel Eklöf
73f6982cbe
grid: merge reflow_{uri,curly}_range_start(), and reflow_{uri,curly}_range_end() 2024-06-27 19:22:01 +02:00
Daniel Eklöf
0d3f2f27e3
grid: refactor: replace {uri,curly}_range_append() with range_append()
Also add range_append_by_ref(), with replaces
uri_range_append_no_strdup().
2024-06-26 19:01:54 +02:00
Daniel Eklöf
cb4a74e10b
grid: row_range_put(): use correct type in call to range_delete() 2024-06-26 18:39:24 +02:00
Daniel Eklöf
45f4eb48fb
grid: refactor: remove union range_data_for_insertion
This union is identical to row_range_data, except the URI char pointer
is const. Let's ignore that, and re-use row_range_data, casting the
URI pointer when necessary.

Also remove uri_range_insert() and curly_range_insert(), and use the
generic version of range_insert() everywhere.
2024-06-26 18:39:24 +02:00
Daniel Eklöf
6a0110446c
grid: grid_row_{uri,curly}_range_put(): share code
grid_row_uri_range_put() and grid_row_curly_range_put() now share the
same base logic.

Range specific data is passed through a union, and range specific
checks are done through switched functions.
2024-06-26 18:39:24 +02:00
Daniel Eklöf
b20302c2a7
grid: reflow: handle styled underlines 2024-06-26 18:39:23 +02:00
Daniel Eklöf
32effc6657
csi: wip: styled underlines
This is work in progress, and fairly untested.

This adds initial tracking of styled underlines. Setting attributes
seems to work (both color and underline style). Grid reflow has *not*
been tested.

When rendering, style is currently ignored (all styles are rendered as
a plain, legacy underline).

Color however, *is* applied.
2024-06-26 18:39:23 +02:00
Daniel Eklöf
20923bb2e8
grid: refactor: first step towards a more generic range handling 2024-06-26 18:39:23 +02:00
Craig Barnes
e8b04e0e2c
xmalloc: add xmemdup() and use to replace some uses of xmalloc+memcpy 2024-03-17 12:31:02 +01:00
Daniel Eklöf
7999975016
Don't use fancy Unicode quotes, stick to ASCII 2024-02-06 12:36:45 +01:00
Daniel Eklöf
231e6eb3f1
grid: resize with reflow: reflow FTCS_COMMAND_{EXECUTED,FINISHED} 2024-02-06 12:13:09 +01:00
Daniel Eklöf
110a6dd6f0
grid: resize without reflow: truncate shell_integration.cmd_{start,end}
This ensures the cmd start/end columns are valid in the new grid.
2024-02-06 12:13:09 +01:00
Daniel Eklöf
e9607de5ae
osc: store column of FTCS_COMMAND_{EXECUTED,FINISHED} in row struct 2024-02-06 12:12:57 +01:00
Daniel Eklöf
f8e875a7cd
term: move row->prompt_marker into new struct, row->shell_integration 2024-02-06 12:12:56 +01:00
Daniel Eklöf
58d967b2f3
Codespell fixes 2023-10-03 14:11:55 +02:00
Daniel Eklöf
49fb0cf359
sixel: re-scale images when the cell dimensions change
Before this patch, when the cell dimensions changed (i.e. when the
font size changes), sixel images were either removed (the new cell
dimensions are smaller than the old), or simply kept at their original
size (new cell dimensions are larger).

With this patch, sixels are instead resized. This means a
sixel *always* occupies the same number of rows and columns,
regardless of how much the font size is changed.

This is done by maintaining two sets of image data and pixman images,
as well as their dimensions. These two sets are the new ‘original’ and
‘scaled’ members of the sixel struct.

The "top-level" pixman image pointer, and the ‘width’ and ‘height’
members either point to the "original", or the "scaled" version.

They are invalidated as soon as the cell dimensions change. They, and
the ‘scaled’ image is updated on-demand (when we need to render a
sixel).

Note that the ‘scaled’ image is always NULL when the current cell
dimensions matches the ones used when emitting the sixel (to save
run-time memory).

Closes #1383
2023-06-30 08:29:35 +02:00
Harri Nieminen
ae26915916 fix typos 2023-03-29 00:45:18 +03:00
Daniel Eklöf
c4f08a3b9a
grid_free(): allow being called with grid == NULL 2022-10-18 18:30:02 +02:00
Daniel Eklöf
18ef36523f
grid: resize: assert grid->cur_row is not NULL after a grid resize 2022-10-10 17:19:18 +02:00
Daniel Eklöf
8179d73daa
render: delay reflow for ‘resize-delay-ms’ milliseconds
Reflowing a large scrollback is *slow*. During an interactive resize,
it can easily take long enough that the compositor fills the Wayland
socket with configure events. Eventually, the socket becomes full and
the compositor terminates the connection, causing foot to exit.

This patch is work-in-progress, and the first step towards alleviating
this.

It delays the reflow by:

* Snapshotting (copying) the original grid when an interactive resize
  is started.
* While resizing, we apply a simple truncation resize of the
  grid (like we handle the alt screen).
* When the resize is done, or paused for ‘resize-delay-ms’, the grid
  is reflowed.

TODO: we *must* not allow any changes to the temporary (truncated)
grid during the resize. Any changes to the grid would be lost when the
final reflow is applied. That is, we must completely pause the ptmx
pipe while a resize is in progress.

Future improvements:

The initial copy can be slow. We should be able to avoid it by
rewriting the reflow algorithm to not free anything. This is
complicated by the fact that some resources (e.g. sixel images) are
currently *moved* to the new grid. They’d instead have to be copied.
2022-10-10 17:19:17 +02:00
Daniel Eklöf
454c82f0f5
grid: reflow: assert there aren’t any open URIs on the last row 2022-08-29 21:03:21 +02:00
Daniel Eklöf
5c86358cd1
grid: reflow: don’t line-wrap the last row
Before this patch, we would line-wrap the last row, just like any
other row, and then afterwards try to reverse this, by adjusting the
offset and free:ing and NULL:ing the "last row".

The problem with this is if the scrollback is full. In this case, the
row we’re freeing is the first row in the scrollback history. This
means we’ll crash as soon as the viewport is moved to the top of the
scrollback.

The fix is fairly, simple. Skip the post-processing logic, and instead
detect when we’re line-wrapping the last row, and skip the call to
line_wrap().

This way, the last row in the new grid corresponds to the last row in
the old grid.
2022-08-29 20:47:33 +02:00
Daniel Eklöf
13281f327b
grid: when setting the new viewport, ensure it’s correctly bounded
Do this by using scrollback relative coordinates, and ensure the new
viewport is not larger than (grid_rows - screen_rows), as that would
mean the viewport crosses the scrollback wrap-around.
2022-08-29 20:46:19 +02:00
Daniel Eklöf
aaf5894ad9
grid: get rid of empty row at the bottom after reflowing
When the window is resized and we reflow the text, we ended up
inserting an empty row at the bottom.

This happens whenever the actual last row has a hard linebreak (which
almost always is the case); we then end the reflow with a line break,
causing an extra, empty, row to be allocated and inserted.

This patch fixes this by detecting when:

1) the last row is empty
2) the next to last row has a hard line break

In this case, we roll back the last line break, by adjusting the new
offset we just calculated, and free:ing the empty row.

TODO: it would be nice if we could detect this in the reflow loop
instead, and avoid doing the last line break all together. I haven’t
yet been able to find a way to do this correctly.

Closes #1108
2022-08-05 18:26:37 +02:00
Daniel Eklöf
0c60bb3f29
grid: reflow: require col count > 0 when skipping line truncation
When reflowing the grid, we truncate lines with a hard linebreak after
the last non-empty cell. This way we don’t reflow trailing empty cells
to a new line when resizing the window to a smaller size.

However, “logical” lines (i.e. those without a hard linebreak)
are *not* truncated. This is to ensure we don’t trim empty cells in
the middle of a logical line spanning multiple physical lines.

Since newly allocated rows are initialized with linebreak=false, we
need to ensure _those_ are still truncated - otherwise all that empty
space under the current prompt will be reflowed.

Note that this is a temporary workaround. The correct solution, I
think, is to track whether a line has been printed to or not, and
simply ignore (not reflow) lines that haven’t yet been touched.
2022-06-30 19:47:18 +02:00
Daniel Eklöf
37f094280b
Revert "grid: invert the default value of ‘linebreak’, from false to true"
This reverts commit cdd46cdf85.
2022-06-30 19:37:01 +02:00
Daniel Eklöf
bdb79e8b9f
osc: add support for OSC 133;A (prompt markers)
This patch adds support for the OSC-133;A sequence, introduced by
FinalTerm and implemented by iTerm2, Kitty and more. See
https://iterm2.com/documentation-one-page.html#documentation-escape-codes.html.

The shell emits the OSC just before printing the prompt. This lets the
terminal know where, in the scrollback, there are prompts.

We implement this using a simple boolean in the row struct ("this row
has a prompt"). The prompt marker must be reflowed along with the text
on window resizes.

In an ideal world, erasing, or overwriting the cell where the OSC was
emitted, would remove the prompt mark. Since we don't store this
information in the cell struct, we can't do that. The best we can do
is reset it in erase_line(). This works well enough in the "normal"
screen, when used with a "normal" shell. It doesn't really work in
fullscreen apps, on the alt screen. But that doesn't matter since we
don't support jumping between prompts on the alt screen anyway.

To be able to jump between prompts, two new key bindings have been
added: prompt-prev and prompt-next, bound to ctrl+shift+z and
ctrl+shift+x respectively.

prompt-prev will jump to the previous, not currently visible, prompt,
by moving the viewport, ensuring the prompt is at the top of the
screen.

prompt-next jumps to the next prompt, visible or not. Again, by moving
the viewport to ensure the prompt is at the top of the screen. If
we're at the bottom of the scrollback, the viewport is instead moved
as far down as possible.

Closes #30
2022-06-16 19:02:10 +02:00
Daniel Eklöf
9567694bab
grid: reflow: don’t trim trailing empty cells from logical lines
When a line in the old grid (the one being reflowed) doesn’t have a
hard linebreak, don’t trim trailing empty cells.

Doing so means we’ll “compress” (remove) empty cells between text
if/when we revert to a larger window size.

The output from neofetch suffers from this; it prints a logo to the
left, and system information to the right. The logo and the system
info column is separated by empty cells (i.e. *not* spaces).

If the window is reduced in size such that the system info is pushed
to a new line, each logo line ends with a number of empty cells. The
next time the window is resized, these empty cells were
ignored (i.e. removed).

That meant that once the window was enlarged again, the system info
column was a) no longer aligned, and b) had been pulled closer to the
logo.

This patch doesn’t special case trailing empty cells when the line
being reflowed doesn’t have a hard linebreak. This means e.g. ‘ls’
output is unaffected.

Closes #1055
2022-06-11 12:02:20 +02:00
Daniel Eklöf
cdd46cdf85
grid: invert the default value of ‘linebreak’, from false to true 2022-06-11 12:02:20 +02:00
Daniel Eklöf
b4f666118f
grid: add abs-to-sb and sb-to-abs utility function
These functions convert row numbers between absolute coordinates and
“scrollback relative” coordinates.

Absolute row numbers can be used to index into the grid->rows[] array.

Scrollback relative numbers are ordered with the *oldest* row first,
and the *newest* row last. That is, in these coordinates, row 0 is the
*first* (oldest) row in the scrollback history, and row N is the
*last* (newest) row.

Scrollback relative numbers are used when we need to sort things after
their age, when determining if something has scrolled out, or when
limiting an operation to ensure we don’t go past the scrollback
wrap-around.
2022-04-25 19:57:18 +02:00
Daniel Eklöf
0800515c04
grid: reflow: add TODO to detect selection on re-used rows, and cancel it 2022-02-07 15:06:55 +01:00
Pranjal Kole
0da19a81bc replace gettimeofday with clock_gettime
POSIX.1-2008 has marked gettimeofday(2) as obsolete, recommending the
use of clock_gettime(2) instead.

CLOCK_MONOTONIC has been used instead of CLOCK_REALTIME because it is
unaffected by manual changes in the system clock. This makes it better
for our purposes, namely, measuring the difference between two points in
time.

tv_sec has been casted to long in most places since POSIX does not
define the actual type of time_t.
2022-01-15 21:35:45 +05:30
Daniel Eklöf
2e8c9c2fe5
grid: reload pointers into the uri range vector after inserting
Inserting elements into the URI range vector typically triggers a
vector resize. This is done using realloc(). Sometimes this causes the
vector to move, thus invalidating all existing pointers into the
vector.
2021-12-08 17:41:29 +01:00
Daniel Eklöf
2fe2dfa847
grid: uri_range_erase(): unittest: try erasing a row without any URIs 2021-11-28 11:05:00 +01:00
Daniel Eklöf
66b8c92c30
grid: uri_range_erase: walk the ranges backwards
This way, ranges are deleted last-to-first, thus avoiding memmoving
multiple ranges over and over again when erasing e.g. a full line.
2021-11-28 11:03:07 +01:00
Daniel Eklöf
747da83c76
grid: remove useless assertion
grid.c:1041:24: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
2021-11-27 18:15:59 +01:00
Daniel Eklöf
5ce6c89df6
grid: resize without reflowing: use URI range utility functions 2021-11-27 16:31:33 +01:00
Daniel Eklöf
dfeca4e134
grid: uri_range_{append,insert}: pass range attributes as separate parameters 2021-11-27 16:25:29 +01:00
Daniel Eklöf
5c6c2de051
grid: snapshot: use uri range utility functions 2021-11-27 16:11:26 +01:00
Daniel Eklöf
5c7e881cd4
grid: uri_range_ensure_size(): add the requested amount of entries 2021-11-27 16:03:07 +01:00
Daniel Eklöf
7699271316
grid: reflow: no need to keep a uri-range-idx variable
The URI ranges are now an array, which means we can get the next URI
range by incrementing the pointer.

Instead of checking if range is not NULL, check that it isn’t the
range terminator (which is NULL when we don’t have any ranges, and
the first address *after* the last range otherwise).
2021-11-26 20:57:08 +01:00
Daniel Eklöf
41565a0d0e
grid: resize without reflow: only verify URI ranges in debug builds 2021-11-26 20:36:59 +01:00