mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Fix the naming and specs for some utility functions
This commit is contained in:
parent
ecd95ba2d2
commit
3e2d6eb5fa
@ -64,7 +64,7 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
|
||||
revisions := []string{}
|
||||
|
||||
for _, deploy := range deploys {
|
||||
if (deploy.Revision != "") && utils.Exclude(revisions, deploy.Revision) {
|
||||
if (deploy.Revision != "") && utils.DoesNotInclude(revisions, deploy.Revision) {
|
||||
lineColor := "white"
|
||||
if wtf.IsToday(deploy.Timestamp) {
|
||||
lineColor = "lightblue"
|
||||
|
@ -30,15 +30,15 @@ func HighlightableHelper(view *tview.TextView, input string, idx, offset int) st
|
||||
fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
|
||||
_, _, w, _ := view.GetInnerRect()
|
||||
fmtStr += input
|
||||
fmtStr += PadRow(offset, w+1)
|
||||
fmtStr += RowPadding(offset, w+1)
|
||||
fmtStr += `[""]` + "\n"
|
||||
return fmtStr
|
||||
}
|
||||
|
||||
// PadRow returns a padding for a row to make it the full width of the containing widget.
|
||||
// RowPadding returns a padding for a row to make it the full width of the containing widget.
|
||||
// Useful for ensurig row highlighting spans the full width (I suspect tcell has a better
|
||||
// way to do this, but I haven't yet found it)
|
||||
func PadRow(offset int, max int) string {
|
||||
func RowPadding(offset int, max int) string {
|
||||
padSize := max - offset
|
||||
if padSize < 0 {
|
||||
padSize = 0
|
||||
|
@ -12,8 +12,13 @@ func Test_CenterText(t *testing.T) {
|
||||
Equal(t, " cat ", CenterText("cat", 9))
|
||||
}
|
||||
|
||||
func Test_PadRow(t *testing.T) {
|
||||
Equal(t, "", PadRow(0, 0))
|
||||
Equal(t, "", PadRow(5, 2))
|
||||
Equal(t, " ", PadRow(1, 2))
|
||||
func Test_HighlightableHelper(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func Test_RowPadding(t *testing.T) {
|
||||
Equal(t, "", RowPadding(0, 0))
|
||||
Equal(t, "", RowPadding(5, 2))
|
||||
Equal(t, " ", RowPadding(1, 2))
|
||||
Equal(t, " ", RowPadding(0, 5))
|
||||
}
|
||||
|
@ -21,6 +21,26 @@ const (
|
||||
TimestampFormat = "2006-01-02T15:04:05-0700"
|
||||
)
|
||||
|
||||
// DoesNotInclude takes a slice of strings and a target string and returns
|
||||
// TRUE if the slice does not include the target, FALSE if it does
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// x := DoesNotInclude([]string{"cat", "dog", "rat"}, "dog")
|
||||
// > false
|
||||
//
|
||||
// x := DoesNotInclude([]string{"cat", "dog", "rat"}, "pig")
|
||||
// > true
|
||||
//
|
||||
func DoesNotInclude(strs []string, val string) bool {
|
||||
for _, str := range strs {
|
||||
if val == str {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ExecuteCommand executes an external command on the local machine as the current user
|
||||
func ExecuteCommand(cmd *exec.Cmd) string {
|
||||
if cmd == nil {
|
||||
@ -49,23 +69,6 @@ func ExecuteCommand(cmd *exec.Cmd) string {
|
||||
return str
|
||||
}
|
||||
|
||||
// Exclude takes a slice of strings and a target string and returns the contents of the original
|
||||
// slice of strings without the target string in it
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// x := Exclude([]string{"cat", "dog", "rat"}, "dog")
|
||||
// > []string{"cat", "rat"}
|
||||
//
|
||||
func Exclude(strs []string, val string) bool {
|
||||
for _, str := range strs {
|
||||
if val == str {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// FindMatch takes a regex pattern and a string of data and returns back all the matches
|
||||
// in that string
|
||||
func FindMatch(pattern string, data string) [][]string {
|
||||
|
@ -2,11 +2,17 @@ package utils
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
. "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_DoesNotInclude(t *testing.T) {
|
||||
Equal(t, true, DoesNotInclude([]string{"cat", "dog", "rat"}, "bat"))
|
||||
Equal(t, false, DoesNotInclude([]string{"cat", "dog", "rat"}, "dog"))
|
||||
}
|
||||
|
||||
func Test_ExecuteCommand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@ -44,7 +50,26 @@ func Test_FindMatch(t *testing.T) {
|
||||
Equal(t, expected, result)
|
||||
}
|
||||
|
||||
func Test_ExcludeWhenTrue(t *testing.T) {
|
||||
Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat"))
|
||||
Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog"))
|
||||
func Test_ReadFileBytes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
file string
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "with non-existant file",
|
||||
file: "/tmp/junk-daa6bf613f4c.md",
|
||||
expected: []byte{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual, _ := ReadFileBytes(tt.file)
|
||||
|
||||
if reflect.DeepEqual(tt.expected, actual) == false {
|
||||
t.Errorf("\nexpected: %q\n got: %q", tt.expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user