Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature: Initial commit
- Loading branch information
JarrodCTaylor
committed
Nov 18, 2013
0 parents
commit f46208c
Showing
8 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Jarrod Taylor | ||
<jarrod [dot] c [dot] taylor [at] gmail [dot] com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# vim-shell-executor | ||
|
||
A simple way to execute Vim buffer contents with a shell command and view the results in a split window. | ||
|
||
I find this most useful for SQL queries, however this will work with any program that can be called from the shell. | ||
|
||
## The Plugin In Action | ||
|
||
![executor_demo](https://f.cloud.github.com/assets/4416952/1560433/ec064cf4-5005-11e3-81ea-c1b7fb477915.gif) | ||
|
||
## How it works | ||
|
||
The plugin provides two commands. | ||
` | ||
ExecuteBuffer | ||
ExecuteSelction | ||
` | ||
These commands allow you to execute either the entire buffer or the visually selected region. The first line | ||
passed to the plugin is the shell command that will be used to run the remaing lines of code. For example | ||
to run a mysql query the first line would be the information you would enter to connect to the database. | ||
The remaining lines are the query that you would like to execute. The example shown in the gif above is | ||
``` shell | ||
mysql -t -u root example | ||
select * from mysql_example_table; | ||
``` | ||
## Installation | ||
Use your plugin manager of choice. | ||
- [Pathogen](https://github.com/tpope/vim-pathogen) | ||
- `git clone https://github.com/JarrodCTaylor/vim-shell-executor ~/.vim/bundle/vim-shell-executor` | ||
- [Vundle](https://github.com/gmarik/vundle) | ||
- Add `Bundle 'https://github.com/JarrodCTaylor/vim-shell-executor'` to .vimrc | ||
- Run `:BundleInstall` | ||
- [NeoBundle](https://github.com/Shougo/neobundle.vim) | ||
- Add `NeoBundle 'https://github.com/JarrodCTaylor/vim-shell-executor'` to .vimrc | ||
- Run `:NeoBundleInstall` | ||
- [vim-plug](https://github.com/junegunn/vim-plug) | ||
- Add `Plug 'https://github.com/JarrodCTaylor/vim-shell-executor'` to .vimrc | ||
- Run `:PlugInstall` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
*vim-shell-executor.txt* | ||
|
||
=============================================================================== | ||
CONTENTS *vim-shell-executor* | ||
|
||
1. Intro ....................................... |vim-shell-executor-intro| | ||
2. Requirements ......................... |vim-shell-executor-requirements| | ||
3. Usage ....................................... |vim-shell-executor-usage| | ||
4. Licence ................................... |vim-shell-executor-licence| | ||
=============================================================================== | ||
1. Intro *vim-shell-executor-intro* | ||
|
||
A simple way to execute Vim buffer contents with a shell command and view the | ||
results in a split window. | ||
|
||
I find this most useful for SQL queries, however this will work with any program | ||
that can be called from the shell. | ||
|
||
2. Requirements *vim-shell-executor-requirements* | ||
|
||
No additional requires are needed for this plugin. Only the shell programs you | ||
would like to run. | ||
|
||
3. Usage *vim-shell-executor-usage* | ||
|
||
The plugin provides two commands. | ||
` | ||
ExecuteBuffer | ||
ExecuteSelction | ||
` | ||
These commands allow you to execute either the entire buffer or the visually | ||
selected region. The first line passed to the plugin is the shell command that | ||
will be used to run the remaining lines of code. For example to run a mysql query | ||
the first line would be the information you would enter to connect to the database. | ||
The remaining lines are the query that you would like to execute. For example | ||
to run a mysql query on the example database would could use the following. | ||
``` shell | ||
mysql -t -u root example | ||
select * from mysql_example_table; | ||
``` | ||
|
||
4. Licence *vim-shell-executor-licence* | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013 Jarrod Taylor | ||
<jarrod [dot] c [dot] taylor [at] gmail [dot] com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import os | ||
import stat | ||
import unittest | ||
import vim_shell_executor as sut | ||
|
||
INPUT_FILE = "/tmp/input" | ||
ERROR_LOG = "/tmp/error.log" | ||
RESULTS_FILE = "/tmp/results" | ||
|
||
|
||
class VimShellExecutorTests(unittest.TestCase): | ||
|
||
def tearDown(self): | ||
self.delete_if_present(ERROR_LOG) | ||
self.delete_if_present(INPUT_FILE) | ||
self.delete_if_present(RESULTS_FILE) | ||
|
||
def test_get_program_output_from_buffer_contents_returns_properly_formatted_results_when_given_valid_python_input(self): | ||
buffer_contents = ["python", "name = 'Jarrod'", "", "def hello():", " print('Hello {0}'.format(name))", "", "hello()"] | ||
return_result = sut.get_program_output_from_buffer_contents(buffer_contents) | ||
expected_result = ["Hello Jarrod"] | ||
self.assertEqual(expected_result, return_result) | ||
|
||
def test_get_program_output_from_buffer_contents_raises_error_when_given_invalid_input(self): | ||
buffer_contents = ["not_a_program", "fail = 27"] | ||
with self.assertRaises(Exception): | ||
sut.get_program_output_from_buffer_contents(buffer_contents) | ||
|
||
def test_write_buffer_contents_to_file_writes_correct_contents_to_desired_file(self): | ||
buffer_contents = ["var example = function() {", " console.log('this is an example');", "}"] | ||
sut.write_buffer_contents_to_file(RESULTS_FILE, buffer_contents) | ||
with open(RESULTS_FILE, "r") as f: | ||
self.assertEqual(f.readlines(), [line + "\n" for line in buffer_contents]) | ||
|
||
def test_execute_file_with_specific_shell_program_populates_an_error_file_when_given_invalid_input_for_specified_shell_command(self): | ||
sut.write_buffer_contents_to_file(INPUT_FILE, ["(def name 'Jarrod')", "(println name)"]) | ||
sut.execute_file_with_specified_shell_program('python') | ||
self.assertTrue(os.stat(ERROR_LOG)[stat.ST_SIZE] > 0) | ||
|
||
def test_check_for_errors_raises_an_exception_if_there_is_a_non_empty_error_log(self): | ||
sut.write_buffer_contents_to_file(INPUT_FILE, ["(def name 'Jarrod')", "(println name)"]) | ||
sut.execute_file_with_specified_shell_program('python') | ||
self.assertTrue(os.stat(ERROR_LOG)[stat.ST_SIZE] > 0) | ||
with self.assertRaises(Exception): | ||
sut.check_for_errors() | ||
|
||
def test_check_for_errors_does_not_raise_an_exception_if_there_is_an_empty_error_log(self): | ||
buffer_contents = ["name = 'Jarrod'", "", "def hello():", " print('Hello {0}'.format(name))", "", "hello()"] | ||
sut.write_buffer_contents_to_file(INPUT_FILE, buffer_contents) | ||
sut.execute_file_with_specified_shell_program('python') | ||
self.assertTrue(os.stat(ERROR_LOG)[stat.ST_SIZE] == 0) | ||
with self.assertRaises(AssertionError): | ||
with self.assertRaises(Exception): | ||
sut.check_for_errors() | ||
|
||
def read_file_to_string(self, file_to_read): | ||
with open(file_to_read, "r") as f: | ||
return f.readlines() | ||
|
||
def delete_if_present(self, file_name): | ||
if os.path.exists(file_name): | ||
os.remove(file_name) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import subprocess | ||
import os | ||
import stat | ||
|
||
INPUT_FILE = "/tmp/input" | ||
ERROR_LOG = "/tmp/error.log" | ||
RESULTS_FILE = "/tmp/results" | ||
|
||
|
||
def get_program_output_from_buffer_contents(buffer_contents): | ||
write_buffer_contents_to_file(INPUT_FILE, buffer_contents[1:]) | ||
execute_file_with_specified_shell_program(buffer_contents[0]) | ||
check_for_errors() | ||
new_buf = read_file_lines(RESULTS_FILE) | ||
return new_buf | ||
|
||
|
||
def write_buffer_contents_to_file(file_name, contents): | ||
with open(file_name, "w") as f: | ||
for line in contents: | ||
f.write(line + "\n") | ||
|
||
|
||
def execute_file_with_specified_shell_program(shell_command): | ||
try: | ||
subprocess.check_call("{0} < {1} > {2} 2> {3}".format(shell_command, INPUT_FILE, RESULTS_FILE, ERROR_LOG), shell=True) | ||
except: | ||
pass | ||
|
||
|
||
def check_for_errors(): | ||
if os.stat("/tmp/error.log")[stat.ST_SIZE]: | ||
raise Exception("Something appears to have gone wrong. Check '/tmp/error.log' or try asking an adult for help.") | ||
|
||
|
||
def read_file_lines(file_to_read): | ||
with open(file_to_read, "r") as f: | ||
return [l.rstrip('\n') for l in f.readlines()] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
" -------------------------------- | ||
" Add our plugin to the path | ||
" -------------------------------- | ||
python import sys | ||
python import vim | ||
python sys.path.append(vim.eval('expand("<sfile>:h")')) | ||
|
||
" -------------------------------- | ||
" Function(s) | ||
" -------------------------------- | ||
function! ExecuteWithShellProgram(selection_or_buffer) | ||
python << endPython | ||
from vim_shell_executor import * | ||
|
||
def create_new_buffer(contents): | ||
delete_old_output_if_exists() | ||
vim.command('aboveleft split executor_output') | ||
vim.command("let s:executorOutputBuffer = bufnr('%')") | ||
vim.command('normal! ggdG') | ||
vim.command('setlocal filetype=text') | ||
vim.command('setlocal buftype=nowrite') | ||
vim.command('call append(0, {0})'.format(contents)) | ||
|
||
def delete_old_output_if_exists(): | ||
execute_buffer_exists = int(vim.eval('exists("s:executorOutputBuffer")')) | ||
if execute_buffer_exists: | ||
vim.command('bdelete! executor_output') | ||
|
||
def get_visual_selection(): | ||
buf = vim.current.buffer | ||
starting_line_num, col1 = buf.mark('<') | ||
ending_line_num, col2 = buf.mark('>') | ||
return vim.eval('getline({}, {})'.format(starting_line_num, ending_line_num)) | ||
|
||
def get_correct_buffer(buffer_type): | ||
if buffer_type == "buffer": | ||
return vim.current.buffer | ||
elif buffer_type == "selection": | ||
return get_visual_selection() | ||
|
||
def execute(): | ||
buf_type = get_correct_buffer(vim.eval("a:selection_or_buffer")) | ||
shell_program_output = get_program_output_from_buffer_contents(buf_type) | ||
create_new_buffer(shell_program_output) | ||
|
||
execute() | ||
|
||
endPython | ||
endfunction | ||
|
||
" -------------------------------- | ||
" Expose our commands to the user | ||
" -------------------------------- | ||
command! ExecuteBuffer call ExecuteWithShellProgram("buffer") | ||
command! -range ExecuteSelection call ExecuteWithShellProgram("selection") |