From c28fda41aa5e94a0d2dc745d95cfaa2643966541 Mon Sep 17 00:00:00 2001 From: Diegiwg Date: Wed, 26 Jun 2024 00:05:50 -0300 Subject: [PATCH] feat: add list command --- README.md | 6 ++++++ cmd/list.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ docs/index.md | 6 ++++++ docs/pt/index.md | 6 ++++++ tt.go | 8 +++++++ 5 files changed, 82 insertions(+) create mode 100644 cmd/list.go diff --git a/README.md b/README.md index 129d685..177ea5a 100755 --- a/README.md +++ b/README.md @@ -44,6 +44,10 @@ Make sure the `$GOPATH/bin` directory is added to your `$PATH` so you can run th - **Description**: When you want to see how much time has passed without finishing the record, use this command. - **Usage**: `tt show` +6. **list**: Lists all time records. + - **Description**: When you want to see all time records, use this command. + - **Usage**: `tt list [--limit: int]` + ### Example Usage ```bash @@ -57,6 +61,8 @@ tt show # See that in 10 minutes it's time to stop working tt stop # Ends today's work, and see the result on the screen of how many hours have passed +tt list +# See the previous record in the list of registered records ``` ## Contributing diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..8713b6e --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "strconv" + "time" + + "github.com/Diegiwg/cli" + "github.com/Diegiwg/tt/data" + "github.com/Diegiwg/tt/model" +) + +func totalTime(r model.Record) string { + var acc time.Duration + + count := len(r.Items) + for index, item := range r.Items { + start := item.Start + end := item.End + + if end == (time.Time{}) { + + if index != count-1 { + continue + } + + end = time.Now() + } + + acc += end.Sub(start) + } + + return "Total time: " + acc.String() +} + +func List(ctx *cli.Context) error { + table := data.ReadOrCreateRecord(ctx) + recordsInTable := len(table.Records) + + limit, err := strconv.Atoi(ctx.Flags["limit"]) + if err != nil { + limit = 10 + } + + println("List of last " + strconv.Itoa(limit) + " records total time:") + + for index := recordsInTable - 1; index >= recordsInTable-limit; index-- { + if index < 0 { + break + } + + r := table.Records[index] + println(totalTime(r)) + } + + return nil +} diff --git a/docs/index.md b/docs/index.md index 5fbf28f..c5960af 100644 --- a/docs/index.md +++ b/docs/index.md @@ -44,6 +44,10 @@ Make sure the `$GOPATH/bin` directory is added to your `$PATH` so you can run th - **Description**: When you want to see how much time has passed without finishing the record, use this command. - **Usage**: `tt show` +6. **list**: Lists all time records. + - **Description**: When you want to see all time records, use this command. + - **Usage**: `tt list [--limit: int]` + ### Example Usage ```bash @@ -57,6 +61,8 @@ tt show # See that in 10 minutes it's time to stop working tt stop # Ends today's work, and see the result on the screen of how many hours have passed +tt list +# See the previous record in the list of registered records ``` ## Contributing diff --git a/docs/pt/index.md b/docs/pt/index.md index 13b9f9e..47a7d2a 100644 --- a/docs/pt/index.md +++ b/docs/pt/index.md @@ -44,6 +44,10 @@ Certifique-se de que o diretório `$GOPATH/bin` está adicionado ao seu `$PATH` - **Descrição**: Quando você quiser ver quanto tempo passou sem finalizar o registro, use este comando. - **Uso**: `tt show` +6. **list**: Lista todos os registros de tempo. + - **Descrição**: Quando você quiser ver todos os registros de tempo, use este comando. + - **Uso**: `tt list [--limit: int]` + ### Exemplo de Uso ```bash @@ -57,6 +61,8 @@ tt show # Verifique que em 10 minutos é hora de parar de trabalhar tt stop # Termina o trabalho de hoje e vê o resultado na tela de quantas horas se passaram +tt show +# Veja o registro anterior na lista de registros cadastrados ``` ## Contribuindo diff --git a/tt.go b/tt.go index 64f5450..1d4bd86 100644 --- a/tt.go +++ b/tt.go @@ -48,6 +48,14 @@ func main() { Exec: cmd.Show, }) + app.AddCommand(&cli.Command{ + Name: "list", + Desc: "Lists all time records", + Help: "When you want to see all time records, use this command.", + Usage: "[--limit: int]", + Exec: cmd.List, + }) + err := app.Run() if err != nil { println(err.Error())