From 2d5c636a16f8b1eeff86e82f1b3c041d9b301796 Mon Sep 17 00:00:00 2001 From: Nikolaus Gotsche Date: Thu, 25 Jan 2018 03:23:19 +0100 Subject: Added Interactive Mode And Colours Gerneral Debugging --- interact.go | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 interact.go (limited to 'interact.go') diff --git a/interact.go b/interact.go new file mode 100644 index 0000000..bbce1bb --- /dev/null +++ b/interact.go @@ -0,0 +1,229 @@ +package main + +import ( + //"os" + "strings" + "strconv" + + "github.com/abiosoft/ishell" + "github.com/fatih/color" +) + + +func interact() { + shell := ishell.New() + + cyan := color.New(color.FgCyan).SprintFunc() + yellow := color.New(color.FgYellow).SprintFunc() + green := color.New(color.FgGreen).SprintFunc() + boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() + + // display info. + shell.Println("Starting interactive Shell") + + shell.AddCmd(&ishell.Cmd{ + Name: "new", + Help: "Start new Project", + Func: func(c *ishell.Context) { + c.Println(boldGreen("Start New Project")) + newProject() + showLastProject() + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "startnow", + Help: "Start a new Task immediately", + Func: func(c *ishell.Context) { + c.Println(boldGreen("New Task")) + newTask(projectid) + stdOut() + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "stopnow", + Help: "Stop the currently Open Task immediately", + Func: func(c *ishell.Context) { + c.Println(boldGreen("Stoping Task",opentask.id)) + closeTask() + stdOut() + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "project", + Help: " Open a Project of the following id", + Func: func(c *ishell.Context) { + arg := "none" + if len(c.Args) > 0 { + arg = strings.Join(c.Args, " ") + argi,err := strconv.Atoi(arg) + if err == nil{ + c.Println(boldGreen("Opening Project",argi)) + setProject(argi) + stdOut() + }else{ + c.Println(boldRed(arg,"is not a valid id!")) + } + }else{ + c.Println(boldRed("project - Please enter an id")) + allProjects() + } + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "edittask", + Help: " Edit a Task of the following id", + Func: func(c *ishell.Context) { + arg := "none" + if len(c.Args) > 0 { + arg = strings.Join(c.Args, " ") + argi,err := strconv.Atoi(arg) + if err == nil{ + c.Println(boldGreen("Editing Task",argi)) + editTask(argi) + //stdOut() + }else{ + c.Println(boldRed(arg,"is not a valid id!")) + } + }else{ + c.Println(boldRed("edittask - Please enter an id")) + } + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "editproject", + Help: " Edit the Project of the following id", + Func: func(c *ishell.Context) { + arg := "none" + if len(c.Args) > 0 { + arg = strings.Join(c.Args, " ") + argi,err := strconv.Atoi(arg) + if err == nil{ + c.Println(boldGreen("Edit Project",argi)) + editProject(argi) + allProjects() + //stdOut() + }else{ + c.Println(boldRed(arg,"is not a valid id!")) + } + }else{ + c.Println(boldRed("editproject - Please enter an id")) + allProjects() + } + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "start", + Help: " - Start Task at a specific Time 'YYYY-MM-DD HH:MM' Or 'HH:MM'", + Func: func(c *ishell.Context) { + arg := "none" + if len(c.Args) > 0 { + arg = strings.Join(c.Args, " ") + c.Println(boldGreen("Start Project at",arg)) + newTaskTime(projectid,arg) + // editProject(argi) + stdOut() + }else{ + c.Println(boldRed("start - Please enter a Datetime")) + } + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "stop", + Help: " - Stop Open Task at a specific Time 'YYYY-MM-DD HH:MM' Or 'HH:MM'", + Func: func(c *ishell.Context) { + arg := "none" + if len(c.Args) > 0 { + arg = strings.Join(c.Args, " ") + c.Println(boldGreen("Stop Task at",arg)) + closeTaskTime(arg) + stdOut() + }else{ + c.Println(boldRed("stop - Please enter a Datetime")) + } + }, + }) + + shell.AddCmd(&ishell.Cmd{ + Name: "all", + Help: "Show all Projects Project", + Func: func(c *ishell.Context) { + //c.Println(boldGreen("Start New Project")) + allProjects() + stdOut() + }, + }) + + + shell.AddCmd(&ishell.Cmd{ + Name: "color", + Help: "color print", + Func: func(c *ishell.Context) { + c.Print(cyan("cyan\n")) + c.Print(green("greencyan\n")) + c.Println(yellow("yellow")) + c.Printf("%s\n", boldRed("bold red")) + }, + }) + + // multiple choice + shell.AddCmd(&ishell.Cmd{ + Name: "choice", + Help: "multiple choice prompt", + Func: func(c *ishell.Context) { + choice := c.MultiChoice([]string{ + "Golangers", + "Go programmers", + "Gophers", + "Goers", + }, "What are Go programmers called ?") + if choice == 2 { + c.Println("You got it!") + } else { + c.Println("Sorry, you're wrong.") + } + }, + }) + +// multiple choice + shell.AddCmd(&ishell.Cmd{ + Name: "checklist", + Help: "checklist prompt", + Func: func(c *ishell.Context) { + languages := []string{"Python", "Go", "Haskell", "Rust"} + choices := c.Checklist(languages, + "What are your favourite programming languages ?", + nil) + out := func() (c []string) { + for _, v := range choices { + c = append(c, languages[v]) + } + return + } + c.Println("Your choices are", strings.Join(out(), ", ")) + }, + }) + + + shell.Run() + // teardown + shell.Close() +// when started with "exit" as first argument, assume non-interact ive execution + // if len(os.Args) > 1 && os.Args[1] == "exit" { + // shell.Process(os.Args[2:]...) + // } else { + // start shell + // shell.Run() + // teardown + // shell.Close() + //} +} + + -- cgit v1.2.3