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

Empty lines in output? #209

Open
tomhoad opened this issue Oct 6, 2022 · 0 comments
Open

Empty lines in output? #209

tomhoad opened this issue Oct 6, 2022 · 0 comments

Comments

@tomhoad
Copy link

tomhoad commented Oct 6, 2022

I am fairly new to Golang, so I'm unsure if this is a problem with my code, Cobra, Promptui, or something else.

I have set up a minimal repro below, using jsonplaceholder's posts endpoint. Every time I run the following command, I seem to get a different output with random lines missing.

Windows 10, Powershell, Windows Terminal.

type Post struct {
	UserId int
	Id     int
	Title  string
	Body   string
}

var posts []Post
var postTitles []string

res, err := http.Get("https://jsonplaceholder.typicode.com/posts")

if err != nil {
	log.Fatalln(err)
}

defer res.Body.Close()

responseData, err := io.ReadAll(res.Body)

if err != nil {
	log.Fatalln(err)
}

err = json.Unmarshal(responseData, &posts)

if err != nil {
	fmt.Println("error:", err)
}

for _, post := range posts {
	postTitles = append(postTitles, post.Title)
}

prompt := promptui.Select{
	Label: "Select Division",
	Items: postTitles,
}

i, _, err := prompt.Run()

if err != nil {
	fmt.Printf("Prompt failed %v\n", err)
	return
}

fmt.Printf("Title: %q\n", posts[i].Title)
fmt.Printf("Body: %q\n", posts[i].Body)

Output on the command line:

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
Body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"

===

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit

Body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"

===

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
Body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"

===

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
Body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"

===

C:\Users\hoadt\Development\cobrademo> go run main.go divisions
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit


===

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
Body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"

===

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit



===

C:\Users\hoadt\Development\cobrademo> go run main.go testcommand
v sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
Body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
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

1 participant