forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrutil.go
107 lines (94 loc) · 2.73 KB
/
strutil.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package strutil
import (
"bytes"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)
func init() {
// golang does not init Seed() itself
rand.Seed(time.Now().UTC().UnixNano())
}
const letters = "BCDFGHJKLMNPQRSTVWXYbcdfghjklmnpqrstvwxy0123456789"
// MakeRandomString returns a random string of length length
//
// The vowels are omitted to avoid that words are created by pure
// chance. Numbers are included.
func MakeRandomString(length int) string {
out := ""
for i := 0; i < length; i++ {
out += string(letters[rand.Intn(len(letters))])
}
return out
}
// Convert the given size in btes to a readable string
func SizeToStr(size int64) string {
suffixes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
for _, suf := range suffixes {
if size < 1000 {
return fmt.Sprintf("%d%s", size, suf)
}
size /= 1000
}
panic("SizeToStr got a size bigger than math.MaxInt64")
}
// Quoted formats a slice of strings to a quoted list of
// comma-separated strings, e.g. `"snap1", "snap2"`
func Quoted(names []string) string {
quoted := make([]string, len(names))
for i, name := range names {
quoted[i] = strconv.Quote(name)
}
return strings.Join(quoted, ", ")
}
// WordWrap takes a input string and word wraps after `n` chars
// into a new slice.
//
// Caveats:
// - If a single word that is biger than max will not get wrapped
// - Extra whitespace will be removed
func WordWrap(s string, max int) []string {
n := 0
var out []string
line := bytes.NewBuffer(nil)
// FIXME: we want to be smarter here. to quote Gustavo: "The
// logic here is corrupting the spacing of the original line,
// which means indentation and tabling will be gone. A better
// approach would be finding the break point and then using
// the original content instead of rewriting it."
for _, word := range strings.Fields(s) {
if n+len(word) > max && n > 0 {
out = append(out, line.String())
line.Truncate(0)
n = 0
} else if n > 0 {
fmt.Fprintf(line, " ")
n += 1
}
fmt.Fprintf(line, word)
n += len(word)
}
if line.Len() > 0 {
out = append(out, line.String())
}
return out
}