File: printer.go

package info (click to toggle)
golang-github-jamesclonk-vultr 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 560 kB
  • sloc: makefile: 35
file content (50 lines) | stat: -rw-r--r-- 959 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cmd

import (
	"fmt"
	"log"
	"os"
	"strings"
	"text/tabwriter"
)

var tw = new(tabwriter.Writer)

func init() {
	tw.Init(os.Stdout, 0, 8, 2, '\t', 0)
}

type columns []interface{}

func tabsPrint(values columns, lengths []int) {
	if len(values) != len(lengths) {
		log.Fatalf("Internal error! Mismatch during tabbed line print. Values: %d, Lengths: %d\n", len(values), len(lengths))
	}

	for i, value := range values {
		format := "\t%s"
		if i == 0 {
			format = "%s"
		}
		fmt.Fprintf(tw, format, replaceCharacters(maxCharacters(fmt.Sprintf("%v", value), lengths[i])))
	}
	fmt.Fprintf(tw, "\n")
}

func tabsFlush() {
	tw.Flush()
}

func replaceCharacters(s string) string {
	s = strings.Replace(s, "\n", `\n`, -1)
	s = strings.Replace(s, "\r", `\r`, -1)
	s = strings.Replace(s, "\t", `\t`, -1)
	return s
}

func maxCharacters(input string, maxLength int) string {
	if len(input) > maxLength {
		input = input[:maxLength-2] + ".."
	}
	return input
}