~schnouki/islas

697dedf5decc27c0310e4d2b499d2fe3f2148686 — Thomas Jost 6 years ago 5a642c5
Abbreviate line "numbers" in the "pretty" format

"Navette Brabois" --> "NB"
1 files changed, 17 insertions(+), 2 deletions(-)

M format/pretty.go
M format/pretty.go => format/pretty.go +17 -2
@@ 3,6 3,7 @@ package format
import (
	"fmt"
	"strings"
	"unicode"

	"github.com/fatih/color"
	"github.com/mattn/go-tty"


@@ 42,6 43,18 @@ func center(s string, l, w int) string {
	return strings.Repeat(" ", lp) + s + strings.Repeat(" ", ls)
}

func abbreviate(s string) string {
	prev := ' '
	return strings.Map(func(r rune) rune {
		res := rune(-1)
		if unicode.IsSpace(prev) {
			res = r
		}
		prev = r
		return res
	}, s)
}

// PrettyFormatter is a pretty time table formatter.
type PrettyFormatter struct {
	tables   []prettyTable


@@ 115,8 128,10 @@ func (t *prettyTable) format(width int) []string {
func (f *PrettyFormatter) Add(tt *fav.TimeTable) {
	t := prettyTable{}

	t.headerLength = len([]rune(fmt.Sprintf("[%s] %s → %s", tt.LineNumber, tt.StopName, tt.Direction)))
	t.header = prettyLineNumber.Sprintf("[%s]", tt.LineNumber)
	lineName := abbreviate(tt.LineNumber)

	t.headerLength = len([]rune(fmt.Sprintf("[%s] %s → %s", lineName, tt.StopName, tt.Direction)))
	t.header = prettyLineNumber.Sprintf("[%s]", lineName)
	t.header += " " + prettyStopName.Sprint(tt.StopName)
	t.header += " → " + prettyDirection.Sprint(tt.Direction)