Skip to content

Instantly share code, notes, and snippets.

@dtinth
Last active January 21, 2016 16:41
Show Gist options
  • Save dtinth/35422de266d36402f0dd to your computer and use it in GitHub Desktop.
Save dtinth/35422de266d36402f0dd to your computer and use it in GitHub Desktop.
My Atom hack for speed coding (for CodeHew 2016)

My Atom hack (init script) for speed programming in Ruby. This code is very ugly but works.

Features

Upon saving,

  • Ruby code in the specified directory is run.
  • Test input data (after 1st __END__ but before 2nd __END__) is sent to the script via stdin.
  • Output and/or errors are displayed in a popup.
  • Program output is compared to test output data (after 2nd __END__). If it matches, then the popup becomes green.
  • If there is a script error, highlight the line causing the error.

Screenshot

Screenshot

createOutput = () ->
for oldElements in document.querySelectorAll('.outputwow')
oldElements.style.display = 'none'
el = document.createElement('pre')
el.classList.add('outputwow')
el.setAttribute('style', 'position:fixed;bottom:1em;left:1em;right:32%;background:rgba(53,52,51,0.7);color:#e9e8e7;font:15px Menlo;padding:1em;z-index:9999999;border:2px solid #8b8685;border-radius:1em;overflow:auto;max-height:20em;')
document.body.appendChild(el)
clear = ->
el.parentNode.removeChild(el)
setTimeout clear, 3000
span = (color) -> (text) ->
s = document.createElement('span')
s.style.color = color
s.appendChild document.createTextNode(text)
el.appendChild s
return {
err: span('#f77')
out: span('#e9e8e7')
fail: -> el.style.borderColor = '#f55'
win: -> el.style.borderColor = '#5f5'
}
onSave = (editor) ->
return unless editor.getPath().match(/codehew\/[^\/]+\//)
{ spawn } = require('child_process')
data = editor.getText().split('__END__')
setTimeout ->
out = createOutput()
res = ''
err = ''
child = spawn('/Users/dtinth/Projects/codehew/run.sh', [ editor.getPath() ])
child.stdout.setEncoding 'utf8'
autokill = ->
if res.length + err.length > 16384
child.kill()
child.stdout.on 'data', (data) ->
res += data
out.out(data)
autokill()
child.stderr.setEncoding 'utf8'
child.stderr.on 'data', (data) ->
err += data
out.err(data)
autokill()
child.on 'close', (code) ->
out.fail() if code != 0
out.win() if res.trim() == String(data[2]).trim()
for line in err.split('\n').map((x) -> x.trim())
if line.startsWith(editor.getPath())
lnom = line.match(/:(\d+)/)
if lnom
lno = +lnom[1] - 1
mark = editor.markBufferRange([[lno, 0], [lno, 0]], persistent: false, invalidate: 'touch')
editor.decorateMarker(mark, type: 'line', class: 'errrr', onlyHead: true)
setTimeout (-> mark.destroy()), 3000
if data.length > 1
child.stdin.write(data[1].trim() + '\n')
child.stdin.end()
setTimeout (-> child.kill()), 3000
atom.workspace.observeTextEditors (editor) ->
editor.onDidSave ->
onSave(editor)
#!/bin/bash
RUBY=/Users/dtinth/.rvm/wrappers/ruby-2.3.0/ruby
exec "$RUBY" "$1"
html /deep/ .errrr {
background: fade(#e55, 50%) !important;
color: #fcc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment