Repository navigation
Refactor name check and title update so works with custom prompt - #584
Conversation
* Run update on new tab, switch tab, close tab and `Return` entered * Do not use terminal.window_title
* In case a script changes the CWD
| } | ||
|
|
||
| t.contents_changed.connect (() => { | ||
| schedule_name_check (); |
There was a problem hiding this comment.
This gets called "whenever the visible appearance of the terminal has changed". This means it is called on every keystroke and also when a program which outputs a log messages or graphical program like htop changes the contents of the terminal. So this means it is called approximately 50-500 times per minute for normal terminal usage. So it should be made sure that this calls are not too expensive.
At the moment this callback eventually calls save_opened_terminals, which is relatively expensive operation. For my machine the CPU usage more than doubled when using vim.
On counter measure would be to check if the terminal has a running foreground process and return immediately if this is the case.
There was a problem hiding this comment.
Maybe this should also only be run on user input and not if the contents of the terminal are changed by a program.
There was a problem hiding this comment.
Denouncing the contents_changed by ~500ms could probably also help.
There was a problem hiding this comment.
Another idea: Doesn't changing the directory always require to press Enter? Wouldn't be the simplest solution to schedule the name_check every time Enter is pressed and no foreground process is running?
There was a problem hiding this comment.
The cwd can be changed by scripts, which are not foreground processes(? I think). If there is a way of detecting when a bash script is running (or detecting when a script inserts Enter) then that would be more efficient
There was a problem hiding this comment.
If the script is run in a sub-process (which is in my understanding equivalent to foreground process) like this
bash script.sh or ./script.sh
it shouldn't be able to change the CWD. But if somebody sources a script which changes the cwd script like this
source script.sh
yes then it could the change the CWD. This is probably really rare so you have to decide if it is worth taking into acount..
Also there is already some kind of precedent set with the remember_position which relies on the Enter key being pressed. Using the contents_changed signal introduces a second mechanism for similar issue: Both want to check if a command was executed. IMO, ideally both should rely on the same mechanism.
|
Do you know what's the point of Shouldn't it be technically possible to get notified whenever the CWD changes, without relying on continuously polling for it? Similar how terminal/src/Widgets/TerminalWidget.vala Lines 450 to 460 in ac1bcc0 use the Linux |
|
@andreasfelix Thanks for the review! The updating is throttled by a timeout so I do not think it is particularly expensive. You are right that a better solution would to be notified of a cwd change rather than reacting to signals that |
|
Isn't |
|
oh yes :/ according to stackoverflow seems like watching the proc filesystem is not possible. |
* Re-use timeout if running instead of recreating on every call * Avoid possibility of timeout running continuously * Do not save terminals when updating labels * Call update labels max 2 times per sec
|
IMO, the easiest solution would be to implement Then you could register the I think this way the code would be a little bit more self documenting, because it makes clear that the names has to be checked every time a terminal widget's cwd changes. And if in the future somebody comes up with some But this way would not catch the case the CWD is changed by a script, but it would be an improvement over how Mate terminal, Gnome terminal or tilix handle it. |
|
The last commit reduces the cost of calling I do not see a lot of advantage in creating a new terminal signal tbh instead of monitoring the existing terminal signals. |
|
If you rather want to cover all cases by using |
|
But then you have each terminal running code everytime its contents changes instead of MainWindow. Each terminal would have to connect to and process the I suppose it might be worth it as |
|
Isn't the |
|
|
|
Yes, this is what I suggested. But if you don't like that, I could do a separate PR afterwards. The PR is probably still worth merging, as it is already a big improvement over the current state. |
|
It should be possible for MainWindow to maintain a cwd property on each terminal and ignore signals where it hasn't changed but it would mean calls to |
|
schedule_name_check calls the get_shell_location for all terminal widgets. so it should be much more expensive than a single call to get_shell_location. at least to my understanding of the code. |
|
One advantage of your approach is that it could be more responsive - it may not be necessary to throttle updating the labels. I'll give it a try. |
* No need to throttle checking for same name
|
I have to admit your way works at least as well and is more responsive 😄 because I removed the throttle. The only case it doesn't work (and probably the original didnt) is when you drag a tab out of a window - the remaining tab labels are not updated. |
Co-authored-by: Felix Andreas <felix.andreas95@googlemail.com>
|
@andreasfelix You might like to look at elementary/code#1050 for comparison. This uses a different method of disambiguation based on File functions which is a little simpler but in theory could result in one tab name being much longer than the other whereas my method ensures they are the same length. |
| resized = true; | ||
| }); | ||
|
|
||
| contents_changed.connect (check_cwd_changed); |
There was a problem hiding this comment.
The only thing that still bother me a little bit, is that this is also triggered for foreground processes. Guarding it with a has_foreground_process check probably doesn't make sense, because I assume they are comparatively expensive.
Anyway it probably doesn't matter so much, I didn't see any performance differences on my machine.
There was a problem hiding this comment.
One solution would be to unregistered the callback when a foreground process is running and re-register it on child_exited. But this is probably a unnecessary micro optimizations, just came to my mind.
There was a problem hiding this comment.
Yes, if foreground processes cannot change the cwd then this check is unnecessary. But I doubt whether the extra complexity of code is worth the (undetectable) performance gain.
* Lose unnecessary boolean * Check names directly on tab restored etc rather than on switch page
| internal const string DEFAULT_LABEL = _("Terminal"); | ||
| public Terminal.Application app; | ||
| public string terminal_id; | ||
| public string current_working_directory { get; private set; default = "";} |
There was a problem hiding this comment.
Codestyle
| public string current_working_directory { get; private set; default = "";} | |
| public string current_working_directory { get; private set; default = ""; } |
felix-andreas
left a comment
There was a problem hiding this comment.
The code looks good to me! It solves both linked issues. And even though the callback of the contents_changed signal is called multiple times per second when using the terminal, I can't measure any performance differences.
|
Thanks for your patience in collaborating on this! I don't particularly like connecting to |
Fixes #583
Fixes #567
Replaces #568