internal/control/view.go

237 lines · 6555 bytes

  1package control
  2
  3import (
  4	"fmt"
  5	"io"
  6	"strings"
  7
  8	"gitbay.org/gitbay/internal/store"
  9	"gitbay.org/gitbay/internal/termtext"
 10)
 11
 12// when is a stored timestamp in a view: the web's format at a
 13// terminal, RFC3339 to the second when plain.
 14func (c *Ctx) when(s string) string {
 15	if c.Term.Cols == 0 {
 16		return stamp(s)
 17	}
 18	t, ok := parseStamp(s)
 19	if !ok {
 20		return s
 21	}
 22	return t.Format("2006-01-02 15:04 UTC")
 23}
 24
 25// siteURL is the instance's address with path segments appended.
 26func (c *Ctx) siteURL(parts ...string) string {
 27	return strings.TrimRight(c.Cfg.Server.SiteURL, "/") + "/" + strings.Join(parts, "/")
 28}
 29
 30// view lays out a show: a title line, aligned fields, a body, events,
 31// comments. Plain output is the same lines without colour or wrapping.
 32type view struct {
 33	c     *Ctx
 34	w     io.Writer
 35	wrote bool // has this view written anything yet
 36}
 37
 38func (c *Ctx) view(w io.Writer) *view { return &view{c: c, w: w} }
 39
 40func (v *view) opts() termtext.Options {
 41	return termtext.Options{Width: max(0, v.c.Term.Cols-2), Color: v.c.Term.Color, Base: v.c.Cfg.Server.SiteURL}
 42}
 43
 44// sep writes a blank line before the next block, unless this view has
 45// written nothing yet: fields, a body or a section as the first thing a
 46// command prints (admin runners, admin stats, notifications settings
 47// show, ...) does not open with an empty line.
 48func (v *view) sep() {
 49	if v.wrote {
 50		io.WriteString(v.w, "\n")
 51	}
 52	v.wrote = true
 53}
 54
 55// section prints a sub-table's label: a blank line, then the label
 56// capitalised and bold at a terminal, or "label:" in plain. Callers skip the call entirely
 57// when the table it introduces has no rows.
 58func (v *view) section(label string) {
 59	v.sep()
 60	if v.c.Term.Cols == 0 {
 61		io.WriteString(v.w, label+":\n")
 62		return
 63	}
 64	io.WriteString(v.w, v.c.Term.heading(label)+"\n")
 65}
 66
 67// title prints "ref  title  state", wrapping title+state to the
 68// terminal width. Continuation lines indent under the title, and each
 69// line is painted after wrapping so no SGR sequence crosses a break.
 70// title or state may be "": either is skipped rather than leaving a
 71// trailing blank field.
 72func (v *view) title(ref, title, state string) {
 73	v.sep()
 74	t := v.c.Term
 75	ref, title, state = t.safe(ref), t.safe(title), t.safe(state)
 76	if t.Cols == 0 {
 77		switch {
 78		case title == "" && state == "":
 79			io.WriteString(v.w, ref+"\n")
 80		case state == "":
 81			io.WriteString(v.w, ref+"  "+t.paint(sgrBold, title)+"\n")
 82		case title == "":
 83			io.WriteString(v.w, ref+"  "+t.paint(stateColor(state), state)+"\n")
 84		default:
 85			io.WriteString(v.w, ref+"  "+t.paint(sgrBold, title)+"  "+t.paint(stateColor(state), state)+"\n")
 86		}
 87		return
 88	}
 89	prefix := ref + "  "
 90	indent := strings.Repeat(" ", cells(prefix))
 91	rest := title
 92	if state != "" {
 93		if rest != "" {
 94			rest += "  "
 95		}
 96		rest += state
 97	}
 98	if rest == "" {
 99		io.WriteString(v.w, t.paint(sgrCyan, ref)+"\n")
100		return
101	}
102	lines := termtext.Wrap(rest, t.Cols-cells(prefix))
103	for i, line := range lines {
104		p := indent
105		if i == 0 {
106			p = t.paint(sgrCyan, ref) + "  "
107		}
108		if i < len(lines)-1 || state == "" {
109			io.WriteString(v.w, p+t.paint(sgrBold, line)+"\n")
110			continue
111		}
112		// Last line carries the state word, the last word overall
113		// (states are single words): keep it coloured, not bold.
114		head, last := line, line
115		if idx := strings.LastIndex(line, " "); idx >= 0 {
116			head, last = line[:idx], line[idx+1:]
117		} else {
118			head = ""
119		}
120		io.WriteString(v.w, p)
121		if head != "" {
122			io.WriteString(v.w, t.paint(sgrBold, head)+" ")
123		}
124		io.WriteString(v.w, t.paint(stateColor(state), last)+"\n")
125	}
126}
127
128// fields prints key/value pairs aligned on the widest key, skipping
129// empty values. A value that does not fit wraps, its continuation
130// lines indented to the value column.
131func (v *view) fields(kv ...string) {
132	wide := 0
133	for i := 0; i+1 < len(kv); i += 2 {
134		if kv[i+1] != "" {
135			wide = max(wide, cells(kv[i]))
136		}
137	}
138	v.sep()
139	for i := 0; i+1 < len(kv); i += 2 {
140		key, val := v.c.Term.safe(kv[i]), v.c.Term.safe(kv[i+1])
141		if val == "" {
142			continue
143		}
144		prefix := "  " + v.c.Term.paint(sgrDim, pad(key, wide)) + "  "
145		if v.c.Term.Cols == 0 {
146			io.WriteString(v.w, prefix+val+"\n")
147			continue
148		}
149		indent := strings.Repeat(" ", 2+wide+2)
150		for j, line := range termtext.Wrap(val, v.c.Term.Cols-2-wide-2) {
151			p := indent
152			if j == 0 {
153				p = prefix
154			}
155			io.WriteString(v.w, p+line+"\n")
156		}
157	}
158}
159
160func (v *view) body(src, format string) {
161	if strings.TrimSpace(src) == "" {
162		return
163	}
164	v.sep()
165	src = v.c.Term.safe(src)
166	for _, line := range strings.Split(strings.TrimRight(termtext.Render(src, format, v.opts()), "\n"), "\n") {
167		if line == "" {
168			io.WriteString(v.w, "\n")
169			continue
170		}
171		io.WriteString(v.w, "  "+line+"\n")
172	}
173}
174
175// text writes src as it is, indented: a commit message, which has no
176// markup to render.
177func (v *view) text(src string) {
178	src = strings.TrimRight(v.c.Term.safe(src), "\n")
179	if strings.TrimSpace(src) == "" {
180		return
181	}
182	v.sep()
183	for _, line := range strings.Split(src, "\n") {
184		if line == "" {
185			io.WriteString(v.w, "\n")
186			continue
187		}
188		io.WriteString(v.w, "  "+line+"\n")
189	}
190}
191
192// event is one line for a system comment: its text without link
193// targets, the time at the right edge at a terminal.
194func (v *view) event(text, format, ts string) {
195	line := "· " + termtext.Inline(v.c.Term.safe(text), format)
196	when := v.c.Term.safe(v.c.when(ts))
197	if cols := v.c.Term.Cols; cols > 0 {
198		room := cols - 2 - 2 - cells(when)
199		line = pad(clip(line, room), room)
200	}
201	io.WriteString(v.w, "  "+v.c.Term.paint(sgrDim, line+"  "+when)+"\n")
202	v.wrote = true
203}
204
205func (v *view) comment(id int64, author, ts, body, format string) {
206	when := v.c.Term.safe(v.c.when(ts)) + fmt.Sprintf(" (comment %d)", id)
207	author = v.c.Term.safe(author)
208	cols := v.c.Term.Cols
209	if cols > 0 {
210		suffix := ", " + when + " "
211		author = clip(author, max(0, cols-cells("── "+suffix)-1))
212	}
213	head := "── " + author + ", " + when + " "
214	if cols > 0 {
215		head += strings.Repeat("─", max(1, cols-cells(head)))
216	}
217	v.sep()
218	io.WriteString(v.w, v.c.Term.paint(sgrDim, head)+"\n")
219	v.body(body, format)
220}
221
222// reactions prints one line of counts under an item, the caller's own
223// marked "(you)". Nothing when nobody has reacted.
224func (v *view) reactions(rs []ReactionOut) {
225	if len(rs) == 0 {
226		return
227	}
228	var parts []string
229	for _, r := range rs {
230		p := fmt.Sprintf("%s %d", store.ReactionEmoji(r.Reaction), r.Count)
231		if r.Me {
232			p += " (you)"
233		}
234		parts = append(parts, p)
235	}
236	io.WriteString(v.w, "reactions: "+strings.Join(parts, ", ")+"\n")
237}