Giter Club home page Giter Club logo

vue-command's Introduction

vue-command

A fully working, most feature-rich Vue.js terminal emulator. See the demo.

Features

  • Parse arguments with yargs-parser
  • Search history (with /)
  • White/Dark theme support
  • Autocompletion resolver (with )
  • Supports asynchronous commands

Installation

$ npm i vue-command --save

Properties

Property Type Default Required Description
autocompletion-resolver Function null No Gets the current input as first and cursor position as the second argument. Must return the whole command
built-in Object {} No See built-in section
commands Object Yes See commands section
help-text String Type help No Sets the placeholder
help-timeout Number 4000 No Sets the placeholder timeout
hide-bar Boolean false No Hides the bar
hide-prompt Boolean false No Hides the prompt
intro String Fasten your seatbelts! No Sets the intro
not-found String not found No Sets the command not found text
prompt String ~neil@moon:# No Sets the prompt
show-help Boolean false No Shows the placeholder
show-intro Boolean false No Shows the intro
title String neil@moon: ~ No Sets the title
white-theme Boolean false No Enables the white theme
yargs-options Object {} No Sets the yargs options

Commands

commands is an object containing key-value pairs where key is the command and the value is a function that will be called with the yargs arguments. The function can return a Promise resolving to a HTML String, that will be used as the output or a Vue.js component, which you can use for more complex functions.

In your component, you have access to the following instance properties:

Name Type Property Description
this.$_arguments Object Computed Parsed yargs arguments
this.$_done Function Method Once your command is finished, call this.$_done() to allow the user to enter a new command. Make sure your component doesn't change from this point on. Also leaves fullscreen mode
this.$_executeCommand(command: String) Function Method After executing this.$_done(), you can use this method to run a subsequent command
this.$_isRunning Boolean Computed Indicates whether your command is still running or if it has terminated
this.$_setIsFullscreen(isFullscreen: Boolean) Function Method Toggle if your command will be the only visible element in the shell

Built-in

Key-value pairs where key is command and value is function with yargs arguments and $data from instance. Function should return String or Promise that resolves to String.

Events

Event Type Description
input String Emits the current input
execute String Emits the whole executing command
executed String Emits after command execution

Usage

You can either return a String or a Vue.js component.

String

Returns the color of the Pokémon "Pikachu".

<template>
  <vue-command
    :yargs-options="{ alias: { color: ['colour'] } }"
    :commands="commands"
  />
</template>

<script>
import VueCommand from 'vue-command'
import 'vue-command/dist/vue-command.css'

export default {
  components: {
    VueCommand
  },

  data: () => ({
    commands: {
      // yargs arguments
      pokedex: ({ color, _ }) => {
        if (color && _[1] === 'pikachu') {
          return 'yellow'
        }
        
        // Return help since no match
        return `Usage: pokedex pokemon [option]<br><br>

        Example: pokedex pikachu --color
        `
      }
    }
  })
}
</script>

<style lang="scss">
  .vue-command {
    .term {
      -webkit-border-radius: 8px;
      -moz-border-radius: 8px;
      border-radius: 8px;
    }

    .term-std {
      min-height: 300px;
      max-height: 300px;
      overflow-y: scroll;
    }
  }
</style>

Component

Simulates the Nano editor available in many shells.

<template>
  <div v-if="$_isRunning">
    <textarea 
      ref="nano" 
      @keydown.ctrl.88="$_done()">
      This is a text editor! Press Ctrl + X to leave.
    </textarea>
  </div>
</template>

<script>
export default {
  mounted () {
    this.$_setIsFullscreen(true)
    this.$refs.nano.focus()
  }
}
</script>

<style lang="scss" scoped>
div,
textarea {
  display: block;
  width: 100%;
  height: 100%;
}
</style>

Now the command has to return the component.

<template>
  <vue-command :commands="commands" />
</template>

<script>
import VueCommand from 'vue-command'
import 'vue-command/dist/vue-command.css'

import nano from './NanoEditor.vue'

export default {
  components: {
    VueCommand
  },

  data: () =>  ({
    commands: { 
      nano: () => nano 
    }
  })
}
</script>

Browser support

This library uses the ResizeObserver to track if the terminals inner height changes. You may need a pollyfill to support your target browser.

Projects using vue-command

Author

Julian Claus and contributors. Special thanks to krmax44 for the amazing work!

License

MIT

vue-command's People

Contributors

ndabap avatar krmax44 avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar tobiasbeck avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.