Skip to content

Commit

Permalink
Add option for custom ellipsis
Browse files Browse the repository at this point in the history
Depending on the application, a custom ellipsis would be nice to adjust
for the respective style.

Add an optional argument in `FixedLength` that allows an override of the
ellipsis. If no override is specified, the default `[...]` is used.
  • Loading branch information
HeavyWombat committed Oct 1, 2019
1 parent 0654c2e commit 8b8c5ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
20 changes: 0 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gonvenience/bunt v1.0.0 h1:+SozbZgVunILmyX/EXbUNz/LTulUZTo8vXlvHiWZNFM=
github.com/gonvenience/bunt v1.0.0/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.0.1 h1:105azL3hzdCHh9WtrB1M+Jvs/r1fQAyHutA4vohJAQ8=
github.com/gonvenience/bunt v1.0.1/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.0.2 h1:eiWx8+bO2yIlZXCZ/zNITj+F+CnXrqgXO9SpRKhL+sQ=
github.com/gonvenience/bunt v1.0.2/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.0.5 h1:/iP7ROAPvBfpGab3eaFeyJPEOgdWJVpkkFNj8lfboZA=
github.com/gonvenience/bunt v1.0.5/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.0.6 h1:b3KEkaSlbXx3m4s2Gg5NfKkbo3YR3daCbqeecbDG9IE=
github.com/gonvenience/bunt v1.0.6/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.0.7 h1:g4xg5OlNNNGNnwRZ/F4r3inRgnbfrbjnw1lO4BODmb4=
github.com/gonvenience/bunt v1.0.7/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.1.0 h1:jKaW7wwtIS1pW29ykowAv1m5TdSo47uZgtZRGD4ss2c=
github.com/gonvenience/bunt v1.1.0/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/bunt v1.1.1 h1:isYxOpDqbRMOSRhZtoux1tYvhhQ/AIbVDFrs24l6t0M=
github.com/gonvenience/bunt v1.1.1/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/term v1.0.0 h1:joCB/j0Ngmdakd3muuLgAGPMf7DNKdoe708c1I6RiBs=
Expand All @@ -30,16 +16,10 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.9.0 h1:SZjF721BByVj8QH636/8S2DnX4n0Re3SteMmw3N+tzc=
github.com/onsi/ginkgo v1.9.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.6.0 h1:8XTW0fcJZEq9q+Upcyws4JSGua2MFysCL5xkaSgHc+M=
github.com/onsi/gomega v1.6.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
11 changes: 8 additions & 3 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ func RandomStringWithPrefix(prefix string, length int) string {
}

// FixedLength expands or trims the given text to the provided length
func FixedLength(text string, length int) string {
func FixedLength(text string, length int, ellipsisOverride ...string) string {
var ellipsis = " [...]"
if len(ellipsisOverride) > 0 {
ellipsis = ellipsisOverride[0]
}

textLength := bunt.PlainTextLength(text)
ellipsisLen := bunt.PlainTextLength(ellipsis)

switch {
case textLength < length: // padding required
return text + strings.Repeat(" ", length-textLength)

case textLength > length:
const ellipsis = " [...]"
return bunt.Substring(text, 0, length-len(ellipsis)) + ellipsis
return bunt.Substring(text, 0, length-ellipsisLen) + ellipsis

default:
return text
Expand Down
5 changes: 5 additions & 0 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ var _ = Describe("Generate random strings with fixed length", func() {

Expect(fmt.Sprintf("%#v", actual)).To(BeEquivalentTo(fmt.Sprintf("%#v", expected)))
})

It("should allow for a custom ellipsis", func() {
Expect(FixedLength("This text is too long", 8, Sprintf("DimGray{...}"))).
To(BeEquivalentTo(Sprintf("This DimGray{...}")))
})
})

Context("creating proper texts", func() {
Expand Down

0 comments on commit 8b8c5ed

Please sign in to comment.