-
Notifications
You must be signed in to change notification settings - Fork 1
/
timerange.go
142 lines (131 loc) · 4.25 KB
/
timerange.go
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
// Dinkur the task time tracking utility.
// <https://github.com/dinkur/dinkur>
//
// Copyright (C) 2021 Kalle Fagerberg
// SPDX-FileCopyrightText: 2021 Kalle Fagerberg
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
package pflagutil
import (
"fmt"
"strings"
"github.com/dinkur/dinkur/pkg/timeutil"
"github.com/spf13/cobra"
)
// NewTimeRangePtr returns a pointer to a new TimeRange instance.
func NewTimeRangePtr(shorthand timeutil.TimeSpanShorthand) *TimeRange {
return (*TimeRange)(&shorthand)
}
// TimeRange is a pflag.Value-compatible type for allowing time span shorthand
// enumeration to be used in flags.
type TimeRange timeutil.TimeSpanShorthand
// String returns the string representation of the time range.
func (r *TimeRange) String() string {
if r == nil {
return ""
}
switch timeutil.TimeSpanShorthand(*r) {
case timeutil.TimeSpanNone:
return "all"
case timeutil.TimeSpanPast:
return "past"
case timeutil.TimeSpanFuture:
return "future"
case timeutil.TimeSpanThisDay:
return "today"
case timeutil.TimeSpanThisWeek:
return "week"
case timeutil.TimeSpanPrevDay:
return "yesterday"
case timeutil.TimeSpanPrevWeek:
return "lastweek"
case timeutil.TimeSpanNextDay:
return "tomorrow"
case timeutil.TimeSpanNextWeek:
return "nextweek"
default:
return ""
}
}
// Set attempts to parse the string as a timeutil.TimeSpanShorthand and updates
// its internal state on success, or returns a parsing error if it fails.
func (r *TimeRange) Set(s string) error {
parsed, ok := parseShorthand(s)
if !ok {
return fmt.Errorf("unknown time range: %q", s)
}
*r = TimeRange(parsed)
return nil
}
// Type returns "range", the flag type name to be used in helper text.
func (r *TimeRange) Type() string {
return "range"
}
// TimeSpanShorthand returns the underlying timeutil value.
func (r *TimeRange) TimeSpanShorthand() timeutil.TimeSpanShorthand {
if r == nil {
return timeutil.TimeSpanNone
}
return timeutil.TimeSpanShorthand(*r)
}
func parseShorthand(s string) (timeutil.TimeSpanShorthand, bool) {
switch strings.ToLower(s) {
case "all", "a":
return timeutil.TimeSpanNone, true
case "past", "p":
return timeutil.TimeSpanPast, true
case "future", "f":
return timeutil.TimeSpanFuture, true
case "today", "t":
return timeutil.TimeSpanThisDay, true
case "week", "w":
return timeutil.TimeSpanThisWeek, true
case "yesterday", "y", "ld":
return timeutil.TimeSpanPrevDay, true
case "lastweek", "lw":
return timeutil.TimeSpanPrevWeek, true
case "tomorrow", "nd":
return timeutil.TimeSpanNextDay, true
case "nextweek", "nw":
return timeutil.TimeSpanNextWeek, true
default:
return timeutil.TimeSpanNone, false
}
}
// TimeRangeCompletion is a completion function for the TimeRange flag type, and
// is meant to be registered into a Cobra command object.
func TimeRangeCompletion(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return []string{
"all\tlist all entries, i.e. no baseline",
"a\tlist all entries, i.e. no baseline",
"past\tlist all entries before now",
"p\tlist all entries before now",
"future\tlist all entries since now",
"f\tlist all entries since now",
"today\tlist today's entries (default)",
"t\tlist today's entries (default)",
"week\tlist this week's entries",
"w\tlist this week's entries",
"yesterday\tlist yesterday's entries",
"y\tlist yesterday's entries",
"ld\tlist yesterday's entries",
"lastweek\tlist last week's entries",
"lw\tlist last week's entries",
"tomorrow\tlist tomorrow's entries",
"nd\tlist tomorrow's entries",
"nextweek\tlist next week's entries",
"nw\tlist next week's entries",
}, cobra.ShellCompDirectiveDefault
}