visidata
visidata copied to clipboard
[] Better save defaults when manipulating files
I am loving all the features of VisiData and I am thinking of using VisiData to manipulate table data. On that note I see that there are some gotcha's when manipulating and saving data that might cause me and others some future frustration:
-
It is quite easy to exit the application without saving data, so it seems to me a sane default if the application does not exit so easily if some (non-derived) data is not saved.
-
It would be nice if VisiData had an option to launch it as a file editor, where the default was to manipulate and save the same file as you opened it with. Selecting 'Save' should then default to saving over the same file you opened, with roughly the same structure (unless modified).
Just as a quick example; if I want to edit a .rec file I currently need to descend into the specific record type sheet and edit the data, but then to save the .rec file it is necessary to back-up to the top sheet (without pressing q too many times), and then use the key combination gsg^S
which selects all rows (being record types) and saves all sheets. I find this to be somewhat of a gotcha and it could lead to some wasted effort.
I can see that these features aren't necessarily trivial to implement, but I thought they were worth mentioning.
Kudos!
Thanks for the ideas, @pyglot ! For the second one, I'm thinking we should add z Ctrl+S
"commit-sheet" command, that works like it does on sqlite and other db sheets. This would be a natural interface extension, and not too hard to implement.
For the first one, do you know about options.quitguard
? I think it should act mostly as you want and prevent accidental quitting of modified sheets.
Thanks for the quick reply, Saul! I didn't know about quitguard. I will make it my default.
Yes, z^S sounds like a good choice. I am now on my phone and unable to try it out, but If z^S works the same from all sheets and I can confidently save and quit from wherever that's what I'm looking for :)
GM. Thanks again for being so helpful and for making such an awesome app!
I have given options.quitguard=1
a trial. It's nice and useful so I will keep it on for data editing, but I am thinking it might be nice to also have a similar quitguard option that looks at all sheets and only takes effect when you are about to exit the application? E.g.: The current behavior is that quitguard stops me from quitting a modified subsheet (typically one step into the .rec), but that subsheet is not a location where I can save the .rec file from. So I still have to accept-quit from the modified subsheet without saving and then do a save from the main sheet. However at the main sheet there are no modifications to that sheet and hence no quitguard preventing me from exiting the main application/sheet which could lead to losing some work.
I see that there may be another way to recover from accidental loss of data input if I add a options.cmdlog_histfile="~/.visidata/cmdlog
to my rc file. Then I could use that to replay what I did. I'll experiment with that a bit later.
I am also happy to discover the possibility to use bindkey()
in the rc file. I think I might try to remap ^S
to z^S
for .rec files when the function becomes available, just for my own taste :).
A quick ref to a saver discussion if you've not already seen it https://github.com/saulpw/visidata/discussions/1266
cmdlog_histfile
could in theory reconstruct your session, but in practice I would only use it as a reference, and mainly for the input strings.
For the saver, I'm thinking about adding a new concept, the rootSheet
, which is the last sheet up the source
chain before a non-Sheet, usually a source Path. So then we could have save-source
:
@BaseSheet.api
def rootSheet(sheet):
r = sheet
while isinstance(r.source, BaseSheet):
r = r.source
return r
BaseSheet.addCommand('', 'save-source',
'vd.saveSheets(rootSheet.source, rootSheet, confirm_overwrite=options.confirm_overwrite)',
'save root sheet to its source')
You can't bind this to z^S
as a general command though, because commit-sheet
is currently on Sheet itself, so we'd have to make a CommittableSheet that all the defer=True
classes could inherit from instead, and then add the current commit-sheet
command to that. So on CommittableSheets z Ctrl+S
would do the same as it does now (which should update the source equivalently), and on source sheets and derived sheets it would be able to call this default. In the meantime you can bind it to something else, or unbind this existing z^S
on Sheet.
Having a rootSheet property inherited by the children sheets sounds good. Both 'save-root' and 'save-source' sound like viable command names to me. Maybe add an option for confirm_filename then the operation can be done with one mapped key without asking for confirmation.
Instead of an option, how about save-source-really
, like vdplus has save-sheet-really
: https://github.com/visidata/vdplus/blob/develop/extras.py#L43
Hehe, yeah ok. It could work. Main caveat would be if you duplicate code over multiple very similar procedures it can get difficult to manage.
Sorry about that. I was typing on my phone.
@saulpw did eat a command save-source
and the rootSheet
concept to VisiData! It seems if you set options.confirm_overwrite
to False, there is no confirmation needed.
So, if I understand correctly, the addresses to this issue are:
in a ~/.visidatarc
:
options.confirm_overwrite = False
options.quitguard = True
bindkey('keystrokeHere', 'save-source')
and that should get you your desired behaviour. You then would use keystrokeHere
to save the current sheet to the source file.
I am going to close this issue as resolved, but please feel free to comment if you need clarification, or have an additional request that I missed!