-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add core CLI logic and basic commands
- Loading branch information
1 parent
f809ed2
commit 6d95896
Showing
51 changed files
with
2,425 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,6 @@ build/ | |
*/Makefile.* | ||
core* | ||
!core*.sh | ||
!core*.go | ||
*~ | ||
.build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package batch | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/chzyer/readline" | ||
) | ||
|
||
func init() { | ||
completions = append( | ||
completions, | ||
readline.PcItem( | ||
"abuts", | ||
readline.PcItemDynamic( | ||
completionStatesList, | ||
readline.PcItemDynamic(completionStatesList), | ||
), | ||
), | ||
) | ||
|
||
commands["abuts"] = func(args []string) error { | ||
if len(args) != 2 { | ||
return errors.New("usage: abuts <source> <compare>") | ||
} | ||
|
||
source, compare := args[0], args[1] | ||
|
||
if stamp1, ok := state[source]; ok { | ||
if stamp2, ok := state[compare]; ok { | ||
printf("%t\n", stamp1.Abuts(stamp2)) | ||
} else { | ||
return errors.New("timestamp '" + compare + "' not set") | ||
} | ||
} else { | ||
return errors.New("timestamp '" + source + "' not set") | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package batch | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/chzyer/readline" | ||
) | ||
|
||
func init() { | ||
completions = append( | ||
completions, | ||
readline.PcItem( | ||
"add-from-end", | ||
readline.PcItemDynamic(completionCalendarList), | ||
), | ||
) | ||
|
||
commands["add-from-end"] = func(args []string) error { | ||
var err error | ||
|
||
if len(args) != 4 { | ||
return errors.New("usage: add-from-end <calendar> <offset> <source> <target>") | ||
} | ||
|
||
calendar, offset, source, target := args[0], args[1], args[2], args[3] | ||
|
||
if stamp, ok := state[source]; ok { | ||
state[target], err = stamp.AddFromEnd(offset, calendar) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printf("%s = %s\n", target, state[target]) | ||
} else { | ||
return errors.New("timestamp '" + source + "' not set") | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package batch | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/chzyer/readline" | ||
) | ||
|
||
func init() { | ||
completions = append( | ||
completions, | ||
readline.PcItem( | ||
"add", | ||
readline.PcItemDynamic(completionCalendarList), | ||
), | ||
) | ||
|
||
commands["add"] = func(args []string) error { | ||
var err error | ||
|
||
if len(args) != 4 { | ||
return errors.New("usage: add <calendar> <offset> <source> <target>") | ||
} | ||
|
||
calendar, offset, source, target := args[0], args[1], args[2], args[3] | ||
|
||
if stamp, ok := state[source]; ok { | ||
state[target], err = stamp.Add(offset, calendar) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
printf("%s = %s\n", target, state[target]) | ||
} else { | ||
return errors.New("timestamp '" + source + "' not set") | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package batch | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/chzyer/readline" | ||
) | ||
|
||
func init() { | ||
completions = append( | ||
completions, | ||
readline.PcItem( | ||
"compare", | ||
readline.PcItemDynamic( | ||
completionStatesList, | ||
readline.PcItemDynamic( | ||
completionStatesList, | ||
readline.PcItemDynamic(func(arg string) []string { | ||
return []string{"start", "end", "start-end", "end-start", "duration"} | ||
}), | ||
), | ||
), | ||
), | ||
) | ||
|
||
commands["compare"] = func(args []string) error { | ||
if len(args) != 3 { | ||
return errors.New("usage: compare <source> <compare> <mode>") | ||
} | ||
|
||
source, compare, mode := args[0], args[1], args[2] | ||
|
||
if stamp1, ok := state[source]; ok { | ||
if stamp2, ok := state[compare]; ok { | ||
printf("%d\n", stamp1.Compare(stamp2, mode)) | ||
} else { | ||
return errors.New("timestamp '" + compare + "' not set") | ||
} | ||
} else { | ||
return errors.New("timestamp '" + source + "' not set") | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package batch | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/chzyer/readline" | ||
) | ||
|
||
func init() { | ||
completions = append( | ||
completions, | ||
readline.PcItem( | ||
"contains", | ||
readline.PcItemDynamic( | ||
completionStatesList, | ||
readline.PcItemDynamic(completionStatesList), | ||
), | ||
), | ||
) | ||
|
||
commands["contains"] = func(args []string) error { | ||
if len(args) != 2 { | ||
return errors.New("usage: contains <source> <compare>") | ||
} | ||
|
||
source, compare := args[0], args[1] | ||
|
||
if stamp1, ok := state[source]; ok { | ||
if stamp2, ok := state[compare]; ok { | ||
printf("%t\n", stamp1.Contains(stamp2)) | ||
} else { | ||
return errors.New("timestamp '" + compare + "' not set") | ||
} | ||
} else { | ||
return errors.New("timestamp '" + source + "' not set") | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package batch | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"strings" | ||
|
||
"github.com/chzyer/readline" | ||
"github.com/danhunsaker/calends" | ||
"github.com/danhunsaker/calends/calendars" | ||
"github.com/mattn/go-shellwords" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
// Collections | ||
var completions []readline.PrefixCompleterInterface | ||
var commands = make(map[string]func([]string) error) | ||
var state = make(map[string]calends.Calends) | ||
|
||
// Helper functions | ||
var printf func(string, ...interface{}) // Need the readline session to properly define this one | ||
var completionCalendarList = func(arg string) []string { | ||
return calendars.ListRegistered() | ||
} | ||
var completionStatesList = func(arg string) (list []string) { | ||
for name := range state { | ||
list = append(list, name) | ||
} | ||
return | ||
} | ||
|
||
// Main logic | ||
var Console = func(c *cli.Context) error { | ||
completions = append( | ||
completions, | ||
readline.PcItem("help"), | ||
readline.PcItem("exit"), | ||
readline.PcItem("quit"), | ||
) | ||
completer := readline.NewPrefixCompleter(completions...) | ||
shellwords.ParseEnv = true | ||
shellwords.ParseBacktick = true | ||
|
||
l, err := readline.NewEx(&readline.Config{ | ||
Prompt: "\033[35mcalends \033[36m» \033[0m", | ||
HistoryFile: "", | ||
AutoComplete: completer, | ||
InterruptPrompt: "^C", | ||
EOFPrompt: "exit", | ||
HistorySearchFold: true, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer l.Close() | ||
|
||
// Normally this helper would be defined entirely outside the main logic | ||
// function, but we need the readline session, first, to make it work... | ||
printf = func(message string, args ...interface{}) { | ||
io.WriteString(l.Stdout(), fmt.Sprintf(message, args...)) | ||
} | ||
|
||
log.SetOutput(l.Stderr()) | ||
for { | ||
line, err := l.Readline() | ||
if err == readline.ErrInterrupt { | ||
if len(line) == 0 { | ||
break | ||
} else { | ||
continue | ||
} | ||
} else if err == io.EOF { | ||
break | ||
} | ||
|
||
line = strings.TrimSpace(line) | ||
|
||
if line == "" || line == "help" { | ||
printf("commands:\n") | ||
printf(completer.Tree(" ")) | ||
continue | ||
} else if line == "quit" || line == "exit" { | ||
break | ||
} | ||
|
||
args, err := shellwords.Parse(line) | ||
if err != nil { | ||
log.Printf("%v\n", err) | ||
} else if cmd, ok := commands[args[0]]; ok { | ||
err = cmd(args[1:]) | ||
if err != nil { | ||
log.Printf("%v\n", err) | ||
} | ||
} else { | ||
log.Printf("unknown command %q\n", args[0]) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.