1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/wtf/utils.go

186 lines
4.0 KiB
Go

package wtf
import (
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"strings"
"time"
"github.com/rivo/tview"
)
const SimpleDateFormat = "Jan 2"
const SimpleTimeFormat = "15:04 MST"
const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
const TimestampFormat = "2006-01-02T15:04:05-0700"
func CenterText(str string, width int) string {
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
}
func ExecuteCommand(cmd *exec.Cmd) string {
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
if err := cmd.Start(); err != nil {
return fmt.Sprintf("%v\n", err)
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
err = cmd.Wait()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
return str
}
func Exclude(strs []string, val string) bool {
for _, str := range strs {
if val == str {
return false
}
}
return true
}
func FindMatch(pattern string, data string) [][]string {
r := regexp.MustCompile(pattern)
return r.FindAllStringSubmatch(data, -1)
}
func NameFromEmail(email string) string {
parts := strings.Split(email, "@")
return strings.Title(strings.Replace(parts[0], ".", " ", -1))
}
func NamesFromEmails(emails []string) []string {
names := []string{}
for _, email := range emails {
names = append(names, NameFromEmail(email))
}
return names
}
// OpenFile opens the file defined in `path` via the operating system
func OpenFile(path string) {
filePath, _ := ExpandHomeDir(path)
cmd := exec.Command("open", filePath)
ExecuteCommand(cmd)
}
// PadRow 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, view *tview.TextView) string {
_, _, w, _ := view.GetInnerRect()
padSize := w - offset
if padSize < 0 {
padSize = 0
}
return strings.Repeat(" ", padSize)
}
func ReadFileBytes(filePath string) ([]byte, error) {
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
return []byte{}, err
}
return fileData, nil
}
func RightAlignFormat(view *tview.TextView) string {
_, _, w, _ := view.GetInnerRect()
return fmt.Sprintf("%%%ds", w-1)
}
func RowColor(module string, idx int) string {
evenKey := fmt.Sprintf("wtf.mods.%s.colors.row.even", module)
oddKey := fmt.Sprintf("wtf.mods.%s.colors.row.odd", module)
if idx%2 == 0 {
return Config.UString(evenKey, "white")
} else {
return Config.UString(oddKey, "lightblue")
}
}
func SygilStr(len, pos int, view *tview.TextView) string {
sygils := ""
if len > 0 {
sygils = strings.Repeat(Config.UString("wtf.paging.pageSygil", "*"), pos)
sygils = sygils + Config.UString("wtf.paging.selectedSygil", "_")
sygils = sygils + strings.Repeat(Config.UString("wtf.paging.pageSygil", "*"), len-1-pos)
sygils = "[lightblue]" + fmt.Sprintf(RightAlignFormat(view), sygils) + "[white]"
}
return sygils
}
/* -------------------- Slice Conversion -------------------- */
func ToInts(slice []interface{}) []int {
results := []int{}
for _, val := range slice {
results = append(results, val.(int))
}
return results
}
func ToStrs(slice []interface{}) []string {
results := []string{}
for _, val := range slice {
results = append(results, val.(string))
}
return results
}
/* -------------------- Date/Time -------------------- */
// DateFormat defines the format we expect to receive dates from BambooHR in
const DateFormat = "2006-01-02"
const TimeFormat = "15:04"
func IsToday(date time.Time) bool {
now := Now()
return (date.Year() == now.Year()) &&
(date.Month() == now.Month()) &&
(date.Day() == now.Day())
}
func Now() time.Time {
return time.Now().Local()
}
func PrettyDate(dateStr string) string {
newTime, _ := time.Parse(DateFormat, dateStr)
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
}
func Tomorrow() time.Time {
return Now().AddDate(0, 0, 1)
}
func UnixTime(unix int64) time.Time {
return time.Unix(unix, 0)
}