Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,54 @@ import (
"fmt"
"os"
"runtime"
"strings"

"github.com/nilock/tuido/tui"
"github.com/nilock/tuido/tuido"
"github.com/nilock/tuido/utils"
)

func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "list":
listCmd := flag.NewFlagSet("list", flag.ExitOnError)
maxItems := listCmd.Int("max", 0, "maximum number of items to display")
inclSnoozed := listCmd.Bool("z", false, "include snoozed items")
listCmd.BoolVar(inclSnoozed, "zzz", false, "include snoozed items")
inclAll := listCmd.Bool("a", false, "include snoozed and completed/cancelled items")
listCmd.BoolVar(inclAll, "all", false, "include snoozed and completed/cancelled items")
listCmd.Parse(os.Args[2:])

path := "."
if listCmd.NArg() > 0 {
path = listCmd.Arg(0)
}

runList(path, *maxItems, *inclSnoozed || *inclAll, *inclAll)
return

case "create", "add":
text := strings.Join(os.Args[2:], " ")
if text == "" {
fmt.Fprintln(os.Stderr, "Usage: tuido create <text>")
os.Exit(1)
}
runCreate(text)
return

case "version", "-version", "--version":
showVersionInfo()
return

case "help", "-help", "--help", "-h":
printHelp()
return
}
}

var showVersion = flag.Bool("version", false, "show version and platform information")
flag.Usage = printHelp
flag.Parse()

if *showVersion {
Expand All @@ -22,6 +63,68 @@ func main() {
tui.Run()
}

func runList(path string, max int, inclSnoozed bool, inclDone bool) {
files := tui.GetFiles(path, tui.GetConfigExtensions())
all := []*tuido.Item{}
for _, f := range files {
all = append(all, tui.GetItems(f)...)
}

tui.SortItems(all)

filtered := all[:0]
for _, item := range all {
s := item.Satus()
if !inclDone && (s == tuido.Checked || s == tuido.Obsolete) {
continue
}
if !inclSnoozed && !item.Active() {
continue
}
filtered = append(filtered, item)
}

count := len(filtered)
if max > 0 && max < count {
count = max
}

for i := 0; i < count; i++ {
fmt.Printf("%s\n", filtered[i].String())
}
}

func runCreate(text string) {
writeTo := tui.GetConfigWriteTo()
item := tuido.New(writeTo, -1, "")
err := item.SetText(text)
if err != nil {
fmt.Printf("Error creating item: %v\n", err)
os.Exit(1)
}
fmt.Printf("Created: %s\n", item.String())
}

func printHelp() {
fmt.Print(`Usage: tuido [command] [options]

Without a command, opens the interactive TUI.

Commands:
list [options] [path] List open/in-progress items (default path: .)
-z, --zzz Include snoozed items
-a, --all Include snoozed and completed/cancelled items
--max N Limit output to N items
create <text> Create a new todo item
add <text> Alias for create
version Show version and platform information
help Show this help

Flags:
-version Show version and platform information
`)
}

func showVersionInfo() {
version := utils.Version()
goos := runtime.GOOS
Expand Down
8 changes: 8 additions & 0 deletions tui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ func parseConfig(file *os.File) config {

return cfg
}

func GetConfigExtensions() []string {
return runConfig.extensions
}

func GetConfigWriteTo() string {
return runConfig.writeto
}
16 changes: 8 additions & 8 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ func Run() {
os.Exit(1)
}
if wtStat.IsDir() {
writeDirFiles := getFiles(runConfig.writeto, runConfig.extensions)
writeDirFiles := GetFiles(runConfig.writeto, runConfig.extensions)
for _, f := range writeDirFiles {
files[f] = struct{}{}
}
}

// [ ] replace with subdir check #active=2022-05-26 #zzz=2
if wrkdirStr != runConfig.writeto {
wdFiles := getFiles(wrkdirStr, runConfig.extensions)
wdFiles := GetFiles(wrkdirStr, runConfig.extensions)
for _, f := range wdFiles {
files[f] = struct{}{}
}
}

items := []*tuido.Item{}
for f := range files {
items = append(items, getItems(f)...)
items = append(items, GetItems(f)...)
}

sortItems(items)
SortItems(items)

tui := newTUI(items, runConfig)
tui.houseKeeping()
Expand Down Expand Up @@ -313,7 +313,7 @@ func (t *tui) populateRenderSelection() {

// Only sort if no filter is active - preserve fuzzy search ranking
if len(t.filter.Value()) == 0 {
sortItems(t.renderSelection)
SortItems(t.renderSelection)
}

// ensure the previous selection value is still in range
Expand Down Expand Up @@ -448,7 +448,7 @@ func itemHasTag(itemTags []tuido.Tag, filter tuido.Tag) bool {

func (t tui) Init() tea.Cmd { return tick() }

func getItems(file string) []*tuido.Item {
func GetItems(file string) []*tuido.Item {
items := []*tuido.Item{}

if f, err := os.Open(file); err != nil {
Expand All @@ -471,7 +471,7 @@ func getItems(file string) []*tuido.Item {
}
}

func getFiles(wd string, extensions []string) []string {
func GetFiles(wd string, extensions []string) []string {

files := []string{}

Expand Down Expand Up @@ -501,7 +501,7 @@ func getFiles(wd string, extensions []string) []string {
return files
}

func sortItems(items []*tuido.Item) {
func SortItems(items []*tuido.Item) {
sort.SliceStable(items, func(i, j int) bool {
if items[i].Importance() > items[j].Importance() {
return true
Expand Down
Loading