mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Cycle through selected issues with highlighting
This commit is contained in:
		
							parent
							
								
									c12bb5071d
								
							
						
					
					
						commit
						626cbe0883
					
				@ -11,6 +11,9 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type Widget struct {
 | 
					type Widget struct {
 | 
				
			||||||
	wtf.TextWidget
 | 
						wtf.TextWidget
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						result   *TicketArray
 | 
				
			||||||
 | 
						selected int
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewWidget() *Widget {
 | 
					func NewWidget() *Widget {
 | 
				
			||||||
@ -30,33 +33,42 @@ func (widget *Widget) Refresh() {
 | 
				
			|||||||
	ticketArray.Count = len(ticketArray.Tickets)
 | 
						ticketArray.Count = len(ticketArray.Tickets)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatal(err)
 | 
							log.Fatal(err)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							widget.result = ticketArray
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	widget.UpdateRefreshedAt()
 | 
						widget.UpdateRefreshedAt()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	widget.View.SetTitle(fmt.Sprintf("%s (%d)", widget.Name, ticketArray.Count))
 | 
						widget.display()
 | 
				
			||||||
	widget.View.SetText(widget.textContent(ticketArray.Tickets))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* -------------------- Unexported Functions -------------------- */
 | 
					/* -------------------- Unexported Functions -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) display() {
 | 
				
			||||||
 | 
						widget.View.SetTitle(fmt.Sprintf("%s (%d)", widget.Name, widget.result.Count))
 | 
				
			||||||
 | 
						widget.View.SetText(widget.textContent(widget.result.Tickets))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) textContent(items []Ticket) string {
 | 
					func (widget *Widget) textContent(items []Ticket) string {
 | 
				
			||||||
	if len(items) == 0 {
 | 
						if len(items) == 0 {
 | 
				
			||||||
		return fmt.Sprintf("No unassigned tickets in queue - woop!!")
 | 
							return fmt.Sprintf("No unassigned tickets in queue - woop!!")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	str := ""
 | 
						str := ""
 | 
				
			||||||
	for _, data := range items {
 | 
						for idx, data := range items {
 | 
				
			||||||
		str = str + widget.format(data)
 | 
							str = str + widget.format(data, idx)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return str
 | 
						return str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) format(ticket Ticket) string {
 | 
					func (widget *Widget) format(ticket Ticket, idx int) string {
 | 
				
			||||||
	var str string
 | 
						var str string
 | 
				
			||||||
	requesterName := widget.parseRequester(ticket)
 | 
						requesterName := widget.parseRequester(ticket)
 | 
				
			||||||
	str = fmt.Sprintf(" [green]%d - %s\n %s\n\n", ticket.Id, requesterName, ticket.Subject)
 | 
						textColor := wtf.Config.UString("wtf.colors.background", "green")
 | 
				
			||||||
 | 
						if idx == widget.selected {
 | 
				
			||||||
 | 
							textColor = wtf.Config.UString("wtf.colors.background", "orange")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						str = fmt.Sprintf(" [%s:]%d - %s\n %s\n\n", textColor, ticket.Id, requesterName, ticket.Subject)
 | 
				
			||||||
	return str
 | 
						return str
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -73,21 +85,80 @@ func (widget *Widget) parseRequester(ticket Ticket) interface{} {
 | 
				
			|||||||
	return fromName
 | 
						return fromName
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) next() {
 | 
				
			||||||
 | 
						widget.selected++
 | 
				
			||||||
 | 
						if widget.result != nil && widget.selected >= len(widget.result.Tickets) {
 | 
				
			||||||
 | 
							widget.selected = 0
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) prev() {
 | 
				
			||||||
 | 
						widget.selected--
 | 
				
			||||||
 | 
						if widget.selected < 0 && widget.result != nil {
 | 
				
			||||||
 | 
							widget.selected = len(widget.result.Tickets) - 1
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) openTicket() {
 | 
					func (widget *Widget) openTicket() {
 | 
				
			||||||
	wtf.OpenFile("https://" + subdomain + ".zendesk.com")
 | 
						sel := widget.selected
 | 
				
			||||||
 | 
						if sel >= 0 && widget.result != nil && sel < len(widget.result.Tickets) {
 | 
				
			||||||
 | 
							issue := &widget.result.Tickets[widget.selected]
 | 
				
			||||||
 | 
							wtf.OpenFile(issue.URL)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) unselect() {
 | 
				
			||||||
 | 
						widget.selected = -1
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) rowColor(idx int) string {
 | 
				
			||||||
 | 
						if widget.View.HasFocus() && (idx == widget.selected) {
 | 
				
			||||||
 | 
							foreColor := wtf.Config.UString("wtf.colors.highlight.fore", "black")
 | 
				
			||||||
 | 
							backColor := wtf.Config.UString("wtf.colors.highlight.back", "orange")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return fmt.Sprintf("%s:%s", foreColor, backColor)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return wtf.RowColor("zendesk", idx)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
 | 
					func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
 | 
				
			||||||
	switch string(event.Rune()) {
 | 
						switch string(event.Rune()) {
 | 
				
			||||||
 | 
						case "j":
 | 
				
			||||||
 | 
							// Select the next item down
 | 
				
			||||||
 | 
							widget.next()
 | 
				
			||||||
 | 
							widget.display()
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						case "k":
 | 
				
			||||||
 | 
							// Select the next item up
 | 
				
			||||||
 | 
							widget.prev()
 | 
				
			||||||
 | 
							widget.display()
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	case "f":
 | 
						case "f":
 | 
				
			||||||
		widget.openTicket()
 | 
							widget.openTicket()
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	switch event.Key() {
 | 
						switch event.Key() {
 | 
				
			||||||
 | 
						case tcell.KeyDown:
 | 
				
			||||||
 | 
							// Select the next item down
 | 
				
			||||||
 | 
							widget.next()
 | 
				
			||||||
 | 
							widget.display()
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
	case tcell.KeyEnter:
 | 
						case tcell.KeyEnter:
 | 
				
			||||||
		widget.openTicket()
 | 
							widget.openTicket()
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
 | 
						case tcell.KeyEsc:
 | 
				
			||||||
 | 
							// Unselect the current row
 | 
				
			||||||
 | 
							widget.unselect()
 | 
				
			||||||
 | 
							widget.display()
 | 
				
			||||||
 | 
							return event
 | 
				
			||||||
 | 
						case tcell.KeyUp:
 | 
				
			||||||
 | 
							// Select the next item up
 | 
				
			||||||
 | 
							widget.prev()
 | 
				
			||||||
 | 
							widget.display()
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
 | 
							// Pass it along
 | 
				
			||||||
		return event
 | 
							return event
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user