Skip to content

Building block for CLI environments for xterm.js

License

Notifications You must be signed in to change notification settings

bbbottle/xterm-js-shell

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xterm-js-shell

Building block for CLI environments for xterm.js

<script src="//unpkg.com/xterm-js-shell/bundle.js"></script>

-- OR --

npm install --save xterm-js-shell

const XtermJSShell = require('xterm-js-shell/bundle.js')
const terminal = new Terminal()

const shell = new XtermJSShell(terminal)

shell
  .command('help', async (shell) => {
    await shell.printLine(`
Try running one of these commands:
${shell.commands.map((command) => ` - ${command}`).join('\n')}

`)
  })
  .command('curl', async (shell, [ url ]) => {
    const response = await fetch(url)

    const text = await response.text()

    await shell.print(text)
  })
  .command('echo', async (shell, args) => {
    let message = null

    if (args.length) await shell.printLine(args.join(' '))

    // Loop until they hit enter without typing anything
    while (message = await shell.readLine('')) {
      await shell.printLine(message)
    }
  })
  .command('confirm', async (shell) => {
    const char = await shell.readChar('Y/n?')

    await shell.printLine(char)
  })
  .command('ssh', async (shell, {url}) => {
    // For use with https://github.com/RangerMauve/websocket-shell-service

    if(!url) url = 'ws:localhost:8080'

    const socket = new WebSocket(url)

    let closed = false

    socket.onclose = () => {
      closed = true
      shell.printLine(`Connection to ${url} closed`)
    }

    socket.onmessage = ({data}) => {
      shell.print(data)
    }

    for await(let data of shell.readStream()) {
      if(closed) break
      socket.send(data)
    }
  })

// Start the Read-Eval-Print-Loop
shell.repl()

terminal.open(someElement)

Features:

  • Takes an xterm.js terminal instance
  • Keyboard navigation, history, tab completion using local-echo
  • Able to define commands with a name and an async function or an async generator
  • Commands take a shell instance, and args array
  • Commands should be able to readLine, readChar from the shell
  • Commands can yield data from the generator for output, or invoke print / printLine
  • Commands should be abele to read raw data from the terminal as a stream
  • After a command resolves or rejects, it's shell should throw whenever there's input
  • Commands should be able to "take over" the terminal to prevent default processing
    • Useful for stuff like SSH
    • Shell will take back control after the program exits
  • Able to detach shell from the terminal

About

Building block for CLI environments for xterm.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 96.5%
  • HTML 3.5%