vue-command

授权协议 MIT License
开发语言 SHELL
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 通正平
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

vue-command

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

Features

  • Parses arguments with getopts
  • Supports asynchronous commands
  • Browse history (with /)
  • Autocompletion resolver (with )
  • Customize terminal with slots
  • Search history (with Ctrl + r)

Installation

$ npm install vue-command --save

Usage

Let's start with a very simple example. We want to send "Hello world" to Stdout when entering hello-world.

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

<script>
import VueCommand, { createStdout } from 'vue-command'
import 'vue-command/dist/vue-command.css'

export default {
  components: {
    VueCommand
  },

  data: () =>  ({
    commands: { 
      'hello-world': () => createStdout('Hello world') 
    }
  })
}
</script>

Now a more complex one. Let's assume we want to build the Nano editor available in many shells.

We will use the provided environment variable to make sure the editor is only visible when this command is executing and inject a function called terminate to tell the terminal that the command has been finished when the user enters Ctrl + x. Furthermore, we inject the setIsFullscreen function to switch the terminal into fullscreen mode.

<template>
  <div v-if="environment.isExecuting">
    <textarea
      ref="nano"
      @keydown.ctrl.88="terminate">This is a text editor! Press Ctrl + x to leave.</textarea>
  </div>
</template>

<script>
export default {
  inject: ['setIsFullscreen', 'terminate'],

  created () {
    this.setIsFullscreen(true)
  },

  mounted () {
    this.$refs.nano.focus()
  }
}
</script>

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 NanoEditor from '@/components/NanoEditor.vue'

export default {
  components: {
    VueCommand
  },

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

Properties

There are two types of commands: Built-in and regular ones. In most cases regular commands are appropriate. Built-in commands provide higher flexibility, see section Built-in for more information.

Some properties can be changed by the terminal, therefore, the sync modifier has to be added.

Property Type Default Sync Description
autocompletion-resolver Function null No See Autocompletion resolver
built-in Object {} No See Built-in section
commands Object {} No See Commands section
cursor Number 0 Yes Sets the Stdin cursor position
event-listeners Array [EVENT_LISTENERS.autocomplete, EVENT_LISTENERS.history, EVENT_LISTENERS.search] No See Event listeners section
executed Set new Set() Yes Executed programs, see "Overwriting executed functions"
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
hide-title Boolean false No Hides the title
history Array [] Yes Executed commands
intro String Fasten your seatbelts! No Sets the intro
is-fullscreen Boolean false Yes Sets the terminal fullscreen mode
is-in-progress Boolean false Yes Sets the terminal progress status
not-found String not found No Sets the command not found text
parser-options Object {} No Sets the parser options
pointer Number 0 Yes Sets the command pointer
prompt String ~neil@moon:# No Sets the prompt
show-help Boolean false No Shows the placeholder
show-intro Boolean false No Shows the intro
stdin String '' Yes Sets the current Stdin
title String neil@moon: ~ No Sets the title

Commands

commands must be an object containing key-value pairs where key is the command and the value is a function that will be called with the getops arguments. The function can return a Promise and must return or resolve a Vue.js component. To return strings or nothing use one of the convenient helper methods:

Function Description
createStdout(content: String, isInnerText: Boolean, isEscapeHtml: Boolean, name: String, ...mixins: Array): Object Returns a Stdout component containing a span element with given inner content
createStderr(content: String, isEscapeHtml: Boolean, name: String, ...mixins: Array): Object Returns a Stderr component containing a span element with given inner content
createDummyStdout(name: String, ...mixins: Array): Object Returns a dummy Stdout to show a Stdin

Helper methods can be imported by name:

import { createStdout, createStderr, createDummyStdout } from 'vue-command'

If none of the helper methods is used, the command has to be manually terminated inside the component. Next to termination it's possible to inject the following functions to manipulate the terminal or signal an event:

Function Description
emitExecute Emit command execution event
emitExecuted Emit command executed event
emitInput(input: String) Emit the current input
setCursor(cursor: Number) Set cursor position
setIsFullscreen(isFullscreen: Boolean) Change if the terminal is in fullscreen mode
setIsInProgress(isInProgress: Boolean) Change if the terminal is in progress
setPointer(pointer: Number) Set command history pointer
setStdin(stdin: String) Set the current Stdin
terminate Executes common final tasks after command has been finished

Functions can be injected into your component by name:

inject: ['setIsFullscreen', 'setIsInProgress', 'terminate']

In your component you have access to a context and an environment variable. The environment variable contains the following properties (note that built-in commands have to take care by theirselves about the terminals state):

Property Description
isExecuting: Boolean Is the current component executing
isFullscreen: Boolean Is the terminal in fullscreen mode
isInProgress: Boolean Is any command active

The context variable contains the following properties:

Property Description
cursor: Number Copy of cursor position at Stdin
executed: Set Copy of executed programs
history: Array Copy of executed commands
parsed: Object Parsed getops arguments
pointer: Number Copy of history command pointer
stdin: String Copy of Stdin

Built-in

Built-in commands provide more control over the terminals behaviour. On the other side, they have to take care about every regular command step. As a matter of fact, regular commands are just calling helper methods or change properties which could be also called or changed by built-in commands. Regular commands can be seen as a facade to built-in commands.

Since built-in commands can capture any command, it's necessary to take care of autocompletion and the command not found experience.

The first argument that is called within the built-in command is the unparsed Stdin. It's possible to use a custom parser at this place. The second argument is the terminal instance. You can use the commandNotFound method if no built-in or regular command has been found.

To fully simulate a regular command circle a built-in command has to follow these steps:

  1. Call setIsInProgress with true to tell there is a command in progress
  2. Add the programm to the executed Set property
  3. Increase the history pointer with setPointer
  4. Execute actual task
  5. Push the Stdout component into the history property
  6. Call setIsInProgress with false to tell there is no command in progress anymore

Autocompletion resolver

It is possible to provide a function that is called when the user hits the key. This function needs to take care of the autocompletion experience and should make usage of properties like history and stdin. The following shows a possible, simple autocompletion function:

this.autocompletionResolver = () => {
  // Make sure only programs are autocompleted. See below for version with options
  const command = this.stdin.split(' ')
  if (command.length > 1) {
    return
  }

  const autocompleteableProgram = command[0]
  // Collect all autocompletion candidates
  let candidates = []
  const programs = [...Object.keys(this.commands)].sort()
  programs.forEach(program => {
    if (program.startsWith(autocompleteableProgram)) {
      candidates.push(program)
    }
  })

  // Autocompletion resolved into multiple results
  if (this.stdin !== '' && candidates.length > 1) {
    this.history.push({
      // Build table programmatically
      render: createElement => {
        const columns = candidates.length < 5 ? candidates.length : 4
        const rows = candidates.length < 5 ? 1 : Math.ceil(candidates.length / columns)

        let index = 0
        let table = []
        for (let i = 0; i < rows; i++) {
          let row = []
          for (let j = 0; j < columns; j++) {
            row.push(createElement('td', candidates[index]))
            index++
          }

          table.push(createElement('tr', [row]))
        }

        return createElement('table', { style: { width: '100%' } }, [table])
      }
    })
  }

  // Autocompletion resolved into one result
  if (candidates.length === 1) {
    this.stdin = candidates[0]
  }
}
Advanced version with option autocompletion
this.autocompletionResolver = () => {
  // Preserve cursor position
  const cursor = this.cursor

  // Reverse concatenate autocompletable according to cursor
  let pointer = this.cursor
  let autocompleteableStdin = ''
  while (this.stdin[pointer - 1] !== ' ' && pointer - 1 > 0) {
    pointer--
    autocompleteableStdin = `${this.stdin[pointer]}${autocompleteableStdin}`
  }

  // Divide by arguments
  const command = this.stdin.split(' ')

  // Autocompleteable is program
  if (command.length === 1) {
    const autocompleteableProgram = command[0]
    // Collect all autocompletion candidates
    const candidates = []
    const programs = [...Object.keys(this.commands)].sort()
    programs.forEach(program => {
      if (program.startsWith(autocompleteableProgram)) {
        candidates.push(program)
      }
    })

    // Autocompletion resolved into multiple results
    if (this.stdin !== '' && candidates.length > 1) {
      this.history.push({
        // Build table programmatically
        render: createElement => {
          const columns = candidates.length < 5 ? candidates.length : 4
          const rows = candidates.length < 5 ? 1 : Math.ceil(candidates.length / columns)

          let index = 0
          const table = []
          for (let i = 0; i < rows; i++) {
            const row = []
            for (let j = 0; j < columns; j++) {
              row.push(createElement('td', candidates[index]))
              index++
            }

            table.push(createElement('tr', [row]))
          }

          return createElement('table', { style: { width: '100%' } }, [table])
        }
      })
    }

    // Autocompletion resolved into one result
    if (candidates.length === 1) {
      // Mutating Stdin mutates the cursor, so we've to wait to push it to the end
      const unwatch = this.$watch(() => this.cursor, () => {
        this.cursor = cursor + (candidates[0].length - autocompleteableStdin.length + 0)

        unwatch()
      })

      this.stdin = candidates[0]
    }

    return
  }

  // Check if option might be completed already or option is last tokens
  if ((this.stdin[cursor] !== '' && this.stdin[cursor] !== ' ') && typeof this.stdin[cursor] !== 'undefined') {
    return
  }

  // Get the executable
  const program = command[0]

  // Check if any autocompleteable exists
  if (typeof this.options.long[program] === 'undefined' && typeof this.options.short[program] === 'undefined') {
    return
  }

  // Autocompleteable is long option
  if (autocompleteableStdin.substring(0, 2) === '--') {
    const candidates = []
    this.options.long[program].forEach(option => {
      // If only dashes are present, user requests all options
      if (`--${option}`.startsWith(autocompleteableStdin) || autocompleteableStdin === '--') {
        candidates.push(option)
      }
    })

    // Autocompletion resolved into one result
    if (candidates.length === 1) {
      const autocompleted = `${this.stdin.substring(0, pointer - 1)} --${candidates[0]}`
      const rest = `${this.stdin.substring(this.cursor)}`

      // Mutating Stdin mutates the cursor, so we've to wait to push it to the end
      const unwatch = this.$watch(() => this.cursor, () => {
        this.cursor = cursor + (candidates[0].length - autocompleteableStdin.length + 2)

        unwatch()
      })

      this.stdin = `${autocompleted}${rest}`

      return
    }

    // Autocompletion resolved into multiple result
    if (autocompleteableStdin === '--' || candidates.length > 1) {
      this.history.push({
        // Build table programmatically
        render: createElement => {
          const columns = candidates.length < 5 ? candidates.length : 4
          const rows = candidates.length < 5 ? 1 : Math.ceil(candidates.length / columns)

          let index = 0
          const table = []
          for (let i = 0; i < rows; i++) {
            const row = []
            for (let j = 0; j < columns; j++) {
              row.push(createElement('td', `--${candidates[index]}`))
              index++
            }

            table.push(createElement('tr', [row]))
          }

          return createElement('table', { style: { width: '100%' } }, [table])
        }
      })
    }

    return
  }

  // Autocompleteable is option
  if (autocompleteableStdin.substring(0, 1) === '-') {
    const candidates = []
    this.options.short[program].forEach(option => {
      // If only one dash is present, user requests all options
      if (`-${option}`.startsWith(autocompleteableStdin) || autocompleteableStdin === '-') {
        candidates.push(option)
      }
    })

    // Autocompletion resolved into one result
    if (candidates.length === 1) {
      const autocompleted = `${this.stdin.substring(0, pointer - 1)} -${candidates[0]}`
      const rest = `${this.stdin.substring(this.cursor)}`

      // Mutating Stdin mutates the cursor, so we've to wait to push it to the end
      const unwatch = this.$watch(() => this.cursor, () => {
        this.cursor = cursor + (candidates[0].length - autocompleteableStdin.length + 1)

        unwatch()
      })

      this.stdin = `${autocompleted}${rest}`

      return
    }

    // Autocompletion resolved into multiple result
    if (autocompleteableStdin === '-' || candidates.length > 1) {
      this.history.push({
        // Build table programmatically
        render: createElement => {
          const columns = candidates.length < 5 ? candidates.length : 4
          const rows = candidates.length < 5 ? 1 : Math.ceil(candidates.length / columns)

          let index = 0
          const table = []
          for (let i = 0; i < rows; i++) {
            const row = []
            for (let j = 0; j < columns; j++) {
              row.push(createElement('td', `-${candidates[index]}`))
              index++
            }

            table.push(createElement('tr', [row]))
          }

          return createElement('table', { style: { width: '100%' } }, [table])
        }
      })
    }
  }
}

Event listeners

Event listeners trigger terminal behaviour under certain conditions like pressing a button. Pass an array of event listeners you want to bind via the event-listeners property. This library provides three event listeners per default which can be imported:

  • Autocompletion: Autocompletion when pressing
  • History: Cycle through history with /
  • Search: Search history with Ctrl and r

An event listener is called with the Vue.js component instance as argument.

Slots

Bar

It's possible to fully customize the terminal bar using slots as shown in the following. Note: If using the bar slot, the properties hide-bar and title will be ignored.

<template>
  <vue-command>
    <div slot="bar">
      Pokedex
    </div>
  </vue-command>
</template>

Prompt

Customize the prompt with the prompt slot. Note: If using the prompt slot, the property prompt will be ignored and the CSS class term-ps has to be manually applied.

<template>
  <vue-command prompt="neil">
    <span
      class="term-ps" 
      slot="prompt">
      {{ prompt }} ready to take off:
    </span>
  </vue-command>
</template>

Events

Event Type Description Note
input String Emits the current input
execute String Emits when executing command Built-in commands have to manually emit this event
executed String Emits after command execution Built-in commands have to manually emit this event. All helper methods emit this event

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.

Overwriting executed functions

To track when the executed property has been mutated, this library overwrites the functions add, clear and delete of it. That means if you plan to overwrite the named Set functions by yourself, this library won't work.

Projects using vue-command

Chuck Norris API

The Chuck Norris jokes are comming from this API. This library has no relation to Chuck Norris or the services provided by the API.

Author

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

I apologize to some contributors that are not in the Git history anymore since I had to delete the repository because of problems with semantic-release.

License

MIT

 相关资料
  • 构造器 每个 Vue.js 应用都是通过构造函数 Vue 创建一个 Vue 的根实例 启动的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 MVVM 模式, Vue 的设计无疑受到了它的启发。因此在文档中经常会使用 vm 这个变量名表示 Vue 实例。 在实例化 Vue 时,需要传入一个选项对象,它可以包含数据、模板、挂载元素、方法、生命周期钩子等选项。全部的选

  • vue

    教程简介 本教程要实现一个简单的后台管理系统,包含登陆、数据列表、数据查询、列表分页、添加数据、修改数据和删除数据等功能,教程会从 Vue 入门开始讲解,包含 es6、Sass、Webpack、Bootstrap、jQuery 等技术,再到后台管理系统的一些常规功能,用 Vue 如何去实现。 也许会有人质疑 Vue 和 jQuery 的搭配,在我本人看来,jQuery 本身已很成熟,而且包含了很多

  • FAQ 哇,非常长的一页!是否意味着 Vue 2.0 已经完全不同了呢,是否需要从头学起呢,Vue 1.0 的项目是不是没法迁移了? 非常开心地告诉你,并不是!几乎 90% 的 API 和核心概念都没有变。因为本节包含了很多详尽的阐述以及许多迁移的例子,所以显得有点长。不用担心,你不必从头到尾把本节读一遍! 我该从哪里开始项目迁移呢? 首先,在当前项目下运行迁移工具。我们非常谨慎地把高级 Vue

  • Integration with Vue is easily done with the @tinymce/tinymce-vue package. To use it, install it with npm like this: npm install @tinymce/tinymce-vue For information on how to use the package, check

  • 本规范提供了一种统一的编码规范来编写 Vue.js 代码。这使得代码具有如下的特性: 其它开发者或是团队成员更容易阅读和理解。 IDEs 更容易理解代码,从而提供高亮、格式化等辅助功能 更容易使用现有的工具 更容易实现缓存以及代码包的分拆 要点 尽量使用ES2015,遵循CommonJs规范 切勿直接操作DOM,所以也应该避免使用jQuery库 data属性一定要是一个函数并且返回一个json对象

  • 全局配置 Vue.config 是一个对象,包含 Vue 的全局配置。可以在启动应用之前修改下列属性: silent 类型: boolean 默认值: false 用法: Vue.config.silent = true 取消 Vue 所有的日志与警告。 optionMergeStrategies 类型: { [key: string]: Function } 默认值: {} 用法: Vue.c