| @@ -27,8 +27,10 @@ func (c *Ctx) siteURL(parts ...string) string { |
| 27 | 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, |
| 31 | | // comments. Plain output is the same lines without colour or wrapping. |
| 30 | // view lays out a show piped: a title line, aligned fields, a body, |
| 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 | 34 | type view struct { |
| 33 | 35 | c *Ctx |
| 34 | 36 | w io.Writer |
| @@ -38,7 +40,7 @@ type view struct { |
| 38 | 40 | func (c *Ctx) view(w io.Writer) *view { return &view{c: c, w: w} } |
| 39 | 41 | |
| 40 | 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 | 46 | // sep writes a blank line before the next block, unless this view has |
| @@ -52,82 +54,32 @@ func (v *view) sep() { |
| 52 | 54 | v.wrote = true |
| 53 | 55 | } |
| 54 | 56 | |
| 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. |
| 57 | // section prints a sub-table's label: a blank line, then "label:". |
| 58 | // Callers skip the call entirely when the table it introduces has no |
| 59 | // rows. |
| 58 | 60 | func (v *view) section(label string) { |
| 59 | 61 | 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") |
| 62 | io.WriteString(v.w, label+":\n") |
| 65 | 63 | } |
| 66 | 64 | |
| 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. |
| 65 | // title prints "ref title state" on one line. title or state may be |
| 66 | // "": either is skipped rather than leaving a trailing blank field. |
| 72 | 67 | func (v *view) title(ref, title, state string) { |
| 73 | 68 | v.sep() |
| 74 | 69 | t := v.c.Term |
| 75 | 70 | 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 |
| 71 | line := ref |
| 72 | if title != "" { |
| 73 | line += " " + title |
| 88 | 74 | } |
| 89 | | prefix := ref + " " |
| 90 | | indent := strings.Repeat(" ", cells(prefix)) |
| 91 | | rest := title |
| 92 | 75 | if state != "" { |
| 93 | | if rest != "" { |
| 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") |
| 76 | line += " " + state |
| 125 | 77 | } |
| 78 | io.WriteString(v.w, line+"\n") |
| 126 | 79 | } |
| 127 | 80 | |
| 128 | 81 | // 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. |
| 82 | // empty values. |
| 131 | 83 | func (v *view) fields(kv ...string) { |
| 132 | 84 | wide := 0 |
| 133 | 85 | for i := 0; i+1 < len(kv); i += 2 { |
| @@ -141,19 +93,7 @@ func (v *view) fields(kv ...string) { |
| 141 | 93 | if val == "" { |
| 142 | 94 | continue |
| 143 | 95 | } |
| 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 | | } |
| 96 | io.WriteString(v.w, " "+pad(key, wide)+" "+val+"\n") |
| 157 | 97 | } |
| 158 | 98 | } |
| 159 | 99 | |
| @@ -190,32 +130,18 @@ func (v *view) text(src string) { |
| 190 | 130 | } |
| 191 | 131 | |
| 192 | 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 | 134 | func (v *view) event(text, format, ts string) { |
| 195 | 135 | 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") |
| 136 | io.WriteString(v.w, " "+line+" "+v.c.Term.safe(stamp(ts))+"\n") |
| 202 | 137 | v.wrote = true |
| 203 | 138 | } |
| 204 | 139 | |
| 205 | 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) |
| 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 | | } |
| 141 | when := v.c.Term.safe(stamp(ts)) + fmt.Sprintf(" (comment %d)", id) |
| 142 | head := "── " + v.c.Term.safe(author) + ", " + when + " " |
| 217 | 143 | v.sep() |
| 218 | | io.WriteString(v.w, v.c.Term.paint(sgrDim, head)+"\n") |
| 144 | io.WriteString(v.w, head+"\n") |
| 219 | 145 | v.body(body, format) |
| 220 | 146 | } |
| 221 | 147 | |