github.com/wtfutil/wtf@v0.43.0/modules/git/display.go (about)

     1  package git
     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, " Git repo data is unavailable ", false
    17  	}
    18  
    19  	widgetTitle := ""
    20  	if widget.settings.lastFolderTitle {
    21  		pathParts := strings.Split(repoData.Repository, "/")
    22  		widgetTitle += pathParts[len(pathParts)-1]
    23  
    24  	} else {
    25  		widgetTitle = repoData.Repository
    26  	}
    27  	if widget.settings.branchInTitle {
    28  		widgetTitle += fmt.Sprintf(" <%s>", repoData.Branch)
    29  	}
    30  	title := ""
    31  	if widget.settings.showModuleName {
    32  		title = fmt.Sprintf(
    33  			"%s - %s[white]",
    34  			widget.CommonSettings().Title,
    35  			widgetTitle,
    36  		)
    37  	} else {
    38  		title = fmt.Sprintf(
    39  			"%s[white]",
    40  			widgetTitle,
    41  		)
    42  	}
    43  
    44  	_, _, width, _ := widget.View.GetRect()
    45  	str := widget.settings.PaginationMarker(len(widget.GitRepos), widget.Idx, width) + "\n"
    46  	for _, v := range widget.settings.sections {
    47  		if v == "branch" {
    48  			str += fmt.Sprintf(" [%s]Branch[white]\n", widget.settings.Colors.Subheading)
    49  			str += fmt.Sprintf(" %s", repoData.Branch)
    50  		} else if v == "files" && (widget.settings.showFilesIfEmpty || len(repoData.ChangedFiles) > 1) {
    51  			str += widget.formatChanges(repoData.ChangedFiles)
    52  		} else if v == "commits" {
    53  			str += widget.formatCommits(repoData.Commits)
    54  		}
    55  		str += "\n"
    56  	}
    57  
    58  	return title, str, false
    59  }
    60  
    61  func (widget *Widget) formatChanges(data []string) string {
    62  	str := fmt.Sprintf(" [%s]Changed Files[white]\n", widget.settings.Colors.Subheading)
    63  
    64  	if len(data) == 1 {
    65  		str += " [grey]none[white]\n"
    66  	} else {
    67  		for _, line := range data {
    68  			str += widget.formatChange(line)
    69  		}
    70  	}
    71  
    72  	return str
    73  }
    74  
    75  func (widget *Widget) formatChange(line string) string {
    76  	if line == "" {
    77  		return ""
    78  	}
    79  
    80  	line = strings.TrimSpace(line)
    81  	firstChar, _ := utf8.DecodeRuneInString(line)
    82  
    83  	// Revisit this and kill the ugly duplication
    84  	switch firstChar {
    85  	case 'A':
    86  		line = strings.Replace(line, "A", "[green]A[white]", 1)
    87  	case 'D':
    88  		line = strings.Replace(line, "D", "[red]D[white]", 1)
    89  	case 'M':
    90  		line = strings.Replace(line, "M", "[yellow]M[white]", 1)
    91  	case 'R':
    92  		line = strings.Replace(line, "R", "[purple]R[white]", 1)
    93  	}
    94  
    95  	return fmt.Sprintf(" %s\n", strings.ReplaceAll(line, "\"", ""))
    96  }
    97  
    98  func (widget *Widget) formatCommits(data []string) string {
    99  	str := fmt.Sprintf(" [%s]Recent Commits[white]\n", widget.settings.Colors.Subheading)
   100  
   101  	for _, line := range data {
   102  		str += widget.formatCommit(line)
   103  	}
   104  
   105  	return str
   106  }
   107  
   108  func (widget *Widget) formatCommit(line string) string {
   109  	return fmt.Sprintf(" %s\n", strings.ReplaceAll(line, "\"", ""))
   110  }