I have several cron jobs run every few hours and they generate reports in either plain-text or HTML. When the clock passes some o’clock, I use tabopen command with bookmark keywords to open the directories of reports. From there, I open the newly generated reports into new tabs and read them.
The entire procedure doesn’t take long, but I think I can save a few keystrokes or mouse clicks, and that’s always a good thing, don’t you think?
Since the reports are not always generated, because there is a possibility of no new incoming data, and timestamps are attached to report filenames, therefore creating individual bookmark for each report is impossible. So, the best way is to open files under those directories. I always remove those reports once I have read, there is no needs for me to keep them in track since the data is gathered from API of external web services.
To achieve this, there are only two important tasks:
- Listing files on local filesystem and
- Opening the listed files in new tabs in the background
That’s all this code has to do in order to save me a small amount of time. Here is the finished code:
js <<EOF group.commands.add( ['openfiles'], 'Open files into new tabs', function(paths) { paths.forEach(function (path) { for (let f of File(File.expandPath(path)).iterDirectory()) { if (f.isFile()) { var params = { background: true, where: dactyl.NEW_TAB }; dactyl.open(f.path, params); } } }); }, { argCount: '*', completer: function (context) { completion.directory(context, true); } }, true ); EOF
To map the command, just append all the directories I need after the command
map <Leader>o :openfiles ~/path/to/foo ~/path/to/bar<CR>
This openfiles command accepts a number of paths. It uses File.expandFile() to expand ~ into user’s home directory, then initializes a File with the expanded path. It then walks through all the files in that directory via iterDirectory() and open whatever is a file into background tab.
The last true is for the parameter replace of add(), that would make sure the openfile command be replaced if one already exists.
I think it’s possible to check with history to see if any of those files has been visited and skip them, but they will be all new to me since I always remove after open them, so I don’t code that part.
Coding in Pentadactyl is always a bit tricky for me since there is no such “References” documentation, so I would have to either find some examples online or just read the source code. Unfortunately, there is not many examples out there, the best way is to read the source code. Nonetheless, so far, I always got what I intended to achieve.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.