implemented list with and without details, delete (rm), showHoursByDays

This commit is contained in:
arnaucode
2017-12-12 20:15:14 +01:00
parent c122ec8138
commit d04a945123
5 changed files with 156 additions and 21 deletions

View File

@@ -1,25 +1,69 @@
# wtt # wtt
Work Time Tracker - console time tracking app Work Time Tracker - console time tracking app
Add wtt to the path:
```
export PATH="$PATH:/home/user/path/wtt"
```
### Use ### Use
Create new project - Create new project
``` ```
./wtt new ProjectName wtt new ProjectName
```
- Abreviation:
```
wtt n ProjectName
``` ```
- List projects
Start working on a project
``` ```
./wtt start ProjectName wtt list
```
- Abreviation:
```
wtt ls
``` ```
Stop working on the current project - Also can list with more details using:
``` ```
./wtt stop wtt ls -a
``` ```
List projects - Start working on a project
``` ```
./wtt list wtt start ProjectName
``` ```
- Abreviation:
```
wtt s ProjectName
```
- Stop working on the current project
```
wtt stop
```
- Delete project
```
wtt rm ProjectName
```
- Show current working
```
wtt current
```
- Abreviation:
```
wtt c
```
- Help:
```
wtt help
```
- Abreviation:
```
wtt h
```
![wtt](https://raw.githubusercontent.com/arnaucode/wtt/master/wtt_demo.gif "wtt")

59
main.go
View File

@@ -25,7 +25,7 @@ func main() {
//get the command line parameters //get the command line parameters
if len(os.Args) > 1 { if len(os.Args) > 1 {
switch os.Args[1] { switch os.Args[1] {
case "new": case "new", "n":
//create project os.Args[2] //create project os.Args[2]
if len(os.Args) > 2 { if len(os.Args) > 2 {
projectName := os.Args[2] projectName := os.Args[2]
@@ -39,11 +39,18 @@ func main() {
color.Red("No project name specified") color.Red("No project name specified")
} }
break break
case "list": case "list", "ls":
//list projects if len(os.Args) > 2 {
listProjects() param := os.Args[2]
if param == "-a" {
listProjectsDetails()
}
} else {
//list projects
listProjects()
}
break break
case "start": case "start", "s":
//check if already there is any project started //check if already there is any project started
if work.CurrentProjectName != "" { if work.CurrentProjectName != "" {
color.Red("Can not start project, already project " + work.CurrentProjectName + " running") color.Red("Can not start project, already project " + work.CurrentProjectName + " running")
@@ -63,6 +70,8 @@ func main() {
work.CurrentProjectName = projectName work.CurrentProjectName = projectName
saveWork() saveWork()
fmt.Println("starting to work in project " + work.Projects[i].Name) fmt.Println("starting to work in project " + work.Projects[i].Name)
} else {
color.Red("No project name to start selected")
} }
break break
case "stop": case "stop":
@@ -80,18 +89,46 @@ func main() {
work.Projects[i].Streaks[j].Duration = time.Now().Sub(work.Projects[i].Streaks[j].Start) work.Projects[i].Streaks[j].Duration = time.Now().Sub(work.Projects[i].Streaks[j].Start)
work.CurrentProjectName = "" work.CurrentProjectName = ""
saveWork() saveWork()
fmt.Print("Worked ") color.Green("Worked " + work.Projects[i].Streaks[j].Duration.String() + " in the project " + work.Projects[i].Name)
fmt.Print(work.Projects[i].Streaks[j].Duration)
fmt.Println(" in the project " + work.Projects[i].Name)
//stop project os.Args[2]
break 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")
}
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: default:
color.Red("no option selected") color.Red("option not exists")
os.Exit(1) os.Exit(1)
} }
} else { } else {
color.Red("no option selected") color.Red("no option selected")
fmt.Println("Can run 'help' for commands information")
fmt.Println("./wtt help")
os.Exit(1) os.Exit(1)
} }
} }

56
work.go
View File

@@ -75,7 +75,7 @@ func newProject(name string) error {
work.Projects = append(work.Projects, newProject) work.Projects = append(work.Projects, newProject)
return nil return nil
} }
func listProjects() { func listProjectsDetails() {
fmt.Println("") fmt.Println("")
fmt.Println("") fmt.Println("")
fmt.Println("Listing projects") fmt.Println("Listing projects")
@@ -94,5 +94,59 @@ func listProjects() {
fmt.Println(streak.Duration) fmt.Println(streak.Duration)
} }
fmt.Println("") 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
}

BIN
wtt

Binary file not shown.

BIN
wtt_demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 MiB