Skip to content

Commit fe09027

Browse files
committed
Create basic script provider
0 parents  commit fe09027

11 files changed

+198
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
npm-debug.log
3+
node_modules

LICENSE.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2014 Kyle Kelley
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Script runner for atom
2+
3+
Run snippets of scripts from within Atom.
4+
5+
To install simply run `apm install script`.
6+
7+
To use simply select some code and hit `cmd-i`. Your code will get run in
8+
a fresh instance of the interpreter used by your scripting language.
9+
10+
![](https://f.cloud.github.com/assets/836375/2300807/e6d04a3c-a109-11e3-93e2-94d86965546d.gif)
11+
12+
13+
## TODO
14+
15+
* Make mappings configurable
16+
* Add tests

keymaps/script.cson

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Keybindings require three things to be fully defined: A selector that is
2+
# matched against the focused element, the keystroke and the command to
3+
# execute.
4+
#
5+
# Below is a basic keybinding which registers on all platforms by applying to
6+
# the root workspace element.
7+
8+
# For more detailed documentation see
9+
# https://atom.io/docs/latest/advanced/keymaps
10+
'.workspace':
11+
'cmd-i': 'script:run-selection'

lib/script-view.coffee

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{ScrollView, BufferedProcess} = require 'atom'
2+
3+
# Runs a portion of a script through an interpreter and displays it line by line
4+
5+
module.exports =
6+
class ScriptView extends ScrollView
7+
8+
@content: ->
9+
@div class: 'script', tabindex: -1, =>
10+
@div class: 'output'
11+
12+
constructor: (interpreter, make_args) ->
13+
super
14+
@interpreter = interpreter
15+
@make_args = make_args
16+
17+
# TODO: Add language to title
18+
getTitle: -> "Script"
19+
20+
addLine: (line, out_type) ->
21+
console.log(line)
22+
console.log("wat")
23+
@find("div.output").append("<pre class='line #{out_type}'>#{line}</pre>")
24+
25+
runit: (code) ->
26+
command = @interpreter
27+
28+
args = @make_args(code)
29+
30+
# Default to where the user opened atom
31+
options =
32+
cwd: atom.project.getPath()
33+
34+
console.log("Running " + command + " " + args)
35+
36+
stdout = (output) => @addLine(output, "stdout")
37+
stderr = (output) => @addLine(output, "stderr")
38+
exit = (return_code) -> console.log("Exited with #{return_code}")
39+
40+
process = new BufferedProcess({command, args, options, stdout, stderr, exit})

lib/script.coffee

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ScriptView = require './script-view'
2+
3+
configUri = "atom://script"
4+
5+
module.exports =
6+
7+
activate: ->
8+
atom.project.registerOpener (uri) =>
9+
10+
interpreter = "coffee"
11+
makeargs = (code) -> ['-e', code]
12+
13+
@scriptView = new ScriptView(interpreter, makeargs) if uri is configUri
14+
15+
atom.workspaceView.command "script:run-selection", =>
16+
editor = atom.workspace.getActiveEditor()
17+
code = editor.getSelectedText()
18+
19+
if ! code?
20+
return
21+
22+
atom.workspaceView.open(configUri, split: 'right')
23+
@scriptView.runit(code)

menus/script.cson

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# See https://atom.io/docs/latest/creating-a-package#menus for more details
2+
'context-menu':
3+
'.overlayer':
4+
'Enable script': 'script:run-selection'
5+
6+
'menu': [
7+
{
8+
'label': 'Packages'
9+
'submenu': [
10+
'label': 'script'
11+
'submenu': [
12+
{ 'label': 'Toggle', 'command': 'script:run-selection' }
13+
]
14+
]
15+
}
16+
]

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "script",
3+
"main": "./lib/script",
4+
"version": "0.0.0",
5+
"private": false,
6+
"description": "Run some script from atom.",
7+
"activationEvents": [
8+
"script:run-selection"
9+
],
10+
"repository": "https://github.com/rgbkrk/atom-script",
11+
"license": "Apache 2",
12+
"engines": {
13+
"atom": ">0.50.0"
14+
},
15+
"dependencies": {}
16+
}

spec/script-spec.coffee

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
script = require '../lib/script'
2+
3+
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
4+
#
5+
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
6+
# or `fdescribe`). Remove the `f` to unfocus the block.
7+
8+
describe "script", ->
9+
activationPromise = null
10+
11+
beforeEach ->
12+
atom.workspaceView = new WorkspaceView
13+
activationPromise = atom.packages.activatePackage('script')
14+
15+
describe "when the script:toggle event is triggered", ->
16+
it "attaches and then detaches the view", ->
17+
expect(atom.workspaceView.find('.script')).not.toExist()
18+
19+
# This is an activation event, triggering it will cause the package to be
20+
# activated.
21+
atom.workspaceView.trigger 'script:toggle'
22+
23+
waitsForPromise ->
24+
activationPromise
25+
26+
runs ->
27+
expect(atom.workspaceView.find('.script')).toExist()
28+
atom.workspaceView.trigger 'script:toggle'
29+
expect(atom.workspaceView.find('.script')).not.toExist()

spec/script-view-spec.coffee

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ScriptView = require '../lib/script-view'
2+
{WorkspaceView} = require 'atom'
3+
4+
describe "ScriptView", ->
5+
it "has one valid test", ->
6+
expect("life").toBe "easy"

stylesheets/script.less

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// The ui-variables file is provided by base themes provided by Atom.
2+
//
3+
// See https://github.com/atom/atom-dark-ui/blob/master/stylesheets/ui-variables.less
4+
// for a full listing of what's available.
5+
@import "ui-variables";
6+
7+
.script {
8+
overflow: scroll;
9+
box-sizing: border-box;
10+
}
11+
12+
.script .output {
13+
background-color: #444;
14+
}
15+
16+
.stderr {
17+
color: red;
18+
}

0 commit comments

Comments
 (0)