summaryrefslogtreecommitdiff
path: root/interact.go
blob: f555b5531ae700cd349f53060e75d1eb035f51f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main

import (
    //"os"
    "strings"
    "strconv"
    "fmt"

    "github.com/abiosoft/ishell"
    "github.com/fatih/color"
)


func interact() {
    stdOut()
    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.Print("\033[H\033[2J")
            //c.Println(boldGreen("Start New Project"))
            newProject()
            showLastProject()
        },
    })
    
    shell.AddCmd(&ishell.Cmd{
        Name: "add",
        Help: "Add new Customer",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            //c.Println(boldGreen("Start New Project"))
            addCustomer()
        },
    })

   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.Print("\033[H\033[2J")
            //c.Println(boldGreen("Stoping Task",opentask.id))
            closeTask()
            stdOut()
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "deletetask",
        Help: "<id> Delete a Task with the following id",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            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))
                    deleteTask(argi)
                    stdOut()
                }else{
                    c.Println(boldRed(arg,"is not a valid id!"))
                }
            }else{
                c.Println(boldRed("deletetask <id> - Please enter an id"))
                allProjects()
            }
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "project",
        Help: "<id> Open a Project of the following id",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            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 <id> - Please enter an id"))
                allProjects()
            }
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "edittask",
        Help: "<id> Edit a Task of the following id",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            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 <id> - Please enter an id"))
            }
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "editproject",
        Help: "<id> Edit the Project of the following id",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            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 <id> - Please enter an id"))
                allProjects()
            }
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "editcustomer",
        Help: "<id> Edit the Customer of the following id",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            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))
                    editCustomer(argi)
                    allCustomers()
                    //stdOut()
                }else{
                    c.Println(boldRed(arg,"is not a valid id!"))
                }
            }else{
                c.Println(boldRed("editcustomer <id> - Please enter an id"))
                allCustomers()
            }
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "start",
        Help: "<DateTime> - 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 <DateTime> - Please enter a Datetime"))
            }
        },
    })

    shell.AddCmd(&ishell.Cmd{
        Name: "stop",
        Help: "<DateTime> - Stop Open Task at a specific Time 'YYYY-MM-DD HH:MM' Or 'HH:MM'",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            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 <DateTime> - Please enter a Datetime"))
            }
        },
    })

   shell.AddCmd(&ishell.Cmd{
        Name: "all",
        Help: "Show all Projects Project",
        Func: func(c *ishell.Context) {
            //c.Print("\033[H\033[2J")
            //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(), ", "))
         },
    })

// Gather Tasks For Bills
    shell.AddCmd(&ishell.Cmd{
        Name: "bill",
        Help: "Select Tasks to be billed",
        Func: func(c *ishell.Context) {
            nix := []int{0}
            ids,str := getTaskList(nix,true)
            choices := c.Checklist(str,
                "What Tasks do you want to bill ?",
                nil)
            out := func() (c []int) {
                for _, v := range choices {
                    c = append(c, ids[v])
                }
                return
             }

            selids := out()
            //bids,str2 := getTaskList(selids,false)
            //c.Println(bids,str2)
            c.Println(len(out()),"Tasks Selected")
            multicust, multiproj,projids := checkCustomerProjects(selids)
            // CHECK IF ONLY ONE CUSTOMER
            billprojid := 0

            if multicust {
                c.Println("Cannot Write One Bill to multiple Customers! Please Select different Tasks")
                
            }else{
                // CHECK IF ONLY ONE PROJECT ELSE CHOOSE ONE
                if multiproj {
                    prid,prstr :=getProjectList(projids)
                    sel := c.MultiChoice(prstr, "What Project Should be Billed ?")
                    billprojid = prid[sel]
                }else{
                    billprojid = projids[0]
                }
                seltasks := getSelectedTasks(selids)
                count,hours,dur := analyzeTasks(seltasks)

                //c.Printf("%v Tasks Selected. Totaling %.2f (h) - Dates: %s\n",count,hours,dur)
                proj,cust := getProject(billprojid)
                billid := newBill(billprojid)
                //prs,cus := getProjectName(billprojid)
                c.Println("For",cust.company,"-",cust.name)
                c.Println("Project:",proj.name,"- ID:",proj.id)
                c.Println("Projected Income:",hours*cust.satz,"€")

                c.Println("Create New Bill:",billid)
                c.ReadLine() //Make NEW BILL WITH ID and INV No

                fullbillinfo := fmt.Sprintf("For: %s - %s - Invoice: %s\nProject: %s - Id:%v\n%v Tasks Selected. Totaling %.2f (h) - Dates: %s\nProjected Income: %.2f(€)\n",cust.company,cust.name,billid,proj.name,proj.id,count,hours,dur,hours*cust.satz)
                sep := "-------------------------\n"
                restinfo := "Here some Info about the rest"
                restids := selids
                //allaccounted := false
                //resttasks := seltasks
                //SELECT SUBSET AND NAME BILLITEM
                //c.clear()//Println(some,"Str2:",str2)
                for {
                    if len(restids) == 0 {
                        break
                    }
                    rids,rstr := getTaskList(restids,false)
                    restinfo = "Select Tasks to Group as Billitem"
                    pre := fmt.Sprintf("%s%s%s",fullbillinfo,sep,restinfo)

                    choices2 := c.Checklist(rstr,pre,nil)
                    out = func() (c []int) {
                        for _, v := range choices2 {
                            c = append(c, rids[v])
                        }
                        return
                    }
                    taskids := out()
                    restids = removeItems(restids,taskids)
                    c.Println("Full",selids,"Your choices are",taskids,"Rest:",removeItems(selids,taskids))
                }
            }
        },
    })


    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()
    //}
}