GithubHelp home page GithubHelp logo

twin / go-choice Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 2.0 3.82 MB

A very simple library for interactively selecting an option on a terminal

License: Apache License 2.0

Go 100.00%
go golang interactive

go-choice's Introduction

Hey, I'm TwiN! πŸ‘‹

Follow TwiN

Just another person trying to embrace their passion prevent the vicissitudes of life from taking away their passion.

Currently building my first SaaS during my free time: gatus.io

πŸ“ Latest Blog Articles

πŸ“Š Statistics

TwiN's Most Used Languages TwiN's GitHub Stats

go-choice's People

Contributors

dependabot[bot] avatar ilkeraksu avatar twin avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-choice's Issues

Support higher number of options

When there's more options than what can fit on the terminal, ... should be added as the first and last option and should allow users to change the page.

Too many screen refreshes

screen.Clear() is pretty laggy on Windows, the following hack should be considered:

Instead of always clearing before printing text, the text could be filled with empty spaces (this is what tcell.Clear() does in the background anyways)

Here's an example:

func render(screen tcell.Screen, question string, options []*Choice, config *Config, selectedChoice *Choice) {
	//screen.Clear()
	_, maximumThatCanBeDisplayed := screen.Size()
	lineNumber := 0
	questionLines := strings.Split(question, "\n")
	for _, line := range questionLines {
		printText(screen, 1, lineNumber, line, config.TextColor, config.BackgroundColor, config.SelectedTextBold)
		lineNumber += 1
	}
	min := selectedChoice.Id + len(questionLines)
	max := maximumThatCanBeDisplayed
	if selectedChoice.Id > max {
		min += 1
		max += 1
	}
	for _, option := range options {
		if option.Id <= (min+1)-maximumThatCanBeDisplayed && !(option.Id > (min+1)-maximumThatCanBeDisplayed) {
			continue
		}
		if option.Selected {
			printText(screen, 1, lineNumber, fmt.Sprintf("> %s", option.Value), config.SelectedTextColor, config.BackgroundColor, config.SelectedTextBold)
		} else {
			printText(screen, 1, lineNumber, fmt.Sprintf("  %s", option.Value), config.TextColor, config.BackgroundColor, config.SelectedTextBold)
		}
		lineNumber += 1
	}
	screen.Show()
}

func printText(screen tcell.Screen, x, y int, text string, fg, bg tcell.Color, bold bool) {
	for _, character := range text {
		screen.SetCell(x, y, tcell.StyleDefault.Background(bg).Foreground(fg).Bold(bold), character)
		x += runewidth.RuneWidth(character)
	}
	// instead of using screen.Clear(), we'll just update the ENTIRE line with empty spaces
	width, _ := screen.Size()
	for i := len(text); i < width; i++ {
		screen.SetCell(x, y, tcell.StyleDefault.Background(bg).Foreground(fg), ' ')
		x += runewidth.RuneWidth(' ')
	}
}

Note that when the list of choices doesn't fit in the screen, this will cause some issues (e.g. bottom lines aren't updated), so something need to be done about that

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.