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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 66 +++++++++++++----- sqlite.go | 181 +++++++++++++++++++++++++++-------------------- 3 files changed, 381 insertions(+), 95 deletions(-) create mode 100644 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() + //} +} + + diff --git a/main.go b/main.go index 6681bb4..5fc8050 100644 --- a/main.go +++ b/main.go @@ -8,19 +8,34 @@ import ( "os" //"strings" _ "os/exec" + //"github.com/fatih/color" ) //var svar string var starttime,stoptime string var projectid, edittaskid, editprojectid int -var newproject, newtask, stoptask, allproj bool +var newproject, newtask, stoptask, allproj, runinter bool + +//var red, green, yellow, cyan color +//var boldRed, boldGreen color func init() { + //red := color.New(color.FgRed).SprintFunc + //green := color.New(color.FgGreen) + //yellow := color.New(color.FgYellow) + //use of package color without selectorcyan := color.New(color.FgCyan) + //boldGreen := color.New(color.FgGreen, color.Bold) + //boldRed := color.New(color.FgRed, color.Bold) + flag.BoolVar(&allproj, "all", false, "Show all Projects") + flag.BoolVar(&runinter, + "run", + false, + "Run in Interactive mode") flag.BoolVar(&newtask, "startnow", false, @@ -59,6 +74,13 @@ func init() { } +func stdOut() { + + showLastProject() + getClosedTasks(0) + showOpenTask() +} + func main() { if len(flag.Args())>0 { fmt.Println("Unknown Commands: ",flag.Args()) @@ -72,43 +94,51 @@ func main() { if allproj { allProjects() } + + if runinter { + interact() + } if newproject { newProject() os.Exit(0) } else if newtask { newTask(projectid) - showLastProject() - getClosedTasks(0) - showOpenTask() - os.Exit(0) + stdOut() + //showLastProject() + //getClosedTasks(0) + //showOpenTask() + //os.Exit(0) } else if stoptask { closeTask() - showLastProject() - getClosedTasks(0) - showOpenTask() + stdOut() + //showLastProject() + //getClosedTasks(0) + //showOpenTask() } else if (starttime != "now") { newTaskTime(projectid,starttime) os.Exit(0) } else if (stoptime != "now") { closeTaskTime(stoptime) - showLastProject() - getClosedTasks(0) - showOpenTask() + stdOut() + //showLastProject() + //getClosedTasks(0) + //showOpenTask() } else if (edittaskid>0) { editTask(edittaskid) } else if (editprojectid>0) { editProject(editprojectid) } else if (projectid>0) { setProject(projectid) - showLastProject() - getClosedTasks(0) - showOpenTask() + stdOut() + //showLastProject() + //getClosedTasks(0) + //showOpenTask() } else { - showLastProject() - getClosedTasks(0) - showOpenTask() - + //showLastProject() + //getClosedTasks(0) + //showOpenTask() + stdOut() } //fmt.Println("Start Act:",actstart) diff --git a/sqlite.go b/sqlite.go index 9082855..273c91b 100644 --- a/sqlite.go +++ b/sqlite.go @@ -9,7 +9,9 @@ import ( "strings" "regexp" "strconv" + _ "github.com/mattn/go-sqlite3" + "github.com/fatih/color" ) type project struct{ @@ -64,6 +66,7 @@ func initDB(filename string) { } func newTaskTime(proj int, tim string) { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() if (opentask.id == 0) { timstr := "1791-09-30 19:07" if isDate(tim) { @@ -72,8 +75,9 @@ func newTaskTime(proj int, tim string) { currdate := time.Now().UTC().Format("2006-01-02") timstr = currdate+" "+getTime(tim) }else { - fmt.Println(tim,"is Not a Valid Timestring! use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") - os.Exit(0) + fmt.Println(tim,boldRed("is Not a Valid Timestring!"), "use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") + return + //os.Exit(0) } stmt, err := db.Prepare("INSERT INTO timetable(project, start, task, checkout) values(?, datetime(?,'utc'), ?, ?)") fmt.Println(timstr) @@ -90,13 +94,14 @@ func newTaskTime(proj int, tim string) { getOpenTask() updateProject(currproject.id) } else { - fmt.Println("Another Task is already Open") + fmt.Println(boldRed("Another Task is already Open")) showOpenTask() } } func newTask(proj int) { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() if (opentask.id == 0) { task := getInput("Specify Task: ") stmt, err := db.Prepare("INSERT INTO timetable(project, task, checkout) values(?, ?, ?)") @@ -111,14 +116,15 @@ func newTask(proj int) { getOpenTask() updateProject(currproject.id) } else { - fmt.Println("Another Task is already Open") + fmt.Println(boldRed("Another Task is already Open")) showOpenTask() } } func closeTaskTime(tim string) { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() if opentask.id==0 { - fmt.Println("There is no Open Task") + fmt.Println(boldRed("There is no Open Task")) return } //timt,err := time.Parse("2006-01-02 15:04",tim) @@ -131,8 +137,9 @@ func closeTaskTime(tim string) { timstr = time.Now().Local().Format("2006-01-02")+" "+getTime(tim) timst = timstr+" "+zone } else { - fmt.Println(tim,"is Not a Valid Timestring! use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") - os.Exit(0) + fmt.Println(tim,boldRed("is Not a Valid Timestring!"), "use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") + return + //os.Exit(0) } timt,err := time.Parse("2006-01-02 15:04 MST",timst) @@ -149,14 +156,15 @@ func closeTaskTime(tim string) { opentask.id=0 updateProject(opentask.projectid) }else{ - fmt.Println("Cannot Stop before the Beginning!") + fmt.Println(boldRed("Cannot Stop before the Beginning!")) } //fmt.Println(tim,timt) } func closeTask() { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() if opentask.id==0 { - fmt.Println("There is no Open Task") + fmt.Println(boldRed("There is no Open Task")) return } if (time.Now().After(opentask.start.Local())) { @@ -168,7 +176,7 @@ func closeTask() { opentask.id=0 updateProject(opentask.projectid) }else{ - fmt.Println("Cannot Stop before the Beginning!") + fmt.Println(boldRed("Cannot Stop before the Beginning!")) } } @@ -223,6 +231,7 @@ func showLastProject() { func newProject() { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() if (opentask.id == 0) { nam := getInput("Enter Project Name: ") stmt, err := db.Prepare("INSERT INTO projects(name, finished) values(?, ?)") @@ -232,14 +241,12 @@ func newProject() { fmt.Println(" Project Created:",nam) getLastProject() } else { - fmt.Println("There is an Open Task") + fmt.Println(boldRed("There is an Open Task")) showOpenTask() } } func getClosedTasks(num int) { - //var rows sql.Rows - //var err error fmt.Println("___Past Tasks_______________") rows,err := db.Query("SELECT * FROM timetable WHERE stop != '1791-09-30 19:07' ORDER BY datetime(start)",currproject.id) checkErr(err) @@ -266,7 +273,7 @@ func getClosedTasks(num int) { } rows.Close() fmt.Println("____________________________") - fmt.Println("Gesamt:",sum,"h") + fmt.Printf("Gesamt: %.2f h\n",sum) } func getLastProject() { @@ -291,39 +298,45 @@ func getLastProject() { } } rows.Close() //good habit to close - // fmt.Println(nuid,"Last Project",nprname) - // fmt.Println(nlast) - // fmt.Println("----------------------------------") currproject.id = nuid currproject.name = nprname currproject.first = nfirst currproject.last = nlast currproject.finish = nfinish - // getLastTasks(0) } func setProject (nid int) { - rows,err := db.Query("SELECT * FROM projects WHERE id = $1",nid) - checkErr(err) + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() + if !isProject(nid){ + fmt.Println(boldRed("There is no Project"),nid) + return + } + if (opentask.id == 0) { + rows,err := db.Query("SELECT * FROM projects WHERE id = $1",nid) + checkErr(err) - var uid int - var prname string - var first time.Time - var last time.Time - var finish int + var uid int + var prname string + var first time.Time + var last time.Time + var finish int - for rows.Next() { - err = rows.Scan(&uid, &prname, &first, &last, &finish) - checkErr(err) - } - rows.Close() //good habit to close - currproject.id = uid - currproject.name = prname - currproject.first = first - currproject.last = last - currproject.finish = finish - updateProject(uid) + for rows.Next() { + err = rows.Scan(&uid, &prname, &first, &last, &finish) + checkErr(err) + } + rows.Close() //good habit to close + currproject.id = uid + currproject.name = prname + currproject.first = first + currproject.last = last + currproject.finish = finish + updateProject(uid) + } else { + fmt.Println(boldRed("There is an Open Task")) + showOpenTask() + } } func allProjects() { @@ -346,6 +359,7 @@ func allProjects() { } func editTask(id int) { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() var chk,prj int var start,stop time.Time var task,startstr,stopstr string @@ -355,8 +369,9 @@ func editTask(id int) { err = rows.Scan(&prj, &start, &stop, &task, &chk) checkErr(err) }else{ - fmt.Println("There Is No Task",id) - os.Exit(0) + fmt.Println(boldRed("There Is No Task"),id) + return + //os.Exit(0) } rows.Close() //good habit to close @@ -368,38 +383,46 @@ func editTask(id int) { } startstr=start.Local().Format("2006-01-02 15:04") stopstr=stop.Local().Format("2006-01-02 15:04") - fmt.Println("Old Start:",startstr) - newstart := getInput("Enter New:") - if newstart!=""{ - if isDate(newstart) { - startstr=newstart - }else{ - fmt.Println(newstart,"is Not a Valid Timestring! use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") - os.Exit(0) - } + for{ + fmt.Println("Old Start:",startstr) + newstart := getInput("Enter New:") + if newstart!=""{ + if isDate(newstart) { + startstr=newstart + break + }else{ + fmt.Println(newstart,boldRed("is Not a Valid Timestring!"), "use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") + //os.Exit(0) + } + }else{break} } fmt.Println("Old End:",stopstr) - newend := getInput("Enter New:") - if newend!=""{ - if isDate(newend) { - stopstr=newend - }else{ - fmt.Println(newend,"is Not a Valid Timestring! use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") - os.Exit(0) - } + for{ + newend := getInput("Enter New:") + if newend!=""{ + if isDate(newend) { + stopstr=newend + break + }else{ + fmt.Println(newend,boldRed("is Not a Valid Timestring!"), "use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") + //os.Exit(0) + } + }else{break} } fmt.Println("Old Project:",prj) - newprj := getInput("Enter New:") - if newprj!=""{ - prj,err = strconv.Atoi(newprj) - if err != nil { - fmt.Println(newprj,"is Not a Valid id. Try an Integer instead") - os.Exit(0) - } - if !isProject(prj) { - fmt.Println("There is no project",prj,"Try -all for Project List") - os.Exit(0) - } + for{ + newprj := getInput("Enter New:") + if newprj!=""{ + prj,err = strconv.Atoi(newprj) + if err != nil { + fmt.Println(newprj,boldRed("is Not a Valid id."), "Try an Integer instead") + //os.Exit(0) + } + if !isProject(prj) { + fmt.Println(boldRed("There is no project"),prj,"Try -all for Project List") + //os.Exit(0) + }else{break} + }else{break} } stmt, err := db.Prepare("UPDATE timetable SET task = ?, start = datetime(?,'utc'), stop = datetime(?,'utc'), project = ? WHERE id = ?") checkErr(err) @@ -410,6 +433,7 @@ func editTask(id int) { } func editProject(id int) { + boldRed := color.New(color.FgRed, color.Bold).SprintFunc() var fin int var first time.Time var name,nfirst string @@ -426,19 +450,22 @@ func editProject(id int) { } nfirst=first.Local().Format("2006-01-02 15:04") fmt.Println("Old Begin:",nfirst) - newfirst := getInput("Enter New:") - if newfirst!=""{ - if isDate(newfirst) { - nfirst=newfirst - }else{ - fmt.Println(newfirst,"is Not a Valid Timestring! use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") - os.Exit(0) - } + for{ + newfirst := getInput("Enter New:") + if newfirst!=""{ + if isDate(newfirst) { + nfirst=newfirst + break + }else{ + fmt.Println(newfirst,boldRed("is Not a Valid Timestring!"), "use: 'YYYY-MM-DD HH:MM' or 'HH:MM'") + //os.Exit(0) + } + }else{break} } - }else{ - fmt.Println("There Is No Project",id) - os.Exit(0) + fmt.Println(boldRed("There Is No Project"),id) + return + //os.Exit(0) } rows.Close() //good habit to close stmt, err := db.Prepare("UPDATE projects SET name = ?, last = datetime(?,'utc') WHERE id = ?") -- cgit v1.2.3