103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"unicode"
|
||
|
|
||
|
"git.bullercodeworks.com/brian/gask"
|
||
|
)
|
||
|
|
||
|
type CliList struct {
|
||
|
Id string // The CLI alpha-id of this list
|
||
|
Tasks []CliTask // The lists tasks
|
||
|
|
||
|
dbList *gask.GaskList // The DB Backing for this list
|
||
|
}
|
||
|
|
||
|
func NewCliList(gl *gask.GaskList) *CliList {
|
||
|
ret := new(CliList)
|
||
|
for i := 0; i < gl.Tasks.Length(); i++ {
|
||
|
ret.Tasks = append(ret.Tasks, *NewCliTask(gl.Tasks.Get(i)))
|
||
|
}
|
||
|
ret.dbList = gl
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
func (cl *CliList) SetId(id string) {
|
||
|
cl.Id = id
|
||
|
}
|
||
|
|
||
|
// GetList takes the id argument and returns the list for the id
|
||
|
// e.g. 'b10' would return list 'b'
|
||
|
func (a *App) GetList(id string) (*gask.GaskList, error) {
|
||
|
var lId string
|
||
|
for _, r := range id {
|
||
|
if !unicode.IsLetter(r) {
|
||
|
break
|
||
|
}
|
||
|
lId = lId + string(r)
|
||
|
}
|
||
|
// lId should now be the alpha-id of the list
|
||
|
if len(lId) == 0 {
|
||
|
return nil, errors.New("Invalid list id given: " + id)
|
||
|
}
|
||
|
idx := alphaToIndex(id)
|
||
|
if a.db.Lists.Length() < idx {
|
||
|
return nil, errors.New("Invalid list id given: " + id)
|
||
|
}
|
||
|
return a.db.Lists.Get(idx), nil
|
||
|
}
|
||
|
|
||
|
// AddList adds a new list
|
||
|
func (a *App) AddList(args []string) error {
|
||
|
//a.db.AddList
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// PrintLists prints all list names with their alpha-ids
|
||
|
func (a *App) PrintLists(args []string) error {
|
||
|
if a.db.Lists.Length() == 0 {
|
||
|
return errors.New("No lists found")
|
||
|
}
|
||
|
matchedLists := a.db.Lists
|
||
|
if len(args) > 0 {
|
||
|
// Try to match the first argument to a list name
|
||
|
var allLists []string
|
||
|
for i := 0; i < a.db.Lists.Length(); i++ {
|
||
|
allLists = append(allLists, a.db.Lists.Get(i).Name)
|
||
|
}
|
||
|
mtch := matchStrings(args[0], allLists)
|
||
|
if len(mtch) > 0 {
|
||
|
// We matched a list, report the id and name
|
||
|
|
||
|
} else {
|
||
|
}
|
||
|
}
|
||
|
if matchedLists.Length() == 0 {
|
||
|
return errors.New("No lists matched the search criteria")
|
||
|
}
|
||
|
for i := 0; i < matchedLists.Length(); i++ {
|
||
|
fmt.Println("(" + indexToAlpha(i+1) + ") " + a.db.Lists.Get(i).Name)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// PrintList prints the lId-th list in the Todos file
|
||
|
func (a *App) PrintList(lId string) error {
|
||
|
listId := 0
|
||
|
for i := range lId {
|
||
|
listId += int(lId[i]) - int('a')
|
||
|
}
|
||
|
if listId < 0 || listId >= a.db.Lists.Length() {
|
||
|
return errors.New("Invalid list id")
|
||
|
}
|
||
|
|
||
|
lst := a.db.Lists.Get(listId)
|
||
|
fmt.Println(lId, lst.Name)
|
||
|
for i := 0; i < lst.Tasks.Length(); i++ {
|
||
|
a.PrintTask(lId, i)
|
||
|
}
|
||
|
return nil
|
||
|
}
|