| @@ -27,8 +27,10 @@ func (c *Ctx) siteURL(parts ...string) string { |
| 27 | return strings.TrimRight(c.Cfg.Server.SiteURL, "/") + "/" + strings.Join(parts, "/") |
27 | return strings.TrimRight(c.Cfg.Server.SiteURL, "/") + "/" + strings.Join(parts, "/") |
| 28 | } |
28 | } |
| 29 | |
29 | |
| 30 | // view lays out a show: a title line, aligned fields, a body, events, |
30 | // view lays out a show piped: a title line, aligned fields, a body, |
| 31 | // comments. Plain output is the same lines without colour or wrapping. |
31 | // events, comments. A terminal draws a screen instead (screen.go); a |
| |
32 | // caller that still reaches view at a terminal gets the same plain |
| |
33 | // layout, with user text made safe. |
| 32 | type view struct { |
34 | type view struct { |
| 33 | c *Ctx |
35 | c *Ctx |
| 34 | w io.Writer |
36 | w io.Writer |
| @@ -38,7 +40,7 @@ type view struct { |
| 38 | func (c *Ctx) view(w io.Writer) *view { return &view{c: c, w: w} } |
40 | func (c *Ctx) view(w io.Writer) *view { return &view{c: c, w: w} } |
| 39 | |
41 | |
| 40 | func (v *view) opts() termtext.Options { |
42 | func (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} |
43 | return termtext.Options{Base: v.c.Cfg.Server.SiteURL} |
| 42 | } |
44 | } |
| 43 | |
45 | |
| 44 | // sep writes a blank line before the next block, unless this view has |
46 | // sep writes a blank line before the next block, unless this view has |
| @@ -52,82 +54,32 @@ func (v *view) sep() { |
| 52 | v.wrote = true |
54 | v.wrote = true |
| 53 | } |
55 | } |
| 54 | |
56 | |
| 55 | // section prints a sub-table's label: a blank line, then the label |
57 | // section prints a sub-table's label: a blank line, then "label:". |
| 56 | // capitalised and bold at a terminal, or "label:" in plain. Callers skip the call entirely |
58 | // Callers skip the call entirely when the table it introduces has no |
| 57 | // when the table it introduces has no rows. |
59 | // rows. |
| 58 | func (v *view) section(label string) { |
60 | func (v *view) section(label string) { |
| 59 | v.sep() |
61 | v.sep() |
| 60 | if v.c.Term.Cols == 0 { |
62 | io.WriteString(v.w, label+":\n") |
| 61 | io.WriteString(v.w, label+":\n") |
| |
| 62 | return |
| |
| 63 | } |
| |
| 64 | io.WriteString(v.w, v.c.Term.heading(label)+"\n") |
| |
| 65 | } |
63 | } |
| 66 | |
64 | |
| 67 | // title prints "ref title state", wrapping title+state to the |
65 | // title prints "ref title state" on one line. title or state may be |
| 68 | // terminal width. Continuation lines indent under the title, and each |
66 | // "": either is skipped rather than leaving a trailing blank field. |
| 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. |
| |
| 72 | func (v *view) title(ref, title, state string) { |
67 | func (v *view) title(ref, title, state string) { |
| 73 | v.sep() |
68 | v.sep() |
| 74 | t := v.c.Term |
69 | t := v.c.Term |
| 75 | ref, title, state = t.safe(ref), t.safe(title), t.safe(state) |
70 | ref, title, state = t.safe(ref), t.safe(title), t.safe(state) |
| 76 | if t.Cols == 0 { |
71 | line := ref |
| 77 | switch { |
72 | if title != "" { |
| 78 | case title == "" && state == "": |
73 | line += " " + title |
| 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 | } |
74 | } |
| 89 | prefix := ref + " " |
| |
| 90 | indent := strings.Repeat(" ", cells(prefix)) |
| |
| 91 | rest := title |
| |
| 92 | if state != "" { |
75 | if state != "" { |
| 93 | if rest != "" { |
76 | line += " " + state |
| 94 | rest += " " |
| |
| 95 | } |
| |
| 96 | rest += state |
| |
| 97 | } |
| |
| 98 | if rest == "" { |
| |
| 99 | io.WriteString(v.w, t.paint(sgrDim, 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(sgrDim, 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 | } |
77 | } |
| |
78 | io.WriteString(v.w, line+"\n") |
| 126 | } |
79 | } |
| 127 | |
80 | |
| 128 | // fields prints key/value pairs aligned on the widest key, skipping |
81 | // fields prints key/value pairs aligned on the widest key, skipping |
| 129 | // empty values. A value that does not fit wraps, its continuation |
82 | // empty values. |
| 130 | // lines indented to the value column. |
| |
| 131 | func (v *view) fields(kv ...string) { |
83 | func (v *view) fields(kv ...string) { |
| 132 | wide := 0 |
84 | wide := 0 |
| 133 | for i := 0; i+1 < len(kv); i += 2 { |
85 | for i := 0; i+1 < len(kv); i += 2 { |
| @@ -141,19 +93,7 @@ func (v *view) fields(kv ...string) { |
| 141 | if val == "" { |
93 | if val == "" { |
| 142 | continue |
94 | continue |
| 143 | } |
95 | } |
| 144 | prefix := " " + v.c.Term.paint(sgrDim, pad(key, wide)) + " " |
96 | io.WriteString(v.w, " "+pad(key, wide)+" "+val+"\n") |
| 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 | } |
97 | } |
| 158 | } |
98 | } |
| 159 | |
99 | |
| @@ -190,32 +130,18 @@ func (v *view) text(src string) { |
| 190 | } |
130 | } |
| 191 | |
131 | |
| 192 | // event is one line for a system comment: its text without link |
132 | // event is one line for a system comment: its text without link |
| 193 | // targets, the time at the right edge at a terminal. |
133 | // targets, then the time. |
| 194 | func (v *view) event(text, format, ts string) { |
134 | func (v *view) event(text, format, ts string) { |
| 195 | line := "· " + termtext.Inline(v.c.Term.safe(text), format) |
135 | line := "· " + termtext.Inline(v.c.Term.safe(text), format) |
| 196 | when := v.c.Term.safe(v.c.when(ts)) |
136 | io.WriteString(v.w, " "+line+" "+v.c.Term.safe(stamp(ts))+"\n") |
| 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 |
137 | v.wrote = true |
| 203 | } |
138 | } |
| 204 | |
139 | |
| 205 | func (v *view) comment(id int64, author, ts, body, format string) { |
140 | func (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) |
141 | when := v.c.Term.safe(stamp(ts)) + fmt.Sprintf(" (comment %d)", id) |
| 207 | author = v.c.Term.safe(author) |
142 | head := "── " + v.c.Term.safe(author) + ", " + when + " " |
| 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() |
143 | v.sep() |
| 218 | io.WriteString(v.w, v.c.Term.paint(sgrDim, head)+"\n") |
144 | io.WriteString(v.w, head+"\n") |
| 219 | v.body(body, format) |
145 | v.body(body, format) |
| 220 | } |
146 | } |
| 221 | |
147 | |