Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Created October 6, 2012 05:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lancejpollard/3843973 to your computer and use it in GitHub Desktop.
Save lancejpollard/3843973 to your computer and use it in GitHub Desktop.
Node.js REPL API (readline API, a lower level REPL module)

Node.js Readline API

Readline is a more robust REPL.

readline  = require('readline')
repl      = readline.createInterface(stdin, stdout, autocomplete)

repl.historyIndex = 0
repl.history = ['App.Post.all()']
repl.on 'attemptClose', ->
  repl.close()

repl.on 'close', ->
  stdin.destroy()

repl.on 'line', (line) ->

repl.setPrompt('node> ')
repl.prompt()

repl.terminal # true|false
# current line
repl.line
# cursor position on the line
repl.cursor = 0
repl._setRawMode(true)

repl.input # stream
repl.input.removeListener('keypress', onkeypress)
repl.output
repl.output.removeListener('resize', onresize)

repl.columns == repl.output.columns # number of columns in the terminal
repl._refreshLine()
repl.close()
repl.pause()
repl.resume()
repl.write()
repl.question(query, callback)
repl._moveCursor(0)
repl._getCursorPos()
repl._historyNext()
repl._historyPrev()
repl._line()
repl.clearLine()
repl._insertString(string)

repl._wordLeft()
repl._wordRight()
repl._deleteLeft()
repl._deleteRight()
repl._deleteWordLeft()
repl._deleteWordRight()
repl._deleteLineLeft()
repl._deleteLineRight()

# internal variable `kHistorySize` is 30, max length of repl history
# so you might want to customize `repl._addHistory()
repl._addHistory = ->
  return '' if @line.length == 0

  if @history.length == 0 || @history[0] != @line
    @history.unshift(@line)
    
    # Only store so many
    @history.pop() if @history.length > repl.maxHistory

  @historyIndex = -1
  @history[0]

# then set max history
repl.maxHistory = 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment