Debian Codegraph @

transform.go

raw symbols SCIP
1 package jl
2
3 import (
4 "bytes"
5 "fmt"
6 "strings"
7 "unicode/utf8"
8 )
9
10 // Context provides the current transformation context, to be used by Transformers and Stringers.
11 type Context struct {
12 // The original string before any transformations were applied.
13 Original string
14 // Indicates that terminal color escape sequences should be disabled.
15 DisableColor bool
16 // Indicates that fields should not be truncated.
17 DisableTruncate bool
18 }
19
20 // Transformer transforms a string and returns the result.
21 type Transformer interface {
22 Transform(ctx *Context, input string) string
23 }
24
25 // TransformFunc is an adapter to allow the use of ordinary functions as Transformers.
26 type TransformFunc func(string) string
27
28 func (f TransformFunc) Transform(ctx *Context, input string) string {
29 return f(input)
30 }
31
32 var (
33 // UpperCase transforms the input string to upper case.
34 UpperCase = TransformFunc(strings.ToUpper)
35 // LowerCase transforms the input string to lower case.
36 LowerCase = TransformFunc(strings.ToLower)
37 )
38
39 // Truncate truncates the string to the a requested number of digits.
40 type Truncate int
41
42 func (t Truncate) Transform(ctx *Context, input string) string {
43 if ctx.DisableTruncate {
44 return input
45 }
46 if utf8.RuneCountInString(input) <= int(t) {
47 return input
48 }
49 return input[:t]
50 }
51
52 // Ellipsize replaces characters in the middle of the string with a single "…" character so that it fits within the
53 // requested length.
54 type Ellipsize int
55
56 func (remain Ellipsize) Transform(ctx *Context, input string) string {
57 if ctx.DisableTruncate {
58 return input
59 }
60 length := utf8.RuneCountInString(input)
61 if length <= int(remain) {
62 return input
63 }
64 remain -= 1 // account for the ellipsis
65 chomped := length - int(remain)
66 start := int(remain)/2
67 end := start + chomped
68 return input[:start] + "…" + input[end:]
69 }
70
71 // LeftPad pads the left side of the string with spaces so that the string becomes the requested length.
72 type LeftPad int
73
74 func (t LeftPad) Transform(ctx *Context, input string) string {
75 spaces := int(t) - utf8.RuneCountInString(input)
76 if spaces <= 0 {
77 return input
78 }
79 buf := bytes.NewBuffer(make([]byte, 0, spaces+len(input)))
80 for i := 0; i < spaces; i++ {
81 buf.WriteRune(' ')
82 }
83 buf.WriteString(input)
84 return buf.String()
85 }
86
87 // LeftPad pads the right side of the string with spaces so that the string becomes the requested length.
88 type RightPad int
89
90 func (t RightPad) Transform(ctx *Context, input string) string {
91 pad := int(t) - utf8.RuneCountInString(input)
92 if pad <= 0 {
93 return input
94 }
95 buf := bytes.NewBuffer(make([]byte, 0, pad+len(input)))
96 buf.WriteString(input)
97 for i := 0; i < pad; i++ {
98 buf.WriteRune(' ')
99 }
100 return buf.String()
101 }
102
103 // Format calls fmt.Sprintf() with the requested format string.
104 type Format string
105
106 func (t Format) Transform(ctx *Context, input string) string {
107 return fmt.Sprintf(string(t), input)
108 }