terminator icon indicating copy to clipboard operation
terminator copied to clipboard

[Feature Request] Option to save / restore complete session state (not just layout)

Open lhriley opened this issue 3 years ago • 5 comments

Is your feature request related to a problem? Please describe.

I've been using Terminator for many years as it is the best option for managing multiple terminal sessions. One major frustration I've had over the years is the fact that there is no option in Terminator that will save the complete session I'm working in.

For example, I use Terminator daily for work, so I typically maintain several terminals in various states (for example, one for executing tasks, another for testing / debugging code, another for running Terraform, etc). Now, if I need to reboot my system for some reason (to apply a kernel update, for example) I lose not only the layout that I had, but also any log history, working directories, etc.

This is extra frustrating in instances where my system would require a reboot unexpectedly (power outage or laptop battery runs low).

Describe the solution you'd like

As a solution to this problem, it would be great to have a "Save Session" feature which would store the layout, working directories, and possibly terminal history. This would be separate from the existing "Layouts" feature which I see as more of a templating solution and thus related, but distinct.

There could be several ways to activate this feature. For example, "Always save working session" which would essentially always track the state of the session. This would be supplemented by a manual "Save working session" option via context menu / hotkey for users who only need the feature under certain conditions, such as saving a default session that they want to launch when Terminator starts.

I see this as being essential the same as similar features in web browsers ("Continue from where I left off"), or a text editor such as Sublime Text (which can even save the state of unsaved changes to documents when the application closes), but the most obvious direct example would be the Session Restore feature of iTerm2.

Describe alternatives you've considered

I've tried using the existing "Layouts" feature, but the interface is clunky / not intuitive, and does not store the working directory automatically (consider how obnoxious this might be to manually set the paths for 10 terminal sessions). Additionally it has no options for saving log history. As I've stated above, I really see "Layouts" as a way to templatize the layout of Terminator and saving session state would be outside the scope of that function (in my opinion).

I've also considered that this might already be implemented by someone, somewhere as a plugin but I've yet to find anything.

Additional context

iTerm2

https://iterm2.com/documentation-restoration.html :

Session Restoration Session restoration works by running your jobs within long-lived servers rather than as child processes of iTerm2. If iTerm2 crashes or upgrades, the servers keep going. When iTerm2 restarts, it searches for running servers and connects to them. The OS's window restoration feature preserves the content of your window, including scrollback history. iTerm2 marries the restored session to the appropriate server so you can pick up where you were.

As an aside, there are several features of iTerm2 that would be insanely valuable in Terminator. Since it is GPL-2.0 it may provide several implementation examples, even if some translation would be required: https://github.com/gnachman/iTerm2

SublimeText

https://docs.sublimetext.io/guide/customization/settings.html#order-of-precedence-of-sublime-settings-files :

In addition to settings files, Sublime Text maintains session data, which is configuration for the particular set of files being currently edited. Session data is updated as you work on files, so if you adjust settings for a particular file in any way (mainly through API calls), they will be recorded in the session and will take precedence over any applicable .sublime-settings files.

A SublimeText plugin (in Python) that extends session management: https://github.com/sascha-wolf/sublime-SessionManager

Google Chrome

https://www.google.com/chrome/privacy/whitepaper.html#continue :

If you have selected the option to “Continue where you left off” in settings on desktop versions of Chrome, when you open Chrome, it attempts to bring you right back to the way things were when the browser was closed. Chrome reloads the tabs you had open and persists session information to get you up and running as quickly as possible. This feature effectively extends a browsing session across restarts. [...]

lhriley avatar Apr 09 '21 19:04 lhriley

I think that we could re-use the "layout" code to have a "save session" button, and/or an "autosave session". That would save the window layout and the working directories. As for the rest of them...

Saving the output would be something outside of the scope of terminator, and would require changes to the gnome vte lib. We can't really guarantee that such a patch would be accepted upstream.

Saving command history is already done by bash, so we would need to figure out a way to hook into that to get command history

Having said all that, I've used iTerm2 on my mac for pretty much as long as it's been around, and it has been a great inspiration for my terminator work, and I've definitely downloaded the code and taken a look at it, but the session restoration stuff I found has not been that good at restoring the command history or the working directory.

It's definitely something to look at, and as always, pull requests are welcome.

mattrose avatar Apr 11 '21 15:04 mattrose

@mattrose Thanks for the quick reply / feedback :1st_place_medal:

I think that we could re-use the "layout" code to have a "save session" button, and/or an "autosave session". That would save the window layout and the working directories.

I would agree that the existing layout code is a good starting place. Personally, I don't really care about editing the saved state (at this time anyway). Just the fact that I can save / restore the state is what is important. This is one of the reasons I consider the two things "separate" from a user perspective.

Saving the output would be something outside of the scope of terminator, and would require changes to the gnome vte lib. We can't really guarantee that such a patch would be accepted upstream.

I'm not familiar with the actual implementation of Terminator, but would it not be possible to stream the output to disk somewhere (as insecure as that might be without an encryption layer) and recall it when the session is restored? I thought something similar to this was happening with Infinite Scrollback, but I might be thinking of another terminal emulator.

It looks like iTerm2 uses some background daemons to manage much of the state, which would probably not be a feasible change to Terminator. In reality that would likely be more successful if someone just ported iTerm2 to a new emulator (i.e. l[inux]Term2).

Saving command history is already done by bash, so we would need to figure out a way to hook into that to get command history

Maybe I wasn't completely clear as I was referencing some parts of the existing "Layouts" implementation. I'm not sure that saving the last running command would be that important, aside from the running shell possibly. I'm sure that most people would not want some random script starting up on launch anyway. At most, maybe hooking into the shell history file to present some kind of hint as "this was the last command run", but that would be more of a wishlist item rather than a necessary feature.

Having said all that, I've used iTerm2 on my mac for pretty much as long as it's been around, and it has been a great inspiration for my terminator work, and I've definitely downloaded the code and taken a look at it, but the session restoration stuff I found has not been that good at restoring the command history or the working directory.

Some of the iTerm2 features are without a doubt the only things I miss from my days when I had to use a Mac for work. It would be great to see more things ported over, and I'm happy to hear that you're already taking inspiration from iTerm2 when approaching Terminator features.

It's definitely something to look at, and as always, pull requests are welcome.

If I end up getting some time, I might start familiarizing myself with the Terminator codebase and see what I might be able to contribute. :+1:

lhriley avatar Apr 12 '21 17:04 lhriley

Finally..something I have been looking for!! I have been using terminator and termius and always felt this to be a feature which could make Terminator the future of terminals. The persistence of the groups. ( Or maybe do we already have this..and I haven't found it properly while reading the docs..) anyway, seeing that the codebase is in python..I think this is something I can dip my legs into. :p

schaffung avatar Apr 30 '21 11:04 schaffung

I started to experiment with this, but I'm struggling to find the right time and place in the code to commit a session save.

For example, I feel that it should occur when adding, modifying, or removing pieces of the terminal layout.

The problem is with handling events like opening or closing one or more windows. Since the act of starting or exiting Terminator is basically the same as manually opening or closing everything, there needs to be some way to determine when we're not simply in a normal state of modifying layout.

This seems relatively easy on open; initialize a variable and change it once opening is complete. Exiting seems a bit more complicated to solve for, but maybe I'm just looking at the exit code in the wrong light.

lhriley avatar Apr 30 '21 16:04 lhriley

I have been using Konsole, in KDE, b/c it does exactly this. When i have to reboot it reopens the correct number of console instances and tabs; each tab remains in it's last used directory; most of my work is done vis SSH and I can call my previous command and reconnect to the same systems i was connected to before in the same order. As i'm typing this, I see that it also has splitting; so i think i'm reverting to that for now.

Terminator looks and feels great though

glenntanner3 avatar May 17 '21 17:05 glenntanner3

Hi.

Have added basic layout auto-save feature, since I am also new to the code base will add in more things gradually after this submission and if I am on right track.

(1) Bug/Issue with description: https://github.com/gnome-terminator/terminator/issues/654 (2) Branch on my repository: https://github.com/vssdeo/terminator/tree/654-Plugin-Submission-SaveLastSessionLayout-Uses-Layout-to-Auto-Save-Last (3) Pull Request: https://github.com/gnome-terminator/terminator/pull/655

vssdeo avatar Oct 16 '22 00:10 vssdeo

#655 comes pretty close to meeting all of the requirements laid out in this issue. It doesn't store history, but that is something that bash does anyways. If everyone is good with this I'm gonna close this bug as resolved

mattrose avatar Oct 31 '22 21:10 mattrose

Thanks for this @vssdeo! Looking forward to trying it out 🍻

lhriley avatar Nov 01 '22 03:11 lhriley

Hi @lhriley you are most welcome but I hope the change helps you out. @mattrose was kind and quick :) enough to pull the change in master :)

From my side the idea was to have minimal changes and to have the feature. Some points like when you have a session you have to close the last window with tabs to remember the last session. This might not be intuitive as while loading the last session two windows are open, one which user has launched and other the restored session.

The base structure is ready and these changes on exact behavior can be easily changed.

Again the idea was to:-

  1. Minimum changes to code base
  2. Use existing features

Rest of the features / behaviors can be added, confirmation box, have only one window on restore of last session, saving multiple session etc as per the user requirement or team agreements.

vssdeo avatar Nov 01 '22 05:11 vssdeo