repl.js

January 20, 2020 · View on GitHub

import 'colors' import { Elm } from './Main.elm' import * as REPL from 'repl'

// I don't know if this actually does anything. I set this when I was fucking around // trying to support shift+Enter but that didn't work so here we are.... process.stdin.setRawMode(true) process.stdin.setEncoding('utf8')

const res = s => console.log(' : ' + s.grey) const err = s => console.log(' : ' + s.red)

const app = Elm.Main.init()

const send = input => { app.ports.stdOut.subscribe( function writeStdOut (output) { app.ports.stdOut.unsubscribe(writeStdOut) res(output.split('\n').filter(line => line !== '').join('\n'))

  repl.setPrompt('λ ')
  repl.displayPrompt(false)
}

)

app.ports.stdErr.subscribe( function writeStdErr (output) { app.ports.stdErr.unsubscribe(writeStdErr) err(output.split('\n').filter(line => line !== '').join('\n'))

  repl.setPrompt('λ ')
  repl.displayPrompt(false)
}

)

app.ports.stdIn.send(input) }

// The buffer stores the current lines if we're in multi-line input mode. This // happens automatically when the input ends with \. const buffer = [] const evaluate = (input, callback, repl) => { if (input.endsWith('\\n')) { buffer.push(input.substring(0, input.length - 2)) repl.setPrompt('| ') // Update the prompt to indicate mutli-line mode repl.displayPrompt(false) // Reset the cursor position

return

}

if (input === '') return

buffer.push(input) send(buffer.join('\n')) buffer.length = 0 }

const repl = REPL.start({ prompt: 'λ ', eval (input, context, file, callback) { evaluate(input, callback, repl) }, useColors: true, preview: true })