github.com/wtfutil/wtf@v0.43.0/modules/mercurial/display.go (about) 1 package mercurial 2 3 import ( 4 "fmt" 5 "strings" 6 "unicode/utf8" 7 ) 8 9 func (widget *Widget) display() { 10 widget.Redraw(widget.content) 11 } 12 13 func (widget *Widget) content() (string, string, bool) { 14 repoData := widget.currentData() 15 if repoData == nil { 16 return widget.CommonSettings().Title, " Mercurial repo data is unavailable ", false 17 } 18 19 title := fmt.Sprintf( 20 "%s - %s[white]", 21 widget.settings.Colors.TextTheme.Title, 22 repoData.Repository, 23 ) 24 25 _, _, width, _ := widget.View.GetRect() 26 str := widget.settings.PaginationMarker(len(widget.Data), widget.Idx, width) + "\n" 27 str += fmt.Sprintf(" [%s]Branch:Bookmark[white]\n", widget.settings.Colors.Subheading) 28 str += fmt.Sprintf(" %s:%s\n", repoData.Branch, repoData.Bookmark) 29 str += "\n" 30 str += widget.formatChanges(repoData.ChangedFiles) 31 str += "\n" 32 str += widget.formatCommits(repoData.Commits) 33 34 return title, str, false 35 } 36 37 func (widget *Widget) formatChanges(data []string) string { 38 str := fmt.Sprintf(" [%s]Changed Files[white]\n", widget.settings.Colors.Subheading) 39 40 if len(data) == 1 { 41 str += " [grey]none[white]\n" 42 } else { 43 for _, line := range data { 44 str += widget.formatChange(line) 45 } 46 } 47 48 return str 49 } 50 51 func (widget *Widget) formatChange(line string) string { 52 if line == "" { 53 return "" 54 } 55 56 line = strings.TrimSpace(line) 57 firstChar, _ := utf8.DecodeRuneInString(line) 58 59 // Revisit this and kill the ugly duplication 60 switch firstChar { 61 case 'A': 62 line = strings.Replace(line, "A", "[green]A[white]", 1) 63 case 'D': 64 line = strings.Replace(line, "D", "[red]D[white]", 1) 65 case 'M': 66 line = strings.Replace(line, "M", "[yellow]M[white]", 1) 67 case 'R': 68 line = strings.Replace(line, "R", "[purple]R[white]", 1) 69 } 70 71 return fmt.Sprintf(" %s\n", strings.ReplaceAll(line, "\"", "")) 72 } 73 74 func (widget *Widget) formatCommits(data []string) string { 75 str := fmt.Sprintf(" [%s]Recent Commits[white]\n", widget.settings.Colors.Subheading) 76 77 for _, line := range data { 78 str += widget.formatCommit(line) 79 } 80 81 return str 82 } 83 84 func (widget *Widget) formatCommit(line string) string { 85 return fmt.Sprintf(" %s\n", strings.ReplaceAll(line, "\"", "")) 86 }