127 lines
2.6 KiB
Go
127 lines
2.6 KiB
Go
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
||
|
//
|
||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
|
|
||
|
package printer
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type theme struct {
|
||
|
reset string
|
||
|
boldblue string
|
||
|
boldmagenta string
|
||
|
green string
|
||
|
}
|
||
|
|
||
|
type Printer struct {
|
||
|
theme theme
|
||
|
noColor bool
|
||
|
maxTerminalWidth int
|
||
|
pager string
|
||
|
statusSeparator string
|
||
|
bullet string
|
||
|
pollMeterSymbol string
|
||
|
successSymbol string
|
||
|
failureSymbol string
|
||
|
dateFormat string
|
||
|
dateTimeFormat string
|
||
|
}
|
||
|
|
||
|
func NewPrinter(
|
||
|
noColor bool,
|
||
|
pager string,
|
||
|
maxTerminalWidth int,
|
||
|
) *Printer {
|
||
|
theme := theme{
|
||
|
reset: "\033[0m",
|
||
|
boldblue: "\033[34;1m",
|
||
|
boldmagenta: "\033[35;1m",
|
||
|
green: "\033[32m",
|
||
|
}
|
||
|
|
||
|
return &Printer{
|
||
|
noColor: noColor,
|
||
|
maxTerminalWidth: maxTerminalWidth,
|
||
|
pager: pager,
|
||
|
statusSeparator: strings.Repeat("─", maxTerminalWidth),
|
||
|
bullet: "\u2022",
|
||
|
pollMeterSymbol: "\u2501",
|
||
|
successSymbol: "\u2714",
|
||
|
failureSymbol: "\u2717",
|
||
|
dateFormat: "02 Jan 2006",
|
||
|
dateTimeFormat: "02 Jan 2006, 15:04 (MST)",
|
||
|
theme: theme,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (p Printer) PrintSuccess(text string) {
|
||
|
success := p.theme.green + p.successSymbol + p.theme.reset
|
||
|
if p.noColor {
|
||
|
success = p.successSymbol
|
||
|
}
|
||
|
|
||
|
printToStdout(success + " " + text + "\n")
|
||
|
}
|
||
|
|
||
|
func (p Printer) PrintFailure(text string) {
|
||
|
printToStderr(p.failureSymbol + " " + text)
|
||
|
}
|
||
|
|
||
|
func (p Printer) PrintInfo(text string) {
|
||
|
printToStdout(text)
|
||
|
}
|
||
|
|
||
|
func (p Printer) headerFormat(text string) string {
|
||
|
if p.noColor {
|
||
|
return text
|
||
|
}
|
||
|
|
||
|
return p.theme.boldblue + text + p.theme.reset
|
||
|
}
|
||
|
|
||
|
func (p Printer) fieldFormat(text string) string {
|
||
|
if p.noColor {
|
||
|
return text
|
||
|
}
|
||
|
|
||
|
return p.theme.green + text + p.theme.reset
|
||
|
}
|
||
|
|
||
|
func (p Printer) fullDisplayNameFormat(displayName, acct string) string {
|
||
|
// use this pattern to remove all emoji strings
|
||
|
pattern := regexp.MustCompile(`\s:[A-Za-z0-9_]*:`)
|
||
|
|
||
|
var builder strings.Builder
|
||
|
|
||
|
if p.noColor {
|
||
|
builder.WriteString(pattern.ReplaceAllString(displayName, ""))
|
||
|
} else {
|
||
|
builder.WriteString(p.theme.boldmagenta + pattern.ReplaceAllString(displayName, "") + p.theme.reset)
|
||
|
}
|
||
|
|
||
|
builder.WriteString(" (@" + acct + ")")
|
||
|
|
||
|
return builder.String()
|
||
|
}
|
||
|
|
||
|
func (p Printer) formatDate(date time.Time) string {
|
||
|
return date.Local().Format(p.dateFormat)
|
||
|
}
|
||
|
|
||
|
func (p Printer) formatDateTime(date time.Time) string {
|
||
|
return date.Local().Format(p.dateTimeFormat)
|
||
|
}
|
||
|
|
||
|
func printToStdout(text string) {
|
||
|
os.Stdout.WriteString(text)
|
||
|
}
|
||
|
|
||
|
func printToStderr(text string) {
|
||
|
os.Stderr.WriteString(text)
|
||
|
}
|