implemented list with and without details, delete (rm), showHoursByDays
arnaucode arnaucode@gmail.com
Tue, 12 Dec 2017 20:15:14 +0100
5 files changed,
154 insertions(+),
19 deletions(-)
M
README.md
→
README.md
@@ -1,25 +1,69 @@
# wtt Work Time Tracker - console time tracking app +Add wtt to the path: +``` +export PATH="$PATH:/home/user/path/wtt" +``` ### Use -Create new project +- Create new project +``` +wtt new ProjectName +``` + - Abreviation: ``` -./wtt new ProjectName +wtt n ProjectName ``` +- List projects +``` +wtt list +``` + - Abreviation: +``` +wtt ls +``` -Start working on a project + - Also can list with more details using: ``` -./wtt start ProjectName +wtt ls -a ``` -Stop working on the current project +- Start working on a project +``` +wtt start ProjectName ``` -./wtt stop + - Abreviation: +``` +wtt s ProjectName ``` -List projects +- Stop working on the current project +``` +wtt stop +``` + +- Delete project ``` -./wtt list +wtt rm ProjectName +``` + +- Show current working ``` +wtt current +``` + - Abreviation: +``` +wtt c +``` +- Help: +``` +wtt help +``` + - Abreviation: +``` +wtt h +``` + +
M
main.go
→
main.go
@@ -25,7 +25,7 @@
//get the command line parameters if len(os.Args) > 1 { switch os.Args[1] { - case "new": + case "new", "n": //create project os.Args[2] if len(os.Args) > 2 { projectName := os.Args[2]@@ -39,11 +39,18 @@ } else {
color.Red("No project name specified") } break - case "list": - //list projects - listProjects() + case "list", "ls": + if len(os.Args) > 2 { + param := os.Args[2] + if param == "-a" { + listProjectsDetails() + } + } else { + //list projects + listProjects() + } break - case "start": + case "start", "s": //check if already there is any project started if work.CurrentProjectName != "" { color.Red("Can not start project, already project " + work.CurrentProjectName + " running")@@ -63,6 +70,8 @@ work.Projects[i].Streaks = append(work.Projects[i].Streaks, newStreak)
work.CurrentProjectName = projectName saveWork() fmt.Println("starting to work in project " + work.Projects[i].Name) + } else { + color.Red("No project name to start selected") } break case "stop":@@ -80,18 +89,46 @@ work.Projects[i].Streaks[j].End = time.Now()
work.Projects[i].Streaks[j].Duration = time.Now().Sub(work.Projects[i].Streaks[j].Start) work.CurrentProjectName = "" saveWork() - fmt.Print("Worked ") - fmt.Print(work.Projects[i].Streaks[j].Duration) - fmt.Println(" in the project " + work.Projects[i].Name) + color.Green("Worked " + work.Projects[i].Streaks[j].Duration.String() + " in the project " + work.Projects[i].Name) + break + case "rm": + if len(os.Args) > 2 { + projectName := os.Args[2] + if work.CurrentProjectName == projectName { + work.CurrentProjectName = "" + } + deleteProject(projectName) + saveWork() + color.Yellow("Project " + projectName + " deleted") + } else { + color.Red("no project name specified") + } - //stop project os.Args[2] break + case "current", "c": + if work.CurrentProjectName != "" { + fmt.Print("Current working project: ") + color.Blue(work.CurrentProjectName) + } else { + fmt.Println("No current working project.") + } + case "help", "h": + fmt.Println("./wtt new {projectname}") + fmt.Println("./wtt ls") + fmt.Println("./wtt ls -a") + fmt.Println("./wtt start {projectname}") + fmt.Println("./wtt stop") + fmt.Println("./wtt rm") + fmt.Println("./wtt current") + fmt.Println("./wtt help") default: - color.Red("no option selected") + color.Red("option not exists") os.Exit(1) } } else { color.Red("no option selected") + fmt.Println("Can run 'help' for commands information") + fmt.Println("./wtt help") os.Exit(1) } }
M
work.go
→
work.go
@@ -75,7 +75,7 @@ newProject.Name = name
work.Projects = append(work.Projects, newProject) return nil } -func listProjects() { +func listProjectsDetails() { fmt.Println("") fmt.Println("") fmt.Println("Listing projects")@@ -94,5 +94,59 @@ fmt.Print(" Duration:")
fmt.Println(streak.Duration) } fmt.Println("") + showHoursByDays(project.Name) + fmt.Println("") } + if work.CurrentProjectName != "" { + fmt.Print("Current working project: ") + color.Blue(work.CurrentProjectName) + } +} +func listProjects() { + fmt.Println("") + fmt.Println("") + fmt.Println("Listing projects") + fmt.Println("") + for k, project := range work.Projects { + fmt.Println("project " + strconv.Itoa(k)) + fmt.Print("name: ") + color.Blue(project.Name) + showHoursByDays(project.Name) + fmt.Println("") + } + if work.CurrentProjectName != "" { + fmt.Print("Current working project: ") + color.Blue(work.CurrentProjectName) + } +} +func deleteProject(name string) { + i := getProjectIByName(name) + if i < 0 { + color.Red("Project name: " + name + ", no exists") + return + } + work.Projects = append(work.Projects[:i], work.Projects[i+1:]...) +} +func showHoursByDays(name string) { + timeFormat := "Mon, 01/02/06" + i := getProjectIByName(name) + if i < 0 { + color.Red("Project name: " + name + ", no exists") + return + } + p := work.Projects[i] + days := make(map[string]time.Duration) + for _, streak := range p.Streaks { + days[streak.Start.Format(timeFormat)] = addDurations(days[streak.Start.Format(timeFormat)], streak.Duration) + } + for day, hours := range days { + fmt.Print(" Day: " + day) + fmt.Print(", total time: ") + fmt.Println(hours) + } +} +func addDurations(d1 time.Duration, d2 time.Duration) time.Duration { + s := d1.Seconds() + d2.Seconds() + r := time.Duration(s) * time.Second + return r }