ghostloda / infinite

用于开发交互式 CLI(tui,terminal) 程序的组件库,提供一系列开箱即用的组件 | A component library for developing interactive CLI(tui,terminal) programs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

infinite

🌊 用于开发交互式 CLI(tui,terminal) 程序的组件库.
go report card release

demo

中文 | English

Features

  • 提供一系列开箱即用的组件
    • autocomplete
    • progress bar / progress-bar group
    • multi/single select
    • spinner
    • confirm
    • input
  • 支持 window/linux (我现在只有这两种操作系统)
  • 可定制,你可以替换组件中的某些选项或方法为你自己的实现
  • 可组合,你可以将一个或多个基础组件联合在一起使用
    • autocompleteinputselection 组成
    • selection 通过嵌入input 来实现过滤功能.
    • ...

Best Practices

  1. 通过消息来更新状态,也就是通过program.Send(msg)来发送消息,Update监听并进行状态更新,最后通过View来反馈结果.
  2. ...

Install

go get github.com/fzdwx/infinite

Showcase

Combined demo

一个 progressspinner 组合使用的demo

demo

代码

Progress group

demo

代码
package main

import (
	"github.com/fzdwx/infinite/components"
	"github.com/fzdwx/infinite/components/progress"
	"time"
)

func main() {
	cnt := 10

	group := progress.NewGroupWithCount(10).
		AppendRunner(func(progress *components.Progress) func() {
			total := cnt
			cnt += 1
			progress.WithTotal(int64(total)).
				WithDefaultGradient()

			return func() {

				for i := 0; i < total+1; i++ {
					progress.IncrOne()
					sleep()
				}

				for i := 0; i < total; i++ {
					progress.DecrOne()
					sleep()
				}

				for i := 0; i < total+1; i++ {
					progress.IncrOne()
					sleep()
				}
			}
		})
	group.Display()
}

func sleep() {
	time.Sleep(time.Millisecond * 100)
}

Multiple select

demo

代码
package main

import (
	inf "github.com/fzdwx/infinite"
	"github.com/fzdwx/infinite/color"
	"github.com/fzdwx/infinite/components"
	"github.com/fzdwx/infinite/components/selection/multiselect"
	"github.com/fzdwx/infinite/style"
)

func main() {
	input := components.NewInput()
	input.Prompt = "Filtering: "
	input.PromptStyle = style.New().Bold().Italic().Fg(color.LightBlue)

	_, _ = inf.NewMultiSelect([]string{
		"Buy carrots",
		"Buy celery",
		"Buy kohlrabi",
		"Buy computer",
		"Buy something",
		"Buy car",
		"Buy subway",
	},
		multiselect.WithFilterInput(input),
	).Display("select your items!")
}

Spinner

demo

代码
package main

import (
	inf "github.com/fzdwx/infinite"
	"github.com/fzdwx/infinite/components"
	"github.com/fzdwx/infinite/components/spinner"
	"time"
)

func main() {
	_ = inf.NewSpinner(
		spinner.WithShape(components.Dot),
		//spinner.WithDisableOutputResult(),
	).Display(func(spinner *spinner.Spinner) {
		for i := 0; i < 10; i++ {
			time.Sleep(time.Millisecond * 100)
			spinner.Refreshf("hello world %d", i)
		}

		spinner.Finish("finish")

		spinner.Refresh("is finish?")
	})

	time.Sleep(time.Millisecond * 100 * 15)
}

Input text

demo

代码
package main

import (
	"fmt"
	inf "github.com/fzdwx/infinite"
	"github.com/fzdwx/infinite/components/input/text"
	"github.com/fzdwx/infinite/theme"
)

func main() {

	i := inf.NewText(
		text.WithPrompt("what's your name? "),
		text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
		text.WithPlaceholder(" fzdwx (maybe)"),
	)

	_ = i.Display()

	fmt.Printf("you input: %s\n", i.Value())
}

Confirm with Input

demo

代码
package main

import (
	"fmt"
	inf "github.com/fzdwx/infinite"
	"github.com/fzdwx/infinite/components/input/confirm"
)

func main() {

	c := inf.NewConfirm(
		confirm.WithDefaultYes(),
		confirm.WithDisplayHelp(),
	)

	c.Display()

	if c.Value() {
		fmt.Println("yes, you are.")
	} else {
		fmt.Println("no,you are not.")
	}
}

Confirm With Selection

Image

代码
package main

import (
	"fmt"
	inf "github.com/fzdwx/infinite"
)

func main() {

	val, _ := inf.NewConfirmWithSelection(
		//confirm.WithDisOutResult(),
	).Display()

	fmt.Println(val)
}

所有示例

Build with

所有依赖

License

MIT

About

用于开发交互式 CLI(tui,terminal) 程序的组件库,提供一系列开箱即用的组件 | A component library for developing interactive CLI(tui,terminal) programs.

License:MIT License


Languages

Language:Go 100.0%