2020-08-06 20:58:47 +03:00
|
|
|
// Package htmlw implements a RecordWriter for HTML.
|
|
|
|
package htmlw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-15 04:45:34 +03:00
|
|
|
"context"
|
2020-08-06 20:58:47 +03:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
2023-05-11 05:03:45 +03:00
|
|
|
"sync"
|
2020-08-06 20:58:47 +03:00
|
|
|
"time"
|
|
|
|
|
2023-11-22 00:49:52 +03:00
|
|
|
"github.com/shopspring/decimal"
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/kind"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/record"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// RecordWriter implements output.RecordWriter.
|
|
|
|
type recordWriter struct {
|
|
|
|
out io.Writer
|
2024-01-27 10:11:24 +03:00
|
|
|
pr *output.Printing
|
2020-08-06 20:58:47 +03:00
|
|
|
buf *bytes.Buffer
|
2024-01-27 10:11:24 +03:00
|
|
|
recMeta record.Meta
|
|
|
|
mu sync.Mutex
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
var _ output.NewRecordWriterFunc = NewRecordWriter
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// NewRecordWriter an output.RecordWriter for HTML.
|
2023-05-07 05:36:34 +03:00
|
|
|
func NewRecordWriter(out io.Writer, pr *output.Printing) output.RecordWriter {
|
|
|
|
return &recordWriter{out: out, pr: pr}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open implements output.RecordWriter.
|
2024-01-15 04:45:34 +03:00
|
|
|
func (w *recordWriter) Open(_ context.Context, recMeta record.Meta) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
w.recMeta = recMeta
|
|
|
|
w.buf = &bytes.Buffer{}
|
|
|
|
|
|
|
|
const header = `<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>sq output</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<table>
|
|
|
|
<colgroup>
|
|
|
|
`
|
|
|
|
|
|
|
|
w.buf.WriteString(header)
|
|
|
|
for _, field := range recMeta {
|
|
|
|
w.buf.WriteString(" <col class=\"kind-")
|
|
|
|
w.buf.WriteString(field.Kind().String())
|
|
|
|
w.buf.WriteString("\" />\n")
|
|
|
|
}
|
|
|
|
w.buf.WriteString(" </colgroup>\n <thead>\n <tr>\n")
|
|
|
|
for _, field := range recMeta {
|
|
|
|
w.buf.WriteString(" <th scope=\"col\">")
|
2023-07-09 04:34:53 +03:00
|
|
|
w.buf.WriteString(field.MungedName())
|
2020-08-06 20:58:47 +03:00
|
|
|
w.buf.WriteString("</th>\n")
|
|
|
|
}
|
|
|
|
w.buf.WriteString(" </tr>\n </thead>\n <tbody>\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements output.RecordWriter.
|
2024-01-15 04:45:34 +03:00
|
|
|
func (w *recordWriter) Flush(context.Context) error {
|
2023-05-11 05:03:45 +03:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2020-08-06 20:58:47 +03:00
|
|
|
_, err := w.buf.WriteTo(w.out) // resets buf
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements output.RecordWriter.
|
2024-01-15 04:45:34 +03:00
|
|
|
func (w *recordWriter) Close(ctx context.Context) error {
|
|
|
|
err := w.Flush(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const footer = ` </tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
|
|
|
_, err = w.out.Write([]byte(footer))
|
|
|
|
return errz.Err(err)
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:08:14 +03:00
|
|
|
func (w *recordWriter) writeRecord(rec record.Record) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
w.buf.WriteString(" <tr>\n")
|
|
|
|
|
|
|
|
var s string
|
|
|
|
for i, field := range rec {
|
|
|
|
w.buf.WriteString(" <td>")
|
|
|
|
|
|
|
|
switch val := field.(type) {
|
|
|
|
default:
|
|
|
|
s = html.EscapeString(fmt.Sprintf("%v", val))
|
|
|
|
// should never happen
|
|
|
|
case nil:
|
|
|
|
// nil is rendered as empty string, which this cell already is
|
2023-05-27 16:57:07 +03:00
|
|
|
case int64:
|
|
|
|
s = strconv.FormatInt(val, 10)
|
|
|
|
case string:
|
|
|
|
s = html.EscapeString(val)
|
|
|
|
case bool:
|
|
|
|
s = strconv.FormatBool(val)
|
|
|
|
case float64:
|
|
|
|
s = stringz.FormatFloat(val)
|
2023-11-22 00:49:52 +03:00
|
|
|
case decimal.Decimal:
|
|
|
|
s = stringz.FormatDecimal(val)
|
2023-05-27 16:57:07 +03:00
|
|
|
case []byte:
|
|
|
|
s = base64.StdEncoding.EncodeToString(val)
|
|
|
|
case time.Time:
|
2022-12-18 03:51:33 +03:00
|
|
|
switch w.recMeta[i].Kind() { //nolint:exhaustive
|
2020-08-06 20:58:47 +03:00
|
|
|
default:
|
2023-05-27 16:57:07 +03:00
|
|
|
s = w.pr.FormatDatetime(val)
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Time:
|
2023-05-27 16:57:07 +03:00
|
|
|
s = w.pr.FormatTime(val)
|
2020-08-23 13:42:15 +03:00
|
|
|
case kind.Date:
|
2023-05-27 16:57:07 +03:00
|
|
|
s = w.pr.FormatDate(val)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.buf.WriteString(s)
|
|
|
|
w.buf.WriteString("</td>\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.buf.WriteString(" </tr>\n")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteRecords implements output.RecordWriter.
|
2024-01-15 04:45:34 +03:00
|
|
|
func (w *recordWriter) WriteRecords(ctx context.Context, recs []record.Record) error {
|
2023-05-11 05:03:45 +03:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2020-08-06 20:58:47 +03:00
|
|
|
var err error
|
|
|
|
for _, rec := range recs {
|
2024-01-15 04:45:34 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
err = w.writeRecord(rec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|