Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Area Update after return / or when typing in the terminal #422

Open
0cv opened this issue Nov 25, 2022 · 3 comments
Open

Area Update after return / or when typing in the terminal #422

0cv opened this issue Nov 25, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@0cv
Copy link

0cv commented Nov 25, 2022

I'm not sure whether it's a feature request or a bug report. I'm using the Area component (which renders a dynamic table) along with some interactive inputs which require a user input on the command line (not part of this library). Whenever the area renders, the cursor will move back to the beginning of the line. Also, if the user creates a new line in the terminal (by clicking on return), it will break the table (see below).

Simple example is just to use the clock of the example, then click a few times on return

area, _ := pterm.DefaultArea.Start() // Start the Area printer.
for i := 0; i < 10; i++ {
    str, _ := pterm.DefaultBigText.WithLetters(pterm.NewLettersFromString(time.Now().Format("15:04:05"))).Srender() // Save current time in str.
    str = pterm.DefaultCenter.Sprint(str) // Center str.
    area.Update(str) // Update Area contents.
    time.Sleep(time.Second)
}
area.Stop()

image

@0cv 0cv added the proposal Proposal to add a new feature to pterm label Nov 25, 2022
@MarvinJWendt
Copy link
Member

MarvinJWendt commented Nov 25, 2022

along with some interactive inputs which require a user input on the command line (not part of this library).

Why aren't you using the PTerm interactive printers, if I may ask?
https://docs.pterm.sh/printers/interactive

Also, if the user creates a new line in the terminal (by clicking on return), it will break the table (see below).

I think the only way to avoid letting users mess up the output, would be to disable user input while the area is running.

Do you use user input, while the Area is running?

If you want to disable user input, while the area is running, you could use this snippet:

func main() {
	area, _ := pterm.DefaultArea.Start()
	running := true // We will maybe expose area.IsActive so this wouldn't be needed
	go func() {
		keyboard.Listen(func(key keys.Key) (stop bool, err error) {
			return !running, nil
		})
	}()
	for i := 0; i < 10; i++ {
		str, _ := pterm.DefaultBigText.WithLetters(putils.LettersFromString(time.Now().Format("15:04:05"))).Srender()
		str = pterm.DefaultCenter.Sprint(str)
		area.Update(str)
		time.Sleep(time.Second)
	}
	area.Stop()
	running = false
}

This snippet uses https://atomicgo.dev/keyboard (PTerm also uses this internally for the interactive printers)

I hope this helps, let me know if you need more help :)

@0cv
Copy link
Author

0cv commented Nov 25, 2022

@MarvinJWendt thanks for the ideas!

So, just to be a bit more clear on the ask, the Table Area which updates dynamically, is a sort of monitoring table, not something which shall end after 10 iterations or 2 minutes or when a download is finished. The user may enter commands, while still monitoring this table - in an ideal scenario

1- Interactive Printer: I'm using an auto complete feature which I don't think is supported here. This being said and regardless of the autocomplete, I don't think that the interactive printer could be combined with an Area component which updates itself in parallel?

2- I like the idea of disabling the user input, then maybe it could be tweaked with some special key, such as when pressing escape, or tab, it would stop the Area update and gives back control to the user. Not ideal, but ...

@MarvinJWendt
Copy link
Member

The user may enter commands, while still monitoring this table - in an ideal scenario

Ah, yeah I think we should be able to fix that. I'll take a look soon!

@MarvinJWendt MarvinJWendt added bug Something isn't working and removed proposal Proposal to add a new feature to pterm labels Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants