File: display.go

package info (click to toggle)
glab 1.53.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,936 kB
  • sloc: sh: 295; makefile: 153; perl: 99; ruby: 68; javascript: 67
file content (83 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package utils

import (
	"fmt"
	"regexp"
	"strings"
)

var lineRE = regexp.MustCompile(`(?m)^`)

func Indent(s, indent string) string {
	if strings.TrimSpace(s) == "" {
		return s
	}
	return lineRE.ReplaceAllLiteralString(s, indent)
}

type ListTitleOptions struct {
	// Name of the List to be used in constructing Description and EmptyMessage if not provided.
	Name string
	// Page represents the page number of the current page
	Page int
	// CurrentPageTotal is the total number of items in current page
	CurrentPageTotal int
	// Total number of records. Default is the total number of rows.
	// Can be set to be greater than the total number of rows especially, if the list is paginated
	Total int
	// RepoName represents the name of the project or repository
	RepoName string
	// ListActionType should be either "search" or "list". Default is list
	ListActionType string
	// Optional. EmptyMessage to display when List is empty. If not provided, default one constructed from list Name.
	EmptyMessage string
}

func NewListTitle(listName string) ListTitleOptions {
	return ListTitleOptions{
		Name:           strings.TrimSpace(listName),
		ListActionType: "list",
		Page:           1,
	}
}

func (opts *ListTitleOptions) Describe() string {
	var pageNumInfo string
	var pageInfo string

	if opts.Total != 0 {
		opts.Name = pluralizeName(opts.Total, opts.Name)
		pageNumInfo = fmt.Sprintf("%d of %d", opts.CurrentPageTotal, opts.Total)
	} else {
		opts.Name = pluralizeName(opts.CurrentPageTotal, opts.Name)
		pageNumInfo = fmt.Sprintf("%d", opts.CurrentPageTotal)
	}

	if opts.Page != 0 {
		pageInfo = fmt.Sprintf("(Page %d)", opts.Page)
	}

	if opts.ListActionType == "search" {
		if opts.CurrentPageTotal > 0 {
			return fmt.Sprintf("Showing %s %s in %s that match your search. %s\n", pageNumInfo, opts.Name,
				opts.RepoName, pageInfo)
		}

		return fmt.Sprintf("No %s match your search in %s.\n", opts.Name, opts.RepoName)
	}

	if opts.CurrentPageTotal > 0 {
		return fmt.Sprintf("Showing %s %s on %s. %s\n", pageNumInfo, opts.Name, opts.RepoName, pageInfo)
	}

	emptyMessage := opts.EmptyMessage
	if emptyMessage == "" {
		return fmt.Sprintf("No %s available on %s.", opts.Name, opts.RepoName)
	}

	return emptyMessage
}

func pluralizeName(num int, thing string) string {
	return strings.TrimPrefix(Pluralize(num, thing), fmt.Sprintf("%d ", num))
}