首页 > 其他分享 >简单聊聊,使用Vue.js编写命令行界面,前端开发CLI的利器

简单聊聊,使用Vue.js编写命令行界面,前端开发CLI的利器

时间:2023-05-02 19:34:50浏览次数:49  
标签:core Vue temir Temir js 组件 import 前端开发

Temir

介绍

Temir,一个用Vue组件来编写命令行界面应用的工具.开发者只需要使用Vue就可以编写命令行应用,不需要任何额外的学习成本.

<script lang="ts" setup>
import { ref } from '@vue/runtime-core'
import { TBox, TText } from '@temir/core'
const counter = ref(0)
setInterval(() => {
  counter.value++
}, 100)
</script>

<template>
  <TBox>
    <TText color="green">
      {{ counter }} tests passed
    </TText>
  </TBox>
</template>

组件

Temir提供了一些基础组件帮助开发者编写与扩展命令行工具:

文本组件 (Text)

文本组件可以显示文本,将其样式更改为粗体、下划线、斜体或删除线.

<TText color="green">
  I am green
</TText>

<TText color="black" background-color="white">
  I am black on white
</TText>

<TText color="white">
  I am white
</TText>

<TText :bold="true">
  I am bold
</TText>

<TText :italic="true">
  I am italic
</TText>

<TText :underline="true">
  I am underline
</TText>

<TText :strikethrough="true">
  I am strikethrough
</TText>

<TText :inverse="true">
  I am inversed
</TText>

盒子组件 (Box)

<Box>是构建布局必不可少的Temir组件.就像在浏览器中<div style='display: flex'>.它提供了一些构建布局的常用属性,比如尺寸、内外边距、对齐方式等.

<template>
  <TBox justify-content="flex-start">
    <TText>X</TText>
  </TBox>
  // [X      ]

  <TBox justify-content="center">
    <TText>X</TText>
  </TBox>
  // [   X   ]

  <TBox justify-content="flex-end">
    <TText>X</TText>
  </TBox>
  // [      X]

  <TBox justify-content="space-between">
    <TText>X</TText>
    <TText>Y</TText>
  </TBox>
  // [X      Y]

  <TBox justify-content="space-around">
    <TText>X</TText>
    <TText>Y</TText>
  </TBox>
  // [  X   Y  ]
</template>

换行组件 (Newline)

添加一个或多个换行符(\n)。 必须在<Text>组件中使用。

<script>
import { TBox, TNewline, TText } from '@temir/core'
</script>

<template>
  <TBox>
    <TText>
      <TText color="green">
        Hello
      </TText>
      <TNewline />
      <TText color="red">
        World
      </TText>
    </TText>
  </TBox>
</template>

填充组件 (Spacer)

沿其包含布局的主轴展开的灵活空间。 作为填充元素之间所有可用空间的快捷方式,它非常有用。

例如,在具有默认伸缩方向(row)的<Box>中使用<Spacer>将把"Left"定位到左边,并将"Right"推到右边。

<script lang="ts" setup>
import { TBox, TSpacer, TText } from '@temir/core'
</script>

<template>
  <TBox>
    <TText>Left</TText>
    <TSpacer />
    <TText>Right</TText>
  </TBox>
</template>

超链接组件 (Link)

<script lang="ts" setup>
import { TBox, TText } from '@temir/core'
import TLink from '@temir/link'
</script>

<template>
  <TBox
    :margin="5"
    width="20"
    border-style="round"
    justify-content="center"
  >
    <TLink url="https://github.com">
      <TText color="yellow">
        Hi
      </TText>
      <TText color="cyan">
        Github
      </TText>
    </TLink>
  </TBox>
</template>

加载中组件 (Spinner)

<script lang="ts" setup>
import { TBox, TText } from '@temir/core'
import TSpinner from '@temir/spinner'
</script>

<template>
  <TBox
    :margin="5"
    width="20"
    border-style="round"
    justify-content="center"
  >
    <TText>
      <TText color="yellow">
        <TSpinner />
      </TText>
      Loading
    </TText>
  </TBox>
</template>

标签页组件 (Tab)

<script lang="ts" setup>
import { computed, ref } from '@vue/runtime-core'
import { TBox, TText } from '@temir/core'
import { TTab, TTabs } from '@temir/tab'

const tabs = ['Vue', 'React', 'Angular', 'Solid', 'Svelte']
const activeIndex = ref(0)
const selectedText = computed(() => tabs[activeIndex.value])
</script>

<template>
  <TBox>
    <TText>
      Selected Text :
      <TText color="red">
        {{ selectedText }}
      </TText>
    </TText>
  </TBox>

  <TBox>
    <TTabs :on-change="(index) => activeIndex = +index">
      <TTab v-for="item in tabs" :key="item">
        {{ item }}
      </TTab>
    </TTabs>
  </TBox>
</template>

选择组件

<script lang="ts" setup>
import TSelectInput from '@temir/select-input'

const items = [
  {
    label: 'Vue',
    value: 'Vue',
  },
  {
    label: 'Vite',
    value: 'Vite',
  },
  {
    label: 'Temir',
    value: 'Temir',
  },
]
function onSelect(value) {
  console.log('selected', value)
}
</script>

<template>
  <TSelectInput :items="items" :on-select="onSelect" />
</template>

安装

npm install @temir/core

使用

<script lang="ts" setup>
import { ref } from '@vue/runtime-core'
import { TBox, TText } from '@temir/core'
const counter = ref(0)
setInterval(() => {
  counter.value++
}, 100)
</script>

<template>
  <TBox>
    <TText color="green">
      {{ counter }} tests passed
    </TText>
  </TBox>
</template>

HMR支持

前面我们提到了开发者体验(DX),在现在的前端工程中,对开发者很有帮助且提效的就是HMR,这么香的东西Temir没有理由不拥有它,话不多说,直接展示:

开箱即用

使用Temir定制化CLI非常简单,我们提供了@temir/cli帮助你快速构建一个基于Temir的CLI.

mkdir my-temir-cli

cd my-temir-cli

touch main.ts

npm install @temir/cl

# Dev (开发)

temir main.ts

# Build (打包)

temir build main.ts

你可以通过下载这个 例子 来快速开始,你也可以打开 repl.it sandbox来在线体验和尝试它。

演示

Hi Temir

Borders

Table

Vitest

实现

  • createRenderer

Temir的实现主要得益于Vue3出色的跨平台能力,我们可以通过createRenderer API创建一个自定义渲染器,通过创建宿主环境中对应的Node和Element,并对元素进行增删改查操作.

  • Yoga

Vue提供了跑在命令行界面的接口,那我们就还缺少一个布局引擎就能把Vue
跑在命令行工具了.Temir使用了Yoga,一款Flexbox布局引擎.使用你在构建浏览器应用时使用过的类似CSS的属性,为你的CLI构建出色的用户界面。

标签:core,Vue,temir,Temir,js,组件,import,前端开发
From: https://www.cnblogs.com/tnth/p/17368087.html

相关文章

  • JS闭包理解
    概念在一个函数中嵌套另一个函数,嵌套(内部)函数对其容器(外部)函数是私有的。闭包是一个可以自己拥有独立的环境与变量的表达式(通常是函数,因为ES6有块级作用域的概念)闭包是指有权访问另一个函数作用域中变量的函数。闭包作用:可以在函数外部访问到函数内部的局部变量;让这些变量始......
  • Vue插槽
    1.匿名插槽----就是定义的时候不带name的插槽,使用的时候也不用带name总结:(1)一定要分清定义插槽和使用插槽格式。如上图A是在使用一个插槽的基础上再继续定义一个匿名插槽。(2)匿名插槽的使用可以加template也可以不加,例如图B和图C,那三个元素有没有用template包裹都一样会生效,也就......
  • 01_JS技巧
    1.判断对象数据类型示例代码如下constisType=(type)=>(target)=>`[object${type}]`===Object.prototype.toString.call(target)constisArray=isType('Array')constisObject=isType('Object')constisBoolean=isType('......
  • Vue指令学习
    1.指令的定义指令(Directives)是带有 v- 前缀的特殊attribute。指令的职责是,当表达式的值改变时,将其产生的连带影响,响应式地作用于DOM。指令还有一些基本的要了解的就是指令的修饰符(.native,.stop,.prevent等),动态参数(<a@[event]="doSomething">),缩写(:,@等)。这些都是......
  • 用vue2封装轮播图组件
    日常练习用vue2封装轮播图组件,传入图片信息数组。实现思想:图片组添加translate动画,通过轮播到第几张图片作为参数,让图片组整体移动。Carousel.vue<template><divclass="carousel"><divclass="carouselList"><!--图片列表--><ulclass="img&quo......
  • ChatGPT搭建Express与Vue3网页版带后台
    技术架构golang+node16+vite+vue3+expresscnetos7以上+宝塔面板文字搭建教程把service目录上传到宝塔www/wwwroot目录下面,放通3000端口,新建一个数据库,导入数据库,修改.env里的配置进入到service目录,执行nohup./linux.bin&测试后端api有没有正常启动,执行curlhttp:/......
  • vue学习 第七天 清除浮动 (clear:xxx)
    清除浮动 问题一、父元素不方便设置高度,子元素设置浮动(不占位置),父元素的高度会默认为0,就会影响下面的标准流的盒子。总结:子盒子浮动,父盒子失去高度,影响了整体布局1、清除浮动的原因由于浮动元素不再占用原文档流的......
  • vue学习 第六天 浮动 (float) 和 页面传统布局(标准流、浮动、定位)。
    浮动(float)1、传统网页布局的三种方式(3种)网页布局的本质---用CSS来摆放盒子。把盒子摆放到相应位置。CSS提供了三种传统布局方式(盒子如何进行排列顺序):普通流(标准流)、浮动、定位2、标准流(普通流/文档流)就是标签按照规定好默认方式排......
  • vue学习 第五天 css基础 --- ps操作 / 圆角边框(border-radius) / 阴影(盒子/文字)b
      ps基本操作1、ps的基本操作2、ps快捷操作的位置3、样式书写习惯 4、样式设置的小细节(注意)1、图片设置width:100%这样图片的宽度就不会超过父容器的宽度。2、块元素没有设置宽度,给margin左右是没有效果的。......
  • unity发布到4399的webgl模式问题:FRAMEWORK.JS中的WEBREQUEST_SEND括号内的函数(不能有
    在发布4399的时候,之前遇到过这个问题,解决方法当然就是删除这个函数啦。步骤也很简单,但是刚开始摸不着头脑搞了好久,最后发现发布的时候有个加密选项,选择不加密,后面build的文件里面就可以进行打开修改,按照要求修改函数即可。......