| 
									
										
										
										
											2023-09-05 22:05:05 -05:00
										 |  |  | package list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import "github.com/rivo/tview" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Option interface { | 
					
						
							|  |  |  | 	Title() string | 
					
						
							|  |  |  | 	Text() string | 
					
						
							|  |  |  | 	Rune() rune | 
					
						
							|  |  |  | 	Selected() func() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type opt struct { | 
					
						
							|  |  |  | 	tit string | 
					
						
							|  |  |  | 	t   string | 
					
						
							|  |  |  | 	r   rune | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (o opt) Title() string { | 
					
						
							|  |  |  | 	return o.tit | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (o opt) Text() string { | 
					
						
							|  |  |  | 	return o.t | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (o opt) Rune() rune { | 
					
						
							|  |  |  | 	return o.r | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (o opt) Selected() func() { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func SimpleItem(title, text string, char rune) Option { | 
					
						
							|  |  |  | 	return opt{title, text, char} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 11:00:07 -05:00
										 |  |  | func List(title string, items []Option, cb func(*tview.List)) Option { | 
					
						
							| 
									
										
										
										
											2023-09-05 22:05:05 -05:00
										 |  |  | 	if len(items) == 0 { | 
					
						
							|  |  |  | 		return opt{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var index int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	app := tview.NewApplication() | 
					
						
							|  |  |  | 	list := tview.NewList() | 
					
						
							|  |  |  | 	for _, item := range items { | 
					
						
							|  |  |  | 		list.AddItem(item.Title(), item.Text(), item.Rune(), item.Selected()) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-09-08 11:00:07 -05:00
										 |  |  | 	list.Box.SetBorder(true).SetTitle(title) | 
					
						
							| 
									
										
										
										
											2023-09-05 22:05:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if cb != nil { | 
					
						
							|  |  |  | 		cb(list) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	list.SetSelectedFunc(func(idx int, _ string, _ string, _ rune) { | 
					
						
							|  |  |  | 		index = idx | 
					
						
							|  |  |  | 		app.Stop() | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 11:00:07 -05:00
										 |  |  | 	if err := app.SetRoot(list, true).EnableMouse(true).Run(); err != nil { | 
					
						
							| 
									
										
										
										
											2023-09-05 22:05:05 -05:00
										 |  |  | 		panic(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return items[index] | 
					
						
							|  |  |  | } |