-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to go-gh text package (cli#6236)
- Loading branch information
Showing
60 changed files
with
371 additions
and
822 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package text | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cli/go-gh/pkg/text" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
var whitespaceRE = regexp.MustCompile(`\s+`) | ||
|
||
func Indent(s, indent string) string { | ||
return text.Indent(s, indent) | ||
} | ||
|
||
// Title returns a copy of the string s with all Unicode letters that begin words mapped to their Unicode title case. | ||
func Title(s string) string { | ||
c := cases.Title(language.English) | ||
return c.String(s) | ||
} | ||
|
||
// RemoveExcessiveWhitespace returns a copy of the string s with excessive whitespace removed. | ||
func RemoveExcessiveWhitespace(s string) string { | ||
return whitespaceRE.ReplaceAllString(strings.TrimSpace(s), " ") | ||
} | ||
|
||
func DisplayWidth(s string) int { | ||
return text.DisplayWidth(s) | ||
} | ||
|
||
func Truncate(maxWidth int, s string) string { | ||
return text.Truncate(maxWidth, s) | ||
} | ||
|
||
func Pluralize(num int, thing string) string { | ||
return text.Pluralize(num, thing) | ||
} | ||
|
||
func FuzzyAgo(a, b time.Time) string { | ||
return text.RelativeTimeAgo(a, b) | ||
} | ||
|
||
// FuzzyAgoAbbr is an abbreviated version of FuzzyAgo. It returns a human readable string of the | ||
// time duration between a and b that is estimated to the nearest unit of time. | ||
func FuzzyAgoAbbr(a, b time.Time) string { | ||
ago := a.Sub(b) | ||
|
||
if ago < time.Hour { | ||
return fmt.Sprintf("%d%s", int(ago.Minutes()), "m") | ||
} | ||
if ago < 24*time.Hour { | ||
return fmt.Sprintf("%d%s", int(ago.Hours()), "h") | ||
} | ||
if ago < 30*24*time.Hour { | ||
return fmt.Sprintf("%d%s", int(ago.Hours())/24, "d") | ||
} | ||
|
||
return b.Format("Jan _2, 2006") | ||
} | ||
|
||
// DisplayURL returns a copy of the string urlStr removing everything except the hostname and path. | ||
// If there is an error parsing urlStr then urlStr is returned without modification. | ||
func DisplayURL(urlStr string) string { | ||
u, err := url.Parse(urlStr) | ||
if err != nil { | ||
return urlStr | ||
} | ||
return u.Hostname() + u.Path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package text | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRemoveExcessiveWhitespace(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want string | ||
}{ | ||
{ | ||
name: "nothing to remove", | ||
input: "one two three", | ||
want: "one two three", | ||
}, | ||
{ | ||
name: "whitespace b-gone", | ||
input: "\n one\n\t two three\r\n ", | ||
want: "one two three", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := RemoveExcessiveWhitespace(tt.input) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestFuzzyAgoAbbr(t *testing.T) { | ||
const form = "2006-Jan-02 15:04:05" | ||
now, _ := time.Parse(form, "2020-Nov-22 14:00:00") | ||
cases := map[string]string{ | ||
"2020-Nov-22 14:00:00": "0m", | ||
"2020-Nov-22 13:59:00": "1m", | ||
"2020-Nov-22 13:30:00": "30m", | ||
"2020-Nov-22 13:00:00": "1h", | ||
"2020-Nov-22 02:00:00": "12h", | ||
"2020-Nov-21 14:00:00": "1d", | ||
"2020-Nov-07 14:00:00": "15d", | ||
"2020-Oct-24 14:00:00": "29d", | ||
"2020-Oct-23 14:00:00": "Oct 23, 2020", | ||
"2019-Nov-22 14:00:00": "Nov 22, 2019", | ||
} | ||
for createdAt, expected := range cases { | ||
d, err := time.Parse(form, createdAt) | ||
assert.NoError(t, err) | ||
fuzzy := FuzzyAgoAbbr(now, d) | ||
assert.Equal(t, expected, fuzzy) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.