diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2018-10-24 13:58:30 +0200 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2018-10-24 13:58:30 +0200 |
| commit | 261d250f50b17117e99ab8b4b6ac7dfd35371386 (patch) | |
| tree | 5ef9261936af3d87ea0356997863ccf5d1253d84 | |
| parent | 3bbc28806f43c323b21d9053a9e25c89614d5f60 (diff) | |
Finished Interface Framing
| -rw-r--r-- | interact.go | 10 | ||||
| -rw-r--r-- | main.go | 7 | ||||
| -rw-r--r-- | sqlite.go | 224 | ||||
| -rw-r--r-- | utils.go | 15 |
4 files changed, 180 insertions, 76 deletions
diff --git a/interact.go b/interact.go index 3f3339b..4b54315 100644 --- a/interact.go +++ b/interact.go @@ -45,7 +45,8 @@ func interact() { //c.Print("\033[H\033[2J") //c.Println(boldGreen("Start New Project")) newProject() - showLastProject() + //showLastProject() + stdOut() c.Println(promptcol("______________________")) }, }) @@ -99,7 +100,8 @@ func interact() { LongHelp: ` Usage: status Shows the current Project, its last Tasks and if there is a open Task.`, Func: func(c *ishell.Context) { - stdOut() + //stdOut() + showStatus(true) c.Println(promptcol("______________________")) }, }) @@ -330,7 +332,7 @@ func interact() { argi, err := strconv.Atoi(arg) if err == nil { editCustomer(argi) - allCustomers() + allCustomers(false) //stdOut() } else { c.Println(boldRed(arg, "is not a valid id!")) @@ -418,7 +420,7 @@ func interact() { LongHelp: ` Usage: allcustomers Show all Customers.`, Func: func(c *ishell.Context) { - allCustomers() + allCustomers(false) c.Println(promptcol("______________________")) }, }) @@ -97,9 +97,10 @@ func title(frame bool) { // the state of Laboravi immediately. func stdOut() { - showLastProject() - getClosedTasks(0) - showOpenTask() + //showLastProject() + //getClosedTasks(0) + //showOpenTask() + showStatus(false) } func main() { @@ -213,8 +213,8 @@ func newTaskTime(tim string) { updateProject(currproject.id) } else { fmt.Println(nli+boldRed("Another Task is already Open")) - fmt.Println(frame("Close Task First",false)) - showOpenTask() + //fmt.Println(frame("Close Task First",false)) + showCurrentTask() } } @@ -853,41 +853,83 @@ func getOpenTask() { //opentask.checkout = checkout != 0 } -func showOpenTask() { +func showStatus(full bool) { +/* boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + fmt.Println(frame(boldGreen("Status"),true)) + fmt.Println(sub("Current Project")) + fmt.Println(nli,currproject.id, ":", currproject.name, "- Started:", currproject.first.Local().Format("Mon _2 Jan 2006")) + fmt.Println(nli," Last Changes", currproject.last.Local().Format("2006 Mon Jan _2 15:04")) +*/ + showOpenProject(true) + + if full { + getClosedTasks(0) + } + + showCurrentTask() + +/* if opentask.id == 0 { + if pausetask > 0 { + st := fmt.Sprintf("Task %v Paused", pausetask) + fmt.Println(frame(st,false)) + } else { + fmt.Println(frame("No Open Tasks",false)) + } + } else { + fmt.Println(sub("Open Task")) + dur := float64(time.Now().Sub(opentask.start)) / (1000000000 * 60 * 60) + fmt.Printf("%s %v: %v - (%v) - %.2f h\n", nli, opentask.id, opentask.taskname, opentask.start.Local().Format("Mon Jan _2 2006 15:04"), dur) + fmt.Println(frame("",false)) + } +*/ +} + +func showCurrentTask() { if opentask.id == 0 { if pausetask > 0 { - fmt.Printf("___Task %v Paused___________\n", pausetask) + //fmt.Printf("___Task %v Paused___________\n", pausetask) + st := fmt.Sprintf("Task %v Paused", pausetask) + fmt.Println(frame(st,false)) } else { - fmt.Println("___No Open Tasks____________") + //fmt.Println("___No Open Tasks____________") + fmt.Println(frame("No Open Tasks",false)) } } else { - fmt.Println("___Open Task________________") + //fmt.Println("___Open Task________________") + fmt.Println(sub("Open Task")) dur := float64(time.Now().Sub(opentask.start)) / (1000000000 * 60 * 60) - fmt.Printf(" %v: %v - (%v) - %.2f h\n", opentask.id, opentask.taskname, opentask.start.Local().Format("Mon Jan _2 2006 15:04"), dur) + //fmt.Printf(" %v: %v - (%v) - %.2f h\n", opentask.id, opentask.taskname, opentask.start.Local().Format("Mon Jan _2 2006 15:04"), dur) //fmt.Println(opentask.id,":", opentask.taskname,"-", opentask.start.Local().Format("Mon Jan _2 2006 15:04"),dur,"h") + fmt.Printf("%s %v: %v - (%v) - %.2f h\n", nli, opentask.id, opentask.taskname, opentask.start.Local().Format("Mon Jan _2 2006 15:04"), dur) + fmt.Println(frame("",false)) } } -func showLastProject() { - fmt.Println("___Last Project_____________") - fmt.Println(currproject.id, ":", currproject.name, "- Started:", currproject.first.Local().Format("Mon _2 Jan 2006")) - fmt.Println(" Last Changes", currproject.last.Local().Format("2006 Mon Jan _2 15:04")) +func showOpenProject(alone bool) { + boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + //fmt.Println("___Last Project_____________") + if alone { + fmt.Println(frame(boldGreen("Current Project"),true)) + } + fmt.Println(nli,currproject.id, ":", currproject.name, "- Started:", currproject.first.Local().Format("Mon _2 Jan 2006")) + fmt.Println(nli," Last Changes", currproject.last.Local().Format("2006 Mon Jan _2 15:04")) + //fmt.Println(frame("Current Project",true)) } func addCustomer() { boldRed := color.New(color.FgRed, color.Bold).SprintFunc() boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() - fmt.Println(boldGreen("Adding new Customer")) - com := getInterInput("Enter Customer Company: ") - nam := getInterInput("Enter Customer Name: ") - add := getInterInput("Enter Address (separate lines by ; [Street;Zip;City;Country]): ") + fmt.Println(frame(boldGreen("Adding new Customer"),true)) + com := getInterInput(sli+"Enter Customer Company: ") + nam := getInterInput(sli+"Enter Customer Name: ") + add := getInterInput(sli+"Enter Address (separate lines by ; [Street;Zip;City;Country]): ") sat := 0.0 for { - satstr := getInterInput("Hourly Rate: ") + satstr := getInterInput(sli+"Hourly Rate: ") sat, err = strconv.ParseFloat(satstr, 64) //checkErr(err) if err != nil { - fmt.Println(satstr, boldRed("can not be Parsed as a Float."), "Try a shape of X.X") + fmt.Println(nli,satstr, boldRed("can not be Parsed as a Float."), "Try a shape of X.X") } else { break } @@ -897,43 +939,48 @@ func addCustomer() { checkErr(err) _, err = stmt.Exec(com, nam, add, sat) checkErr(err) - fmt.Println(boldGreen(" Customer Successfully Added:"), com, nam, add, sat) + fmt.Println(nli,boldGreen(" Customer Successfully Added:"), com, nam, add, sat) + fmt.Println(frame(posR(),false)) } func newProject() { boldRed := color.New(color.FgRed, color.Bold).SprintFunc() boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() + fmt.Println(frame(boldGreen("Creating new Project"),true)) if opentask.id > 0 { - fmt.Println(boldRed("There is an Open Task")) - showOpenTask() + fmt.Println(nli,boldRed("There is an Open Task")) + fmt.Println(frame(negR(),false)) + //showOpenTask() return } if pausetask > 0 { - fmt.Println(boldRed("Task ", pausetask, " pause status removed")) + fmt.Println(sli,boldRed("Task ", pausetask, " pause status removed")) + fmt.Println(nli) setPauseTask(0) } - fmt.Println(boldGreen("Creating new Project")) - nam := getInterInput("Enter Project Name: ") + //fmt.Println(boldGreen("Creating new Project")) + nam := getInterInput(sli+"Enter Project Name: ") icust := 0 - allCustomers() + allCustomers(true) for { - cust := getInterInput("Enter Customer id: ") + cust := getInterInput(sli+"Enter Customer id: ") icust, err = strconv.Atoi(cust) if err == nil && (isCustomer(icust) || icust == 0) { break } else { - fmt.Println(cust, boldRed("is an invalid ID or Not a known Customer")) + fmt.Println(nli,cust, boldRed("is an invalid ID or Not a known Customer")) } } comm := "" - if isInterSure("Do you want to Comment the Project?") { - comm = getInterMultiInput("New Comment: ") + if isInterSure(sli+"Do you want to Comment the Project?") { + comm = getInterMultiInput(nli+"New Comment: ") } stmt, err := db.Prepare("INSERT INTO projects(name, comment, finished, customer) values(?, ?, ?, ?)") checkErr(err) _, err = stmt.Exec(nam, comm, 0, icust) checkErr(err) - fmt.Println(" Project Created:", nam) + fmt.Println(nli," Project Created:", nam) + fmt.Println(frame(posR(),false)) getLastProject() } @@ -941,10 +988,10 @@ func getClosedTasks(num int) { rows, err := db.Query("SELECT * FROM timetable WHERE stop != '1791-09-30 19:07' ORDER BY datetime(start)", currproject.id) checkErr(err) if num > 0 { - rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07'ORDER BY datetime(start) DESC LIMIT $2", currproject.id, num) + rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07' ORDER BY datetime(start) DESC LIMIT $2", currproject.id, num) checkErr(err) } else { - rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07'ORDER BY datetime(start)", currproject.id) + rows, err = db.Query("SELECT * FROM timetable WHERE project = $1 AND checkout > 0 AND stop != '1791-09-30 19:07' ORDER BY datetime(start)", currproject.id) checkErr(err) } var id, proj, check int @@ -958,19 +1005,23 @@ func getClosedTasks(num int) { //} for rows.Next() { if first { - fmt.Println("___Billed Tasks_______________") + //fmt.Println("___Billed Tasks_______________") + fmt.Println(sub("Billed Tasks")) first = false } - err = rows.Scan(&id, &proj, &sta, &sto, &tas, &check) + err = rows.Scan(&id, &proj, &sta, &sto, &tas, &com, &check) checkErr(err) dur = float64(sto.Sub(sta)) / (1000000000 * 60 * 60) - fmt.Printf("%v: %v (%v-%v) - %.2f h\n", id, tas, sta.Local().Format("2006 Mon Jan _2 15:04"), sto.Local().Format("15:04"), dur) + fmt.Printf("%s %v: %v (%v-%v) - %.2f h\n", nli, id, tas, sta.Local().Format("2006 Mon Jan _2 15:04"), sto.Local().Format("15:04"), dur) //fmt.Println(id,tas,sta.Local().Format("2006 Mon Jan _2 15:04"),sto.Local().Format("15:04"),dur,"h") sum += dur } if !first { - fmt.Println("____________________________") - fmt.Printf("Billed: %.2f h\n", sum) + //fmt.Println("____________________________") + //fmt.Printf("Billed: %.2f h\n", sum) + st := fmt.Sprintf("Billed: %.2f h", sum) + fmt.Println(sub(st)) + fmt.Println(nli) } rows.Close() @@ -988,20 +1039,23 @@ func getClosedTasks(num int) { //} for rows.Next() { if first { - fmt.Println("___Past Tasks_______________") + //fmt.Println("___Past Tasks_______________") + fmt.Println(sub("Past Tasks")) first = false } err = rows.Scan(&id, &proj, &sta, &sto, &tas, &com, &check) checkErr(err) dur = float64(sto.Sub(sta)) / (1000000000 * 60 * 60) - fmt.Printf("%v: %v (%v-%v) - %.2f h\n", id, tas, sta.Local().Format("2006 Mon Jan _2 15:04"), sto.Local().Format("15:04"), dur) + fmt.Printf("%s %v: %v (%v-%v) - %.2f h\n", nli, id, tas, sta.Local().Format("2006 Mon Jan _2 15:04"), sto.Local().Format("15:04"), dur) //fmt.Println(id,tas,sta.Local().Format("2006 Mon Jan _2 15:04"),sto.Local().Format("15:04"),dur,"h") sum2 += dur } //if err != nil && err != sql.ErrNoRows{ if !first { - fmt.Println("____________________________") - fmt.Printf("Unbilled: %.2f[h] Total: %.2f[h]\n", sum2, sum+sum2) + //fmt.Println("____________________________") + //fmt.Printf("Unbilled: %.2f[h] Total: %.2f[h]\n", sum2, sum+sum2) + st := fmt.Sprintf("Unbilled: %.2f[h] Total: %.2f[h]", sum2, sum+sum2) + fmt.Println(sub(st)) } rows.Close() } @@ -1040,24 +1094,29 @@ func getLastProject() { currproject.finished = nfinish //currproject.finish = nfinish != 0 currproject.customer = ncustom + } func setProject(nid int) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() + fmt.Println(frame(boldGreen("Opening Project ", nid),true)) if opentask.id > 0 { - fmt.Println(boldRed("There is an Open Task")) - showOpenTask() + fmt.Println(nli,boldRed("There is an Open Task")) + //fmt.Println(frame("",false)) + showCurrentTask() return } if pausetask > 0 { - fmt.Println(boldRed("Task ", pausetask, " pause status removed")) + fmt.Println(sli,boldRed("Task ", pausetask, " pause status removed")) + fmt.Println(nli) setPauseTask(0) } - if isProject(nid) { - fmt.Println(boldGreen("Opening Project ", nid)) - } else { - fmt.Println(boldRed("There is no Project"), nid) + if !isProject(nid) { + //fmt.Println(boldGreen("Opening Project ", nid)) + //} else { + fmt.Println(nli,boldRed("There is no Project"), nid) + fmt.Println(frame("",false)) return } @@ -1085,6 +1144,9 @@ func setProject(nid int) { //currproject.finish = finish != 0 currproject.customer = custo updateProject(uid) + + showOpenProject(false) + showCurrentTask() } func getCustomerList() (outint []int, outstr []string) { @@ -1103,7 +1165,8 @@ func getCustomerList() (outint []int, outstr []string) { return } -func allCustomers() { +func allCustomers(inline bool) { + boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() rows, err := db.Query("SELECT * FROM customers") //rows,err := db.Query("SELECT (id, company, name, address, satz, lastbill) FROM customers") checkErr(err) @@ -1115,22 +1178,40 @@ func allCustomers() { var satz float64 var last time.Time - fmt.Println("___All Customers________________") + //fmt.Println("___All Customers________________") + if inline { + fmt.Println(sub(boldGreen("All Customers"))) + }else{ + fmt.Println(frame(boldGreen("All Customers"),true)) + } + cnt := 0 for rows.Next() { + cnt++ err = rows.Scan(&uid, &comp, &name, &addr, &satz, &last) checkErr(err) lstr := last.Local().Format("2006-01-02 15:04 MST") if lstr == "1791-09-30 20:12 LMT" { lstr = "Never" } - fmt.Printf(" %v:%s: %s, Rate: %.2f[€/h] , Last Paid Bill: %s\n", uid, comp, name, satz, lstr) + if uid > 0 { + fmt.Printf("%s %v:%s: %s, Rate: %.2f[€/h] , Last Paid Bill: %s\n", nli, uid, comp, name, satz, lstr) + } + } + if cnt==0 { + fmt.Println(nli," Nobody") + } + if inline { + fmt.Println(sub("")) + }else{ + fmt.Println(frame("",false)) } } func allProjects() { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() - fmt.Println(boldGreen("Loading all customers")) - fmt.Println("___All Projects________________") + //fmt.Println(boldGreen("Loading all customers")) + //fmt.Println("___All Projects________________") + fmt.Println(frame(boldGreen("All Projects"),true)) rows3, err := db.Query("SELECT * FROM customers") checkErr(err) @@ -1155,7 +1236,7 @@ func allProjects() { var customer, check int var start, stop time.Time - fmt.Println("____For", com, nam) + fmt.Printf("%s%s %s: %s, %s\n",ssli,li,"For", com, nam) for rows.Next() { err = rows.Scan(&uid, &prname, &comm, &first, &last, &finish, &customer) checkErr(err) @@ -1174,25 +1255,28 @@ func allProjects() { //fmt.Printf(" %v:%s \n First: %s, Last:%s, Total:%.2f(h) ,Fin:%v, For:%v\n",uid,prname,first.Local().Format("2006-01-02 15:04 MST"),last.Local().Format("2006-01-02 15:04 MST"),sum,finish,customer) if (sumo + sumb) > 0 { - fmt.Printf(" %v:%s \n", uid, prname) - fmt.Printf(" Unbilled: %.2f[h] Billed: %.2f[h] | Total: %.2f[h]\n", sumo, sumb, sumo+sumb) + fmt.Printf("%s %v:%s \n", sli, uid, prname) + fmt.Printf("%s Unbilled: %.2f[h] Billed: %.2f[h] | Total: %.2f[h]\n", nli, sumo, sumb, sumo+sumb) //fmt.Printf(" First: %s, Last:%s, Fin:%v, For:%v\n\n",first.Local().Format("2006-01-02 15:04 MST"),last.Local().Format("2006-01-02 15:04 MST"),finish,customer) - fmt.Printf(" First: %s, Last:%s, \n\n", first.Local().Format("2006-01-02 15:04 MST"), last.Local().Format("2006-01-02 15:04 MST")) + fmt.Printf("%s First: %s, Last:%s, \n%s\n", nli, first.Local().Format("2006-01-02 15:04 MST"), last.Local().Format("2006-01-02 15:04 MST"),nli) } else { - fmt.Print(" Nothing\n") + if uid >0 { + fmt.Print(sli," Nothing\n") + } } rows2.Close() //good habit to close } rows.Close() //good habit to close } rows3.Close() //good habit to close - fmt.Println("_______________________________\n") + fmt.Println(frame("",false)) + //fmt.Println("_______________________________\n") } func deleteBill(id int) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() - fmt.Println(boldGreen("Deleting Bill ", id)) + fmt.Println(frame(boldGreen("Deleting Bill ", id),true)) var prj int var identity, moneys, hours string var date time.Time @@ -1206,27 +1290,29 @@ func deleteBill(id int) { prstr, custr := getProjectName(prj) hsum := sumFloatArray(string2FloatArray(hours, ";")) msum := sumFloatArray(string2FloatArray(moneys, ";")) - fmt.Printf("%v: For %v- %v (%v) - %.1f[h] : %.2f[€]\n", identity, custr, prstr, date.Local().Format("2006 Mon Jan _2"), hsum, msum) - if isInterSure("Are You Sure?") { + fmt.Printf("%s %v: For %v- %v (%v) - %.1f[h] : %.2f[€]\n", nli, identity, custr, prstr, date.Local().Format("2006 Mon Jan _2"), hsum, msum) + if isInterSure(sli+"Are You Sure?") { uncheckTasks(id) //Set corresponding Tasks to checkout=0 stmt, err := db.Prepare("DELETE FROM bills WHERE id = ?") checkErr(err) _, err = stmt.Exec(id) checkErr(err) - fmt.Println(boldGreen("Bill ", id, " deleted successfully!")) + fmt.Println(nli,boldGreen("Bill ", id, " deleted successfully!")) } else { + fmt.Println(frame(negR(),false)) return } } else { - fmt.Println(boldRed(id, " is Not a known Bill!")) + fmt.Println(nli,boldRed(id, " is Not a known Bill!")) showLastBills(0) } + fmt.Println(frame("",false)) } func deleteTask(id int) { boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() - fmt.Println(boldGreen("Deleting Task ", id)) + fmt.Println(frame(boldGreen("Deleting Task ", id),true)) var chk, prj int var start, stop time.Time var task, comm string @@ -1238,19 +1324,21 @@ func deleteTask(id int) { rows.Close() //good habit to close //fmt.Println(boldGreen("Delete Task", id)) dur := float64(stop.Sub(start)) / (1000000000 * 60 * 60) - fmt.Printf("%v: %v (%v-%v) - %.2f h\n Comments:\n%s\n", prj, task, start.Local().Format("2006 Mon Jan _2 15:04"), stop.Local().Format("15:04"), dur, comm) - if isInterSure("Are You Sure?") { + fmt.Printf("%s %v: %v (%v-%v) - %.2f h\n Comments:\n%s\n", nli, prj, task, start.Local().Format("2006 Mon Jan _2 15:04"), stop.Local().Format("15:04"), dur, comm) + if isInterSure(sli+"Are You Sure?") { stmt, err := db.Prepare("DELETE FROM timetable WHERE id = ?") checkErr(err) _, err = stmt.Exec(id) checkErr(err) - fmt.Println(boldGreen("Task ", id, " deleted successfully!")) + fmt.Println(nli,boldGreen("Task ", id, " deleted successfully!")) } else { + fmt.Println(frame(negR(),false)) return } } else { - fmt.Println(boldRed(id, " is Not a known Task!")) + fmt.Println(nli,boldRed(id, " is Not a known Task!")) } + fmt.Println(frame("",false)) } func editCustomer(id int) { @@ -15,9 +15,10 @@ import ( ) const ( - framewidth int = 40 + framewidth int = 50 li string = "\u2500" mli string = "\u2500\u2530\u2500\u2500" //Masterline -T- + ssli string = " \u2520\u2500\u2500" //Slave Line L__ sli string = " \u2520\u2500 " //Slave Line L_ nli string = " \u2503 " //noone Line | fli string = " \u2516\u2500\u2500" //Footer Line L @@ -42,6 +43,18 @@ func negR() (out string){ return } +func sub(text string) (out string) { + out = ssli +text + n := utf8.RuneCountInString(out) + r := framewidth - n + if r < 1 { + out = out + li + }else{ + out = out + strings.Repeat(li,r) + } + return +} + func frame(text string, head bool) (out string) { if head { out = mli + text |
