Skip to content

Commit d594dd5

Browse files
authored
feat: accpet --scope for issue title (#12)
Close #9
1 parent 6695e9c commit d594dd5

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ First, you need run `gh todo init`. It will create `todo` repo in your GitHub ac
3535

3636
Then you can use `gh todo add` to add new task. It will create an issue with today(yyyy-MM-dd) as the title in `todo` repo.
3737

38+
You can specify the issue title with `--scope`. The default is today(yyyy-MM-dd). Current support [yesterday, tommorow, week, month, year]. Any other will be used directly as issue title.
39+
3840
The `gh todo` or `gh todo list` where show todo list.
3941

4042
## Usage
@@ -49,7 +51,7 @@ gh todo home
4951
# Add todo item
5052
gh todo add [item]
5153
# Open `issue` in browser
52-
gh todo edit
54+
gh todo view
5355
# Close `issue`
5456
gh todo done
5557
# Show todo list

completion.zsh

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ _gh-todo() {
2424
init)
2525
_arguments '(--template)--template[template repo]'
2626
;;
27+
add | view | edit | done | list)
28+
_arguments '(--scope)--scope[todo scope]'
29+
;;
2730
esac
2831
;;
2932
esac

gh-todo

+62-11
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,75 @@ Actions:
1111
init Create \`todo\` repo
1212
home Open \`issues\` in browser
1313
add Add todo item
14-
edit Open \`issue\` in browser
14+
view/edit Open \`issue\` in browser
1515
done Close \`issue\`
1616
list Show todo list
1717
18+
Options:
19+
--version Show version
20+
--help Show help for command
21+
--scope TODO scope, default: today(yyyy-MM-dd).
22+
Support [yesterday, tommorow, weekly, monthly, yearly].
23+
Any other will be used directly as issue title.
1824
Examples:
1925
gh todo add abc
2026
EOF
2127
}
2228

2329
version="0.3.0"
24-
today=$(date +"%Y-%m-%d")
30+
2531
repo="$(gh config get -h github.com user)/todo"
2632

2733
if [ -n "$GH_TODO_REPO" ]; then
2834
repo="$GH_TODO_REPO"
2935
fi
3036

37+
# parse `--scope` option
38+
POSITIONAL=()
39+
while [[ $# -gt 0 ]]; do
40+
key="$1"
41+
42+
case $key in
43+
--scope)
44+
scope="$2"
45+
shift # past argument
46+
shift # past value
47+
;;
48+
*) # unknown option
49+
POSITIONAL+=("$1") # save it in an array for later
50+
shift # past argument
51+
;;
52+
esac
53+
done
54+
set -- "${POSITIONAL[@]}" # restore positional parameters
55+
56+
issue_title=$(date +"%Y-%m-%d")
57+
# get issue id by `--scope`
3158
get_issue_id() {
32-
gh issue list --search "$today" --json "number" --jq ".[0].number" --repo $repo
59+
if [ -n "$scope" ]; then
60+
case "$scope" in
61+
yesterday)
62+
issue_title=$(date -v-1d +"%Y-%m-%d")
63+
;;
64+
tommorow)
65+
issue_title=$(date -v+1d +"%Y-%m-%d")
66+
;;
67+
week)
68+
issue_title="Week: $(date -v -Mon +"%Y-%m-%d") ~ $(date -v -Mon -v+6d +"%Y-%m-%d")"
69+
;;
70+
month)
71+
issue_title="Month: $(date +"%Y-%m")"
72+
;;
73+
year)
74+
issue_title="Year: $(date +"%Y")"
75+
;;
76+
*)
77+
issue_title=$scope
78+
;;
79+
esac
80+
fi
81+
82+
gh issue list --search "$issue_title" --json "number" --jq ".[0].number" --repo $repo
3383
}
3484

3585
init() {
@@ -44,9 +94,9 @@ add() {
4494
input="$1"
4595
issue_id=$(get_issue_id)
4696
if [[ -z $issue_id ]]; then
47-
exec gh issue create --title $today --body "- [ ] $input" --repo $repo
97+
exec gh issue create --title $issue_title --body "- [ ] $input" --repo $repo
4898
else
49-
body=$(gh issue list --search "$today" --json "body" --jq ".[0].body" --repo $repo)
99+
body=$(gh issue list --search "$issue_title" --json "body" --jq ".[0].body" --repo $repo)
50100
body=$(echo -e "$body\n- [ ] $input")
51101
exec gh issue edit $issue_id --body "$body" --repo $repo
52102
fi
@@ -55,15 +105,15 @@ add() {
55105
edit() {
56106
issue_id=$(get_issue_id)
57107
if [[ -z $issue_id ]]; then
58-
echo "No plans for today yet."
108+
echo "No plans for $issue_title yet."
59109
fi
60110
exec gh issue view $issue_id --repo $repo --web
61111
}
62112

63113
_done() {
64114
issue_id=$(get_issue_id)
65115
if [[ -z $issue_id ]]; then
66-
echo "No plans for today yet."
116+
echo "No plans for $issue_title yet."
67117
else
68118
exec gh issue close $issue_id --repo $repo
69119
fi
@@ -72,7 +122,7 @@ _done() {
72122
list() {
73123
issue_id=$(get_issue_id)
74124
if [[ -z $issue_id ]]; then
75-
echo "No plans for today yet."
125+
echo "No plans for $issue_title yet."
76126
else
77127
exec gh issue view $issue_id --repo $repo
78128
fi
@@ -112,15 +162,16 @@ while [ $# -gt 0 ]; do
112162
fi
113163
add "$input"
114164
;;
115-
edit)
165+
view | edit)
116166
shift
117-
edit $@
167+
edit
118168
;;
119169
done)
120170
shift
121-
_done $@
171+
_done
122172
;;
123173
list)
174+
shift
124175
list
125176
;;
126177
*)

0 commit comments

Comments
 (0)