Skip to content

示例

TIP

这里是一些示例,可以帮助你快速了解如何使用各个组件。

autocomplete path

一个基于 autocomplete 的路径补全器。

demo

代码
go
package main

import (
	"github.com/duke-git/lancet/v2/slice"
	"github.com/fzdwx/infinite/components"
	"github.com/sahilm/fuzzy"
	"path/filepath"
	"sort"
)

func main() {
	var f components.Suggester = func(valCtx components.AutocompleteValCtx) ([]string, bool) {
		cursorWord := valCtx.CursorWord()
		files, err := filepath.Glob(cursorWord + "*")
		if err != nil {
			return nil, false
		}

		matches := fuzzy.Find(cursorWord, files)
		if len(matches) == 0 {
			return nil, false
		}

		sort.Stable(matches)

		suggester := slice.Map[fuzzy.Match, string](matches, func(index int, item fuzzy.Match) string {
			return files[item.Index]
		})
		return suggester, true
	}

	c := components.NewAutocomplete(f)

	components.NewStartUp(c).Start()
}

progress-bar group

使用 progress-bar group 同时运行多个 progress-bar

demo

代码
go
package main

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

func main() {
	cnt := 10

	if err := progress.NewGroupWithCount(10).
		AppendRunner(run(cnt)).Display(); err != nil {
		panic(err)
	}
}

func run(cnt int) func(progress *components.Progress) func() {
	return 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()
			}
		}
	}
}

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

download file

一个使用 progress bar 下载文件的demo demo

代码
go
package main

import (
	"flag"
	"github.com/fzdwx/infinite/components"
	"github.com/fzdwx/infinite/components/progress"
	"net/http"
	"os"
	"path"
)

var urlF = flag.String("d", "", "download url")

func init() {
	flag.Parse()
}

func main() {
	url := *urlF
	progress.NewGroupWithCount(1).
		AppendRunner(func(pro *components.Progress) func() {
			resp, err := http.Get(url)
			if err != nil {
				pro.Println("get error", err)
				resp.Body.Close()
				return func() {}
			}
			pro.WithTotal(resp.ContentLength)

			return func() {
				defer resp.Body.Close()

				dest, err := os.OpenFile(path.Base(url), os.O_CREATE|os.O_WRONLY, 0o777)
				defer dest.Close()
				if err != nil {
					pro.Println("dest open error", err)
					return
				}

				_, err = progress.StartTransfer(resp.Body, dest, pro)
				if err != nil {
					pro.Println("trans error", err)
				}
			}
		}).Display()
}

multiple select

一个多选的示例。

demo

代码
go
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

使用 spinner 以及在运行中刷新输入提示文字。

demo

代码
go
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

代码
go
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.WithDefaultValue("fzdwx (maybe)"),
	)

	val, _ := i.Display()

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

confirm

with input

使用 input 实现的 confirm

demo

代码
go
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.")
	}

}

with selection

使用 selection 实现的 confirm

demo

代码
go
package main

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

func main() {

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

	fmt.Println(val)
}

input original demo

这是一个最原始的 input demo,直接使用startUp 启动 input

demo

代码
go
package main

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

func main() {
	input := components.NewInput()

	if err := components.NewStartUp(input).Start(); err != nil {
		panic(err)
	}

	fmt.Println("input:", input.Value())
}