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

Outputting to multiple lines #14

Closed
g3kk0 opened this issue Jun 2, 2017 · 3 comments
Closed

Outputting to multiple lines #14

g3kk0 opened this issue Jun 2, 2017 · 3 comments

Comments

@g3kk0
Copy link

g3kk0 commented Jun 2, 2017

This is more a question than an issue.

I'd like to output and update the status of a list of tasks:

e.g.

Task1... Done
Task2... Done
Task3... Running
Task4... 

I've attempted to do this using multiple Fprintf statements as follows:

w := uilive.New()
w.Start()

for i := 0; i <= 60; i++ {
  var t1, t2, t3, t4 string

  if i > 2 {
    t1 = "Done     "
    t2 = "Running"
  }
  if i > 5 {
    t1 = "Done     "
    t2 = "Done     "
    t3 = "Running"
  }
  if i > 7 {
    t1 = "Done     "
    t2 = "Done     "
    t3 = "Done     "
    t4 = "Running"
  }
  if i > 9 {
    t1 = "Done     "
    t2 = "Done     "
    t3 = "Done     "
    t4 = "Done     "
  }

  fmt.Fprintf(w, "Task1...  %s\n", t1)
  fmt.Fprintf(w, "Task2...  %s\n", t2)
  fmt.Fprintf(w, "Task3...  %s\n", t3)
  fmt.Fprintf(w, "Task4...  %s\n", t4)
  time.Sleep(time.Second)
}

w.Stop()

This initially appears to work fine however on some iterations various tasks disappear and then reappear.

e.g.

# I get this
Task3...  Done
Task4...  Done

# instead of
Task1...  Done
Task2...  Done
Task3...  Done
Task4...  Done

Am I using this incorrectly or could this possibly be a bug?

@g3kk0
Copy link
Author

g3kk0 commented Jun 2, 2017

For some reason adding this seems to help:

w.RefreshInterval = 1000

Could this be related to #9

@g3kk0
Copy link
Author

g3kk0 commented Jun 5, 2017

The solution to this was to only use a single call to the writer:

for i := 0; i <= 60; i++ {
  fmt.Fprintf(writer,
    "Task1...  %s\n"+
    "Task2...  %s\n"+
    "Task3...  %s\n"+
    "Task4...  %s\n",
    t1, t2, t3, t4, t5, t6)
  time.Sleep(time.Second)
}

@asahasrabuddhe
Copy link
Collaborator

asahasrabuddhe commented Apr 11, 2019

Hello @g3kk0 ,

I have managed to implement this feature back on my fork. I will be doing a PR soon for @gosuri to review and merge. Until that time, you can feel free to use my fork :)

Here's the code that I am using:

	//start listening for updates and render
	writer.Start()

	for i := 0; i <= 100; i++ {
		var t1, t2, t3, t4 string

		t1 = "Running"

		if i > 20 {
			t1 = "Done"
			t2 = "Running"
		}
		if i > 50 {
			t1 = "Done"
			t2 = "Done"
			t3 = "Running"
		}
		if i > 70 {
			t1 = "Done"
			t2 = "Done"
			t3 = "Done"
			t4 = "Running"
		}
		if i > 90 {
			t1 = "Done"
			t2 = "Done"
			t3 = "Done"
			t4 = "Done"
		}

		_, _ = fmt.Fprintf(writer, "Task1...  %s\n", t1)
		_, _ = fmt.Fprintf(writer.Newline(), "Task2...  %s\n", t2)
		_, _ = fmt.Fprintf(writer.Newline(), "Task3...  %s\n", t3)
		_, _ = fmt.Fprintf(writer.Newline(), "Task4...  %s\n", t4)
		time.Sleep(time.Millisecond * 100)
	}
	_, _ = fmt.Fprintln(writer, "Finished")
	writer.Stop() // flush and stop rendering

and this is the output for the same

render1554955255840-min

gosuri pushed a commit that referenced this issue Apr 11, 2019
* fix #3, #9 and #11 from upstream
* add support for outputting to newline #14
* update example to showcase newline feature
* update main.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants