vodo/helpers.go

147 lines
3.0 KiB
Go

package main
import (
"errors"
"math"
"strings"
"git.bullercodeworks.com/brian/gask"
)
// matchString gets the closest match of 'ndl' in 'hay'
func matchString(ndl string, hay []string) string {
var nextParms []string
for i := range ndl {
for _, p := range hay {
if p[i] == ndl[i] {
nextParms = append(nextParms, p)
}
}
// If we get here and there is only one string left, return it
hay = nextParms
if len(nextParms) == 1 {
break
}
// Otherwise, loop
nextParms = []string{}
}
if len(hay) == 0 {
return ""
}
return hay[0]
}
// matchStrings returns _all_ strings in hay that match ndl
func matchStrings(ndl string, hay []string) []string {
var nextParms []string
for i := range ndl {
for _, p := range hay {
if p[i] == ndl[i] {
nextParms = append(nextParms, p)
}
}
// If we get here and there is only one string left, return it
hay = nextParms
if len(nextParms) == 1 {
break
}
// Otherwise, loop
nextParms = []string{}
}
if len(hay) == 0 {
return []string{}
}
return hay
}
func indexToAlpha(id int) string {
// We want 0 to be 'a'
id = id + 1
var ret string
d, r := id/26, id%26
if r == 0 {
d, r = d-1, 26
}
if d > 26 {
return indexToAlpha(d) + string('a'+r-1)
}
if d > 0 {
ret = string('a' + d - 1)
}
return ret + string('a'+r-1)
}
func alphaToIndex(id string) int {
var j, ret int
for i := len(id) - 1; i >= 0; i-- {
ret += int(id[i]-'a'+1) * int(math.Pow(26, float64(j)))
j++
}
ret--
return ret
}
// taskToAppString takes a task ID and a the task and returns the string ready to be output
func taskToAppString(stId string, gt *gask.GaskTask) string {
var ret string
pct := gt.GetPctComplete()
str := strings.Repeat(" ", gt.Depth) // Task depth
str += stId // Task ID
if pct == 1 { // % Complete
str += "[X]"
} else if pct > 0.67 {
ret += "[O]"
} else if pct > 0.34 {
ret += "[o]"
} else if pct > 0 {
ret += "[.]"
} else {
ret += "[ ]"
}
ret += gt.Description // Description
//tskCnt++
//for _, st := range gt.Subtasks {
//}
return ret
}
// getListTaskId returns the task id for the given task in the given list
func getListTaskId(lst *gask.GaskList, tsk *gask.GaskTask) (int, error) {
var tskCnt int
for i := 0; i < lst.Tasks.Length(); i++ {
lt := lst.Tasks.Get(i)
if lt == tsk {
return tskCnt, nil
}
tskCnt++
if lt.GetSubtaskCount() > 0 {
if stCnt, err := getSubtaskId(lt, tsk); err != nil {
return (tskCnt + stCnt), nil
}
tskCnt += lt.GetSubtaskCount() - 1
}
}
return -1, errors.New("Couldn't find task")
}
func getSubtaskId(tsk *gask.GaskTask, sub *gask.GaskTask) (int, error) {
var tskCnt int
for _, st := range tsk.Subtasks {
if &st == sub {
return tskCnt, nil
}
tskCnt++
if st.GetSubtaskCount() > 0 {
if stCnt, err := getSubtaskId(&st, sub); err != nil {
return (tskCnt + stCnt), nil
}
tskCnt += st.GetSubtaskCount() - 1
}
}
return -1, errors.New("Couldn't find subtask")
}