Created
March 22, 2018 01:15
-
-
Save TSFoster/f31d8780602e20faea8ed908ed4abe9d to your computer and use it in GitHub Desktop.
Open files in existing neovim instance in iTerm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Requirements: | |
* iTerm | |
* nvim | |
* nvr | |
Install: | |
1. New application in Automator | |
2. Add Run JavaScript action | |
3. Add below | |
4. Save | |
5. Open files with application | |
*/ | |
var iTerm = Application('iTerm'); | |
if(!iTerm.running()) iTerm.launch(); | |
var app = Application.currentApplication(); | |
app.includeStandardAdditions = true; | |
function run(input) { | |
if (input.length == 0 || input.length == undefined) return; | |
openIn(input, app.doShellScript("/usr/local/bin/nvr --serverlist | uniq").split('\r').map(function getCwd(sock) { | |
return { | |
socket: sock, | |
cwd: app.doShellScript("/usr/local/bin/nvr --remote-expr 'getcwd()' --servername " + sock), | |
}; | |
}).reduce(function checkForUniqueness(memo, instance) { | |
if (memo.paths[instance.cwd]) { | |
memo.paths[instance.cwd] += 1; | |
} else { | |
memo.paths[instance.cwd] = 1; | |
} | |
memo.sockets[instance.cwd + (memo.paths[instance.cwd] == 1 ? '' : ' (' + memo.paths[instance.cwd] + ')')] = instance.socket; | |
return memo; | |
}, {sockets: {}, paths: {}}).sockets); | |
} | |
function openIn(files, sockets) { | |
var fileNames = "'" + files.join("' '") + "'"; | |
var newInstance = 'A new instance'; | |
var options = Object.keys(sockets); | |
options.push(newInstance); | |
if (options.length == 1) return iTerm.write(iTerm.createTabWithDefaultProfile(iTerm.windows.first).currentSession, {text: 'nvim ' + fileNames}); | |
var answer = app.chooseFromList(options, { | |
defaultItems: [newInstance], | |
withTitle: 'Open in nvim', | |
withPrompt: 'Please select which instance of nvim you’d like to open the file' + (files.length > 1 ? 's' : '') + ' in:', | |
multipleSelectionsAllowed: false, | |
emptySelectionAllowed: false, | |
})[0]; | |
switch (answer) { | |
case undefined: | |
return; | |
case 'A new instance': | |
return openIn(files, {}); | |
default: | |
return app.doShellScript('/usr/local/bin/nvr --servername ' + sockets[answer] + ' --remote-tab ' + fileNames); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment