-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-look
executable file
·1285 lines (1208 loc) · 90.1 KB
/
gh-look
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
# https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# <------------------- TODOs -------------------- >
# TODO: the statement [[...]] && ... || ... is a typical bash pitfall case, if the command after && fails the command after || gets executed as well (it should not); use a simple if ... else ... statement
# https://mywiki.wooledge.org/BashPitfalls#cmd1_.26.26_cmd2_.7C.7C_cmd3
# TODO: When making a emoji reaction let the user choose from a list of valid emojis, currently it only doees on start up
# TODO: If there is a merge event on a PR, don't display the "close" event as well https://pkg.go.dev/text/template
# TODO: sometimes when searching for a popular repo like "vscode, tsx,..." it does not give the result on the first try and you have to reload with "ctrl+r". INVESTIGATE, why this is!
# TODO: disable comments that are marked as spam, use "isMinimized" or "minimizedReason"
# TODO: find a better way to manage the help section for each command, currently too much duplication.
# TODO: run tests on the script - https://github.com/dodie/testing-in-bash
# TODO: allow the user to set their hotkeys in a config file
# TODO: when the user sets the "-o" flag, let the user choose from a list of valid sorting options with fzf
# TODO: add a hotkey to allow switching between page results, page 1, page 2, ... page n
# TODO: toggle the reaction with a single hotkey, first check if there has been an reaction with the current selected emoji, if not make a reaction, if there has been undo the reaction
# TODO: add option to subscribe to a specific issue/PR, must visulaize that I am subscribed to it, check GraphQL API 'updateSubscription' (mutation)/ 'viewerSubscription'(query).
# TODO: adopt shell completion, for example: https://github.com/molovo/revolver/blob/master/revolver.zsh-completion
# TODO: use 'export -f' to make functions available in child processes, or use "set -a" https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# <------------------- DOCUMENTATION -------------------- >
# writing GitHub GraphQL queries
# https://docs.github.com/en/graphql
# modify GO templates
# https://pkg.go.dev/text/template
# <------------------- VARIABLES -------------------- >
SORTING_ORDER=""
OWNER_REPO_NAME=""
USER_NAME=""
PREVIEW_WINDOW_VISIBILITY="hidden"
INITIAL_QUERY=""
INITIAL_COMMENT_QUERY=""
OUTPUT_SELECTION=""
# resetting the '-F' option to its default setting with '-+F'
LANG_BAT_SETUP="bat --pager 'less --clear-screen -+F -+X -R' --paging always --color always --style plain --language"
# for better speed
CACHE_TIME="20s"
# minimum required fzf version, using "become(...)"
# https://github.com/junegunn/fzf/releases
MIN_FZF_VERSION="0.38.0"
# workflow runs
UPDATE_TIME=10
EXCLUDE_PULL_REQUESTS=true
NUMBER_WORKFLOW_RUN_LIST=10
# colors
COLOR_RESET=$(tput sgr0)
RED_NORMAL=$(tput setaf 1)
GREEN_NORMAL=$(tput setaf 2)
YELLOW_NORMAL=$(tput setaf 3)
BLUE_NORMAL=$(tput setaf 4)
WHITE_BOLD=$(
tput bold
tput setaf 7
)
# emojis
REACTION_EMOJI="THUMBS_UP"
valid_emojis=("👍:THUMBS_UP" "👎:THUMBS_DOWN" "😄:LAUGH" "🎉:HOORAY" "😕:CONFUSED" "💖:HEART" "🚀:ROCKET" "👀:EYES")
# GH_TOGGLE_EMOJI_REACTION=$'gh api graphql --raw-field node_id={1} --raw-field query=\'query ($node_id: ID!) { node(id: $node_id) { ... on Reactable { reactionGroups { viewerHasReacted content }}}}\' --jq \'.data.node.reactionGroups[] | select(.viewerHasReacted) | .content\''" | while IFS= read -r line; do if grep -q \"$line\" <<<\"$REACTION_EMOJI\"; then echo true && break; else echo false; fi; done | tail -1"
GH_ADD_REACTION=$'gh api graphql --silent --raw-field query=\'mutation($id: ID! $emoji: ReactionContent! ) { addReaction(input: {subjectId: $id content: $emoji }) { clientMutationId }}\''
GH_REMOVE_REACTION=$'gh api graphql --silent --raw-field query=\'mutation($id: ID! $emoji: ReactionContent! ) { removeReaction(input: {subjectId: $id content: $emoji }) { clientMutationId }}\''
# <--------------- HELPER FUNCTIONS ---------------- >
_die_with_octocat() {
GH_PAGER="cat" gh api octocat
printf "%s[%s] < ERROR > %s%s" "$RED_NORMAL" "$(date +"%H:%M:%S")" "${1:?"No error message has been defined."}" "$COLOR_RESET"
printf "\n%s\n" "${2-}"
exit 1
}
# for comparing multi-digit version numbers https://apple.stackexchange.com/a/123408/11374
_version_number() {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
}
# Here options are defined that are common to all commands.
# https://github.com/junegunn/fzf-git.sh/blob/main/fzf-git.sh#L104
_fzf_basic_options() {
# Hint: tablerow truncates the table columns to fit the entire table in the current terminal window.
# The workaround to avoid the truncation is to force a really wide terminal with GH_FORCE_TTY=10000.
# IMPORTANT: anything after "$@" will overwrite options in the actual command
SHELL="bash" GH_FORCE_TTY=10000 fzf -- \
--header '' \
--delimiter '\s+' \
--color 'header:regular' \
"$@" \
--ansi --layout reverse --info inline --no-multi --height 100% \
--ellipsis '' --border horizontal \
--no-separator --print-query \
--preview-window "$PREVIEW_WINDOW_VISIBILITY:wrap:right:65%:border-left" \
--bind 'btab:change-preview-window(50%:nohidden|65%:down:nohidden:border-top|nohidden)'
}
# More ideas: https://raw.githubusercontent.com/sindresorhus/cli-spinners/master/spinners.json
SPIN_FORM=(▰▱▱▱▱▱▱ ▰▰▱▱▱▱▱ ▰▰▰▱▱▱▱ ▰▰▰▰▱▱▱ ▰▰▰▰▰▱▱ ▰▰▰▰▰▰▱ ▰▰▰▰▰▰▰)
# Restarting fzf immediately may not show the newly created changes. Wait at least a second or two.
_load_indicator() {
# Save the current terminal state
tput smcup
# Save cursor position
tput sc
for i in "${SPIN_FORM[@]}"; do
# Restore cursor position to the saved position
tput rc
printf "%s %s" "${1:-"Restarting"}" "$i"
sleep "${2-"0.2"}"
done
# Restore the terminal state
tput rmcup
}
# confirm user decisions and return an exit status based on the user's response
_user_confirm() {
local confirm
tput smcup
printf "%s\n\n${WHITE_BOLD}CONFIRM${COLOR_RESET} %s (${GREEN_NORMAL}y${COLOR_RESET}/${RED_NORMAL}n${COLOR_RESET})?\n> " "${1:-"<Missing Context Message >"}" "${2-"<Missing Confirmation Context >"}"
stty raw -echo
confirm=$(dd bs=1 count=1 2>/dev/null)
stty sane
tput rmcup
[[ $confirm == "y" ]] && return 0 || return 1
}
# return the emoji from its corresponding string name
_emoji_from_name() {
for emoji in "${valid_emojis[@]}"; do
if [[ ${emoji#*:} == "$1" ]]; then
echo "${emoji%%:*}"
break
fi
done
}
# choose a valid emoji from a list
_emoji_picker() {
for emoji in "${valid_emojis[@]}"; do
echo "${emoji%%:*} ${emoji#*:}"
done |
_fzf_basic_options \
--header "Pick an emoji and hit enter" \
--bind 'enter:become:echo {2}' \
--delimiter ' '
}
# <--------------- FUNCTIONS ---------------- >
help_general_function() {
cat <<EOF
${WHITE_BOLD}Usage${COLOR_RESET}
gh look [Command] [Flags] [Search term]
${WHITE_BOLD}Commands${COLOR_RESET}
${GREEN_NORMAL} i, issue ${COLOR_RESET} List Issues
${GREEN_NORMAL} p, pr ${COLOR_RESET} List Pull Requests
${GREEN_NORMAL} r, run ${COLOR_RESET} List Workflow Runs
${GREEN_NORMAL} s, search${COLOR_RESET} Search for GitHub Repositories
${GREEN_NORMAL}st, star ${COLOR_RESET} List Starred Repositories
For a listing of options, use gh look [Command] --help
EOF
}
help_issue_function() {
cat <<EOF
${WHITE_BOLD}Usage${COLOR_RESET}
gh look [ i | issue ] [Flags] [Search term]
${WHITE_BOLD}Flags${COLOR_RESET}
${GREEN_NORMAL}<none>${COLOR_RESET} List Issues from current directory
${GREEN_NORMAL}-c ${COLOR_RESET} Cache the response, for example "30s", "15m", "1h" (default: 20s)
${GREEN_NORMAL}-e ${COLOR_RESET} Emoji to make a reaction (default: 👍)
${GREEN_NORMAL}-h ${COLOR_RESET} Help
${GREEN_NORMAL}-o ${COLOR_RESET} sorting order of Issues (default: created-desc)
${GREEN_NORMAL}-r ${COLOR_RESET} Specify a repository (form: OWNER/REPO)
${GREEN_NORMAL}-w ${COLOR_RESET} Display the preview window upon start (default: hidden)
${WHITE_BOLD}Symbols${COLOR_RESET}
💬 Total number of comments (gray commentable; red locked)
📣 Total number of emojis (green reactable; yellow reacted; red locked)
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}enter ${COLOR_RESET} See comments
${GREEN_NORMAL}tab ${COLOR_RESET} Toggle preview
${GREEN_NORMAL}shift+tab ${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+a ${COLOR_RESET} ALL Issues
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+e ${COLOR_RESET} Edit an Issue
${GREEN_NORMAL}ctrl+f ${COLOR_RESET} Fuzzy search
${GREEN_NORMAL}ctrl+g ${COLOR_RESET} Close/Reopen Issue after confirmation
${GREEN_NORMAL}ctrl+n ${COLOR_RESET} New Issue
${GREEN_NORMAL}ctrl+o ${COLOR_RESET} OPEN Issues only
${GREEN_NORMAL}ctrl+p ${COLOR_RESET} Put "involves:@me" into the search
${GREEN_NORMAL}ctrl+r ${COLOR_RESET} Reload
${GREEN_NORMAL}ctrl+t ${COLOR_RESET} React with $(_emoji_from_name "$REACTION_EMOJI") emoji
${GREEN_NORMAL}ctrl+u ${COLOR_RESET} Undo the $(_emoji_from_name "$REACTION_EMOJI") emoji reaction
${GREEN_NORMAL}ctrl+x ${COLOR_RESET} Write a comment
${GREEN_NORMAL}shift+left ${COLOR_RESET} Switch to Search
${GREEN_NORMAL}shift+right${COLOR_RESET} Switch to Pull Requests
${GREEN_NORMAL}esc ${COLOR_RESET} Quit
EOF
}
fzf_issue_function() {
GH_ISSUE_PREVIEW=$'gh api graphql --paginate --raw-field id={1} --raw-field query=\'query($id: ID!, $endCursor: String){node(id: $id) { ... on Issue { author { login } body createdAt comments(first: 100) { totalCount nodes { author { login } body createdAt viewerDidAuthor reactionGroups { content reactors(last: 4) { totalCount nodes { ... on Actor { login }}}}}} timelineItems(itemTypes: [CLOSED_EVENT, ISSUE_COMMENT, REOPENED_EVENT], first: 100, after: $endCursor) { nodes { ... on ClosedEvent { actor { login } closer { ... on PullRequest { baseRepository { nameWithOwner } number }} createdAt stateReason } ... on IssueComment { author { login } body createdAt viewerDidAuthor reactionGroups { content reactors(last: 4) { totalCount nodes { ... on Actor { login }}}} reactionGroups { content viewerHasReacted reactors(last: 4) { nodes { ... on Actor { login }} totalCount }}} ... on ReopenedEvent { actor { login } createdAt stateReason }} pageInfo { hasNextPage endCursor }} labels(first:10) { nodes { name } } number reactionGroups { content viewerHasReacted reactors(last: 4) { nodes { ... on Actor { login }} totalCount }} state title viewerDidAuthor }}}\' --template \'{{- $prefix := .data.node -}}{{- $stateIssueColor := "green+b" -}}{{- if eq $prefix.state "CLOSED" -}}{{- $stateIssueColor = "93+b" -}}{{- end -}}{{- $issueAuthor := "[DELETED_USER]" -}}{{- if $prefix.author -}}{{- $issueAuthor = $prefix.author.login -}}{{- end -}}{{- $authorColor := "cyan+hb" -}}{{- if $prefix.viewerDidAuthor -}}{{- $authorColor = "yellow+bh" -}}{{- end -}}{{- tablerow ($prefix.title | color "white+bh") -}}{{- tablerender -}}{{- tablerow (printf "%s ◉ #%.0f" $prefix.state $prefix.number | color $stateIssueColor) ($issueAuthor | color $authorColor) (printf "│ %s ∙ %.0f Comments │" (timeago $prefix.createdAt) $prefix.comments.totalCount | color "gray+h") -}}{{- tablerender -}}{{- if $prefix.labels.nodes -}}{{- tablerow ("Labels:" | color "white+b") (pluck "name" $prefix.labels.nodes | join ", " | printf "[ %s ]" | color "white") -}}{{- tablerender -}}{{- end -}}{{- range $prefix.reactionGroups -}}{{ $emoji := .content }}{{ if eq .content "CONFUSED" }}{{ $emoji = "😕" }}{{ else if eq .content "EYES" }}{{ $emoji = "👀" }}{{ else if eq .content "HEART" }}{{ $emoji = "💖" }}{{ else if eq .content "HOORAY" }}{{ $emoji = "🎉" }}{{ else if eq .content "LAUGH" }}{{ $emoji = "😄" }}{{ else if eq .content "THUMBS_DOWN" }}{{ $emoji = "👎" }}{{ else if eq .content "THUMBS_UP" }}{{ $emoji = "👍" }}{{ else if eq .content "ROCKET" }}{{ $emoji = "🚀" }}{{ end }}{{ if gt .reactors.totalCount 0.0 }}{{ $emojiGiver := pluck "login" .reactors.nodes | join ", " | color "green+d" }}{{ if gt (pluck "login" .reactors.nodes | len ) 3 }}{{- /* NOTE: number of names limited */ -}}{{ $emojiGiver = slice (pluck "login" .reactors.nodes) 1 | join ", " | printf "%s, ..." | color "green+d" }}{{end}}{{- $emojiTotal := .reactors.totalCount | color "green+bd" -}}{{- if .viewerHasReacted -}}{{- $emojiTotal = .reactors.totalCount | color "yellow+bh" -}}{{- end -}}{{- tablerow (printf "%s %s" $emojiTotal $emoji) $emojiGiver -}}{{- tablerender -}}{{- end -}}{{- end }}{{- tablerow ("——————————————————————————————————————————————————————————"| color "gray+d") -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- $bodyText := $prefix.body }}{{ if eq ($bodyText | len) 0 -}}{{- $bodyText = " No description provided. " | color "white+i" -}}{{- end -}}{{- $bodyText -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- range $prefix.timelineItems.nodes -}}{{- $actorTimeline := "[DELETED_USER]" -}}{{- if .actor -}}{{- $actorTimeline = .actor.login -}}{{- end -}}{{- $authorTimelineColor := "cyan+hb" -}}{{- if .viewerDidAuthor -}}{{- $authorTimelineColor = "yellow+bh" -}}{{- end -}}{{- if eq .stateReason "COMPLETED" "NOT_PLANNED" -}}{{- $stateClosedColor := "93+b" -}}{{- if eq .stateReason "NOT_PLANNED" -}}{{- $stateClosedColor = "gray+hb" -}}{{- end -}}{{- $linkedPR := "" -}}{{- if .closer -}}{{- $linkedPR = (printf "%s #%v" .closer.baseRepository.nameWithOwner .closer.number | color "blue+hb") -}}{{- end -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color $stateClosedColor) -}}{{- tablerender -}}{{- tablerow (printf "%s closed this as %s %s %s" ($actorTimeline | color $authorTimelineColor) (.stateReason | color $stateClosedColor) (timeago .createdAt | color "white+bh") $linkedPR) -}}{{- tablerender -}}{{- tablerow ("◢■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◣"| color $stateClosedColor) -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- else if eq .stateReason "REOPENED" -}}{{- $stateReopenedColor := "green+b" -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color $stateReopenedColor) -}}{{- tablerender -}}{{- tablerow (printf "%s %s this Issue %s" ($actorTimeline | color $authorTimelineColor) (.stateReason | color $stateReopenedColor) (timeago .createdAt | color "white+bh")) -}}{{- tablerender -}}{{- tablerow ("◢■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◣"| color $stateReopenedColor) -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- else -}}{{- $authorTimeline := "[DELETED_USER]" -}}{{- if .author -}}{{- $authorTimeline = .author.login -}}{{- end -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color "gray+d") -}}{{- tablerender -}}{{- tablerow ($authorTimeline | color $authorTimelineColor) ( timeago .createdAt | color "white+bh") -}}{{- tablerender -}}{{- range .reactionGroups -}}{{ $emojiInComment := .content }}{{ if eq .content "CONFUSED" }}{{ $emojiInComment = "😕" }}{{ else if eq .content "EYES" }}{{ $emojiInComment = "👀" }}{{ else if eq .content "HEART" }}{{ $emojiInComment = "💖" }}{{ else if eq .content "HOORAY" }}{{ $emojiInComment = "🎉" }}{{ else if eq .content "LAUGH" }}{{ $emojiInComment = "😄" }}{{ else if eq .content "THUMBS_DOWN" }}{{ $emojiInComment = "👎" }}{{ else if eq .content "THUMBS_UP" }}{{ $emojiInComment = "👍" }}{{ else if eq .content "ROCKET" }}{{ $emojiInComment = "🚀" }}{{ end }}{{ if gt .reactors.totalCount 0.0 }}{{ $emojiInCommentGiver := pluck "login" .reactors.nodes | join ", " | color "green+d" }}{{ if gt (pluck "login" .reactors.nodes | len ) 3 }}{{- /* NOTE: number of names limited */ -}}{{ $emojiInCommentGiver = slice (pluck "login" .reactors.nodes) 1 | join ", " | printf "%s, ..." | color "green+d" }}{{end}}{{- $emojiInCommentTotal := .reactors.totalCount | color "green+bd" -}}{{- if .viewerHasReacted -}}{{- $emojiInCommentTotal = .reactors.totalCount | color "yellow+bh" -}}{{- end -}}{{- tablerow (printf "%s %s" $emojiInCommentTotal $emojiInComment) $emojiInCommentGiver -}}{{- tablerender -}}{{- end -}}{{- end -}}{{- tablerow ("‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥"| color "gray+d") -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- .body -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- end -}}{{- end -}}\''
# fzf allows hiding columns "--with-nth 2...", headers are also affected, so NODE_ID_HIDDEN is included there
GH_ISSUE_COMMAND=$'gh api graphql --raw-field query=\'query($filter: String!) {search(query: $filter, type: ISSUE, first: 35) {issueCount nodes { ... on Issue { author { login } comments {totalCount} id number updatedAt reactions { totalCount viewerHasReacted } state title viewerCanReact viewerDidAuthor }}}}\' --template \'
{{- $limitCount := .data.search.issueCount -}}
{{- if gt .data.search.issueCount 35.0 -}}{{- $limitCount = 35.0 -}}{{- end -}}
{{- tablerow "NODE_ID_HIDDEN" (printf "%.0f of ∑ %.0f" $limitCount .data.search.issueCount | color "blue+b") ("|" | color "white+d") ("? Help · esc Quit" | color "blue") -}}{{- tablerender -}}
{{- tablerow "" -}}{{- tablerender -}}
{{- $headerColor := "blue+b" -}}
{{- tablerow "NODE_ID_HIDDEN" ("ISSUE" | color $headerColor) ("AUTHOR" | color $headerColor) ("LAST UPDATE" | color $headerColor) "💬" "📣" ("TITLE" | color $headerColor) -}}
{{- range .data.search.nodes -}}
{{- $stateIssueColor := "green" -}}
{{- if eq .state "CLOSED" -}}{{- $stateIssueColor = "93" -}}{{- end -}}
{{- $author := "[DELETED_USER]" -}}
{{- if .author -}}{{- $author = .author.login -}}{{- end -}}
{{- $authorColor := "cyan+h" -}}
{{- if .viewerDidAuthor -}}{{- $authorColor = "yellow" -}}{{- end -}}
{{- $commentCount := "\u00a0" -}}{{- /* NOTE: U+00a0 (non-breaking space) for entries without comments, which the viewer can comment on. Needed to make a if decision with fzf later in the script. */ -}}
{{- if not .viewerCanReact -}}{{- $commentCount = (or (printf "%.0f" .comments.totalCount) "0") -}}
{{- else if .comments.totalCount -}}{{- $commentCount = printf "%.0f" .comments.totalCount -}}
{{- end -}}
{{- $commentColor := "white+bd" -}}
{{- if not .viewerCanReact -}}{{- $commentColor = "red+bd" -}}{{- end -}}
{{- $emojiCount := "" -}}
{{- if not .viewerCanReact -}}{{- $emojiCount = (or (printf "%.0f" .reactions.totalCount) "0") -}}
{{- else if .reactions.totalCount -}}{{- $emojiCount = printf "%.0f" .reactions.totalCount -}}
{{- end -}}
{{- $emojiColor := "green+bd" -}}
{{- if not .viewerCanReact -}}{{- $emojiColor = "red+bd" -}}
{{- else if .reactions.viewerHasReacted -}}{{- $emojiColor = "yellow+b" -}}
{{- end -}}
{{- tablerow .id (printf "#%.0f" .number | color $stateIssueColor) ($author | color $authorColor) (timeago .updatedAt) ($commentCount | color $commentColor) ($emojiCount | color $emojiColor) .title -}}
{{- end -}}\''
INITIAL_ISSUE_PROMPT=$(grep -q A <"$SCRATCH_FILE" && echo "Issues ◉ [$OWNER_REPO_NAME] > " || echo "${GREEN_NORMAL}OPEN Issues ◉ [$OWNER_REPO_NAME] > ")
OUTPUT_SELECTION=$(FZF_DEFAULT_COMMAND="(grep -q A <$SCRATCH_FILE && $GH_ISSUE_COMMAND --cache ${1:-$CACHE_TIME} --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME $INITIAL_QUERY \" || $GH_ISSUE_COMMAND --cache ${1:-$CACHE_TIME} --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME state:open $INITIAL_QUERY \") || true" \
_fzf_basic_options --disabled --header-lines 3 --with-nth 2.. \
--prompt "$INITIAL_ISSUE_PROMPT" \
--query "$INITIAL_QUERY" --preview "sleep 0.35; $GH_ISSUE_PREVIEW | $LANG_BAT_SETUP md" \
--bind "change:first+reload-sync:sleep 0.25; (grep -q A <$SCRATCH_FILE && $GH_ISSUE_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME \"{q} || $GH_ISSUE_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--bind "?:toggle-preview+change-preview:echo '$(help_issue_function)'" \
--bind "tab:toggle-preview+change-preview:sleep 0.35; $GH_ISSUE_PREVIEW | $LANG_BAT_SETUP md" \
--bind "ctrl-a:rebind(change)+change-prompt(Issues ◉ [$OWNER_REPO_NAME] > )+disable-search+execute-silent(echo A >$SCRATCH_FILE)+reload-sync:$GH_ISSUE_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME \"{q} || true" \
--bind "ctrl-b:execute-silent(gh issue view {2} --web --repo $OWNER_REPO_NAME)" \
--bind "ctrl-f:unbind(change)+enable-search+clear-query+change-prompt(${RED_NORMAL}FUZZY SEARCH [$OWNER_REPO_NAME] > )" \
--bind "ctrl-o:rebind(change)+change-prompt(${GREEN_NORMAL}OPEN Issues ◉ [$OWNER_REPO_NAME] > )+disable-search+execute-silent(echo B >$SCRATCH_FILE)+reload-sync:$GH_ISSUE_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME state:open \"{q} || true" \
--bind "ctrl-p:put(involves:@me)" \
--bind "ctrl-r:reload-sync:(grep -q A <$SCRATCH_FILE && $GH_ISSUE_COMMAND --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME \"{q} || $GH_ISSUE_COMMAND --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--bind "ctrl-t:execute-silent($GH_ADD_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:(grep -q A <$SCRATCH_FILE && $GH_ISSUE_COMMAND --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME \"{q} || $GH_ISSUE_COMMAND --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--bind "ctrl-u:execute-silent($GH_REMOVE_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:(grep -q A <$SCRATCH_FILE && $GH_ISSUE_COMMAND --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME \"{q} || $GH_ISSUE_COMMAND --raw-field filter=\"$SORTING_ORDER type:issue repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--expect "ctrl-e,ctrl-g,ctrl-n,ctrl-x,enter,shift-left,shift-right") || true
printed_query="$(sed 1q <<<"$OUTPUT_SELECTION")"
expected_key="$(sed '1d;3d' <<<"$OUTPUT_SELECTION")"
TICKET_NUMBER="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $2}' | tr -d "#")"
output_textline="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{$1=""; sub("^ *",""); print $0}')"
output_id="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $1}')"
number_comments="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $6,$7}' | grep -Eo '[0-9]+' || true)"
# reset the variable
OUTPUT_SELECTION=""
case "$expected_key" in
ctrl-e)
gh issue edit "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_QUERY="$printed_query"
fzf_issue_function 0s
;;
ctrl-g)
check_info_api=()
# "[0]" is the ticket closed ?
# "[1]" can the viewer close it ?
while IFS='' read -r line; do check_info_api+=("$line"); done < <(
gh api graphql --raw-field id="$output_id" \
--raw-field query=$'query ($id: ID!) { node(id: $id) { ... on Issue { closed viewerCanClose }}}' \
--jq '.data.node | .closed,.viewerCanClose'
)
if [ "${check_info_api[0]}" = "true" ]; then
if [ "${check_info_api[1]}" = "true" ]; then
if _user_confirm "$output_textline" "reopen this issue"; then
if _user_confirm "$output_textline" "do you want to add a comment"; then
tput sc
echo -en "${WHITE_BOLD}Enter a comment!${COLOR_RESET}\n> "
read -r message
tput rc && tput el
gh issue reopen "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" --comment "$message" &>/dev/null
else
gh issue reopen "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" &>/dev/null
fi
_load_indicator $'🟢 Success.... Issue reopened!\nRestarting'
else
_load_indicator $'Discarding... Issue was not reopened!\nRestarting'
fi
else
_load_indicator $'🛑 Missing permissions to reopen this Issue!\nRestarting'
fi
else
if [ "${check_info_api[1]}" = "true" ]; then
if _user_confirm "$output_textline" "close this issue"; then
if _user_confirm "$output_textline" "do you want to add a comment"; then
tput sc
echo -en "${WHITE_BOLD}Enter a comment!${COLOR_RESET}\n> "
read -r message
tput rc && tput el
gh issue close "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" --comment "$message" &>/dev/null
else
gh issue close "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" &>/dev/null
fi
_load_indicator $'🟢 Success.... Issue closed!\nRestarting'
else
_load_indicator $'Discarding... Issue was not closed!\nRestarting'
fi
else
_load_indicator $'🛑 Missing permissions to close this Issue!\nRestarting'
fi
fi
INITIAL_QUERY="$printed_query"
fzf_issue_function 0s
;;
ctrl-n)
gh issue create --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_QUERY="$printed_query"
fzf_issue_function 0s
;;
ctrl-x)
gh issue comment "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_QUERY="$printed_query"
fzf_issue_function 0s
;;
enter)
INITIAL_QUERY="$printed_query"
if [ -z "$number_comments" ]; then
# adding U+00a0 (non-breaking space) for issues with zero comments and commentable
# without it an issue with 0 comments and >1 reaction would trick the if check
# sometimes the "age" column can only be 2 words instead of 3, e.g. "just now", that is why $6 and $7
_load_indicator $'No comments on this issue\nRestarting'
fzf_issue_function 0s
else
fzf_issue_comment_function
fi
;;
shift-left)
INITIAL_QUERY=""
fzf_search_function
;;
shift-right)
INITIAL_QUERY=""
fzf_pr_function
;;
esac
}
help_issue_comment_function() {
cat <<EOF
${WHITE_BOLD}Symbols${COLOR_RESET}
📣 Total number of emojis (green reactable; yellow reacted; red locked)
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}tab ${COLOR_RESET} Toggle preview
${GREEN_NORMAL}shift+tab${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+g ${COLOR_RESET} Write a comment
${GREEN_NORMAL}ctrl+n ${COLOR_RESET} New Issue
${GREEN_NORMAL}ctrl+t ${COLOR_RESET} React with $(_emoji_from_name "$REACTION_EMOJI") emoji
${GREEN_NORMAL}ctrl+u ${COLOR_RESET} Undo the $(_emoji_from_name "$REACTION_EMOJI") emoji reaction
${GREEN_NORMAL}ctrl+x ${COLOR_RESET} Delete the comment after confirmation
${GREEN_NORMAL}esc ${COLOR_RESET} Return to Issue list
EOF
}
# Get the name and date of the commenter, convert the emojis to real emojis, add the total number next to it. Highlight if the viewer has used it.
# Get the last 4 emoji reactor names for each group,
# based on the length of the array of reactor names, either print or slice 1 and add a ", ..." as an indicator of more than three
# End the list with the commenter's actual message.
GH_COMMENTS_PREVIEW=$'gh api graphql --raw-field id={1} --raw-field query=\'query($id: ID! ){node(id: $id) {... on Comment { author{login} body createdAt viewerDidAuthor} ... on Reactable { reactionGroups { content viewerHasReacted reactors(last: 4) { nodes {... on Actor { login }} totalCount }}}}}\' --template \'{{- $prefix := .data.node -}}{{- $author := "[DELETED_USER]" -}}{{- if $prefix.author -}}{{- $author = $prefix.author.login -}}{{- end -}}{{- $authorColor := "cyan+hb" -}}{{- if $prefix.viewerDidAuthor -}}{{- $authorColor = "yellow+bh" -}}{{- end -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color "gray+d") -}}{{- tablerender -}}{{- tablerow ($author | color $authorColor) (timeago $prefix.createdAt | color "white+bh") -}}{{- tablerender -}}{{- range $prefix.reactionGroups -}}{{ $emoji := .content }}{{ if eq .content "CONFUSED" }}{{ $emoji = "😕" }}{{ else if eq .content "EYES" }}{{ $emoji = "👀" }}{{ else if eq .content "HEART" }}{{ $emoji = "💖" }}{{ else if eq .content "HOORAY" }}{{ $emoji = "🎉" }}{{ else if eq .content "LAUGH" }}{{ $emoji = "😄" }}{{ else if eq .content "THUMBS_DOWN" }}{{ $emoji = "👎" }}{{ else if eq .content "THUMBS_UP" }}{{ $emoji = "👍" }}{{ else if eq .content "ROCKET" }}{{ $emoji = "🚀" }}{{ end }}{{ if gt .reactors.totalCount 0.0 }}{{ $emojiGiver := pluck "login" .reactors.nodes | join ", " | color "green+d" }}{{ if gt (pluck "login" .reactors.nodes | len ) 3 }}{{- /* NOTE: number of names limited */ -}}{{ $emojiGiver = slice (pluck "login" .reactors.nodes) 1 | join ", " | printf "%s, ..." | color "green+d" }}{{end}}{{- $emojiTotal := .reactors.totalCount | color "green+bd" -}}{{- if .viewerHasReacted -}}{{- $emojiTotal = .reactors.totalCount | color "yellow+bh" -}}{{- end -}}{{- tablerow (printf "%s %s" $emojiTotal $emoji) $emojiGiver -}}{{- tablerender -}}{{- end -}}{{- end }}{{- tablerow ("‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥"| color "gray+d") -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- $prefix.body -}}\''
fzf_issue_comment_function() {
GH_ISSUE_COMMENTS_COMMAND=$'gh api graphql --paginate --raw-field query=\'query ($owner: String!, $name: String!, $number: Int!, $endCursor: String) { repository(owner: $owner, name: $name) { issue(number: $number) { author { login } createdAt labels(first:10) { nodes { name } } number state title comments(first: 100, after: $endCursor) { totalCount nodes { author { login } body createdAt databaseId id reactions { totalCount viewerHasReacted } viewerCanReact viewerDidAuthor } pageInfo { endCursor hasNextPage }}}}}\' --template \'
{{- $prefix := .data.repository.issue -}}
{{- $stateIssueCommentColor := "green" -}}
{{- if eq $prefix.state "CLOSED" -}}{{- $stateIssueCommentColor = "93" -}}{{- end -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ($prefix.title | color "white+bh") -}}{{- tablerender -}}
{{- $issueAuthor := "[DELETED_USER]" -}}
{{- if $prefix.author -}}{{- $issueAuthor = $prefix.author.login -}}{{- end -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" (printf "%s ◉ #%.0f" $prefix.state $prefix.number | color $stateIssueCommentColor) ($issueAuthor | color "cyan+hb") (printf "│ %s ∙ %.0f Comments │" (timeago $prefix.createdAt) $prefix.comments.totalCount | color "gray+h") ("? Help · esc Quit" | color "blue") -}}{{- tablerender -}}
{{- if $prefix.labels.nodes -}}{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ("Labels:" | color "white+b") (pluck "name" $prefix.labels.nodes | join ", " | printf "[ %s ]" | color "white") -}}{{- else -}}{{- tablerow "" -}}{{- end -}}{{- tablerender -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ("———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————" | color "gray+d") -}}{{- tablerender -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" "" -}}{{- tablerender -}}
{{- $headerColor := "blue+b" -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ("COMMENTER" | color $headerColor) ("AGE" | color $headerColor) "📣" ("COMMENT" | color $headerColor) -}}
{{- range $prefix.comments.nodes -}}
{{- $author := "[DELETED_USER]" -}}
{{- if .author -}}{{- $author = .author.login -}}{{- end -}}
{{- $authorColor := "cyan+h" -}}
{{- if .viewerDidAuthor -}}{{- $authorColor = "yellow" -}}{{- end -}}
{{- $emojiCount := "" -}}
{{- if not .viewerCanReact -}}{{- $emojiCount = (or (printf "%.0f" .reactions.totalCount) "0") -}}
{{- else if .reactions.totalCount -}}{{- $emojiCount = printf "%.0f" .reactions.totalCount -}}
{{- end -}}
{{- $emojiColor := "green+bd" -}}
{{- if not .viewerCanReact -}}{{- $emojiColor = "red+bd" -}}
{{- else if .reactions.viewerHasReacted -}}{{- $emojiColor = "yellow+b" -}}
{{- end -}}
{{- tablerow .id .databaseId ($author | color $authorColor) (timeago .createdAt) ($emojiCount | color $emojiColor) .body -}}
{{- end -}}\''
OUTPUT_SELECTION=$(FZF_DEFAULT_COMMAND="$GH_ISSUE_COMMENTS_COMMAND --cache ${1:-$CACHE_TIME} --raw-field owner=${OWNER_REPO_NAME%/*} --raw-field name=${OWNER_REPO_NAME#*/} --field number=$TICKET_NUMBER" \
_fzf_basic_options --header-lines 6 --with-nth 3.. \
--query "$INITIAL_COMMENT_QUERY" --prompt "${BLUE_NORMAL}Issue 💬 [$OWNER_REPO_NAME] > " \
--preview-window "+5:~5" --preview "$GH_COMMENTS_PREVIEW | $LANG_BAT_SETUP md" \
--bind "?:toggle-preview+change-preview:echo '$(help_issue_comment_function)'" \
--bind "tab:toggle-preview+change-preview:$GH_COMMENTS_PREVIEW | $LANG_BAT_SETUP md" \
--bind "ctrl-b:execute-silent(python -m webbrowser https://github.com/$OWNER_REPO_NAME/issues/$TICKET_NUMBER#issuecomment-{2})" \
--bind "ctrl-t:execute-silent($GH_ADD_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:$GH_ISSUE_COMMENTS_COMMAND --raw-field owner=${OWNER_REPO_NAME%/*} --raw-field name=${OWNER_REPO_NAME#*/} --field number=$TICKET_NUMBER || true" \
--bind "ctrl-u:execute-silent($GH_REMOVE_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:$GH_ISSUE_COMMENTS_COMMAND --raw-field owner=${OWNER_REPO_NAME%/*} --raw-field name=${OWNER_REPO_NAME#*/} --field number=$TICKET_NUMBER || true" \
--expect "ctrl-g,ctrl-n,ctrl-x,esc") || true
printed_query="$(sed 1q <<<"$OUTPUT_SELECTION")"
expected_key="$(sed '1d;3d' <<<"$OUTPUT_SELECTION")"
output_textline="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{$1=""; $2=""; sub("^ *",""); print $0}')"
output_id="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $1}')"
# reset the variable
OUTPUT_SELECTION=""
case "$expected_key" in
ctrl-g)
gh issue comment "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_COMMENT_QUERY="$printed_query"
fzf_issue_comment_function 0s
;;
ctrl-n)
gh issue create --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_QUERY="$printed_query"
fzf_issue_function 0s
;;
ctrl-x)
check_viewer_permission=$(
gh api graphql --raw-field id="$output_id" \
--raw-field query=$'query ($id: ID!) { node(id: $id) { ... on IssueComment { viewerCanDelete }}}' \
--jq '.data.node.viewerCanDelete'
)
if [ "$check_viewer_permission" = "true" ]; then
if _user_confirm "$output_textline" "deletion of this comment"; then
gh api graphql --silent --raw-field id="$output_id" \
--raw-field query=$'mutation($id: ID!) { deleteIssueComment(input: {id: $id}) { clientMutationId }}'
_load_indicator $'🟢 Success.... comment deleted\nRestarting'
else
_load_indicator $'Discarding... comment was not deleted!\nRestarting'
fi
else
_load_indicator $'🛑 Missing permissions to delete this comment!\nRestarting'
fi
INITIAL_COMMENT_QUERY="$printed_query"
fzf_issue_comment_function 0s
;;
esc)
INITIAL_COMMENT_QUERY=""
fzf_issue_function
;;
esac
}
###################################### PULL REQUEST (PR)
help_pr_function() {
cat <<EOF
${WHITE_BOLD}Usage${COLOR_RESET}
gh look [ p | pr ] [Flags] [Search term]
${WHITE_BOLD}Flags${COLOR_RESET}
${GREEN_NORMAL}<none>${COLOR_RESET} List Pull Requests from current directory
${GREEN_NORMAL}-c ${COLOR_RESET} Cache the response, for example "30s", "15m", "1h" (default: 20s)
${GREEN_NORMAL}-e ${COLOR_RESET} Emoji to make a reaction (default: 👍 )
${GREEN_NORMAL}-h ${COLOR_RESET} Help
${GREEN_NORMAL}-o ${COLOR_RESET} sorting order of Pull Requests (default: created-desc)
${GREEN_NORMAL}-r ${COLOR_RESET} Specify a repo (form: OWNER/REPO)
${GREEN_NORMAL}-w ${COLOR_RESET} Display the preview window upon start (default: hidden)
${WHITE_BOLD}Symbols${COLOR_RESET}
🔖 Checks indicator
${YELLOW_NORMAL}◌${COLOR_RESET} in progress
${GREEN_NORMAL}✓${COLOR_RESET} success
${RED_NORMAL}x${COLOR_RESET} failure
⊘ cancelled
💬 Total number of comments (gray commentable; red locked)
📣 Total number of emojis (green reactable; yellow reacted; red locked)
${GREEN_NORMAL}+${COLOR_RESET} Additions
${RED_NORMAL}-${COLOR_RESET} Deletions
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}enter ${COLOR_RESET} See comments
${GREEN_NORMAL}tab ${COLOR_RESET} Toggle preview
${GREEN_NORMAL}shift+tab ${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+a ${COLOR_RESET} ALL Pull Requests
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+d ${COLOR_RESET} Toggle diff
${GREEN_NORMAL}ctrl+e ${COLOR_RESET} Edit a Pull Request
${GREEN_NORMAL}ctrl+f ${COLOR_RESET} Fuzzy search
${GREEN_NORMAL}ctrl+g ${COLOR_RESET} Merge a Pull Request
${GREEN_NORMAL}ctrl+o ${COLOR_RESET} OPEN Pull Requests only
${GREEN_NORMAL}ctrl+p ${COLOR_RESET} Put "involves:@me" into the search
${GREEN_NORMAL}ctrl+r ${COLOR_RESET} Reload
${GREEN_NORMAL}ctrl+t ${COLOR_RESET} React with $(_emoji_from_name "$REACTION_EMOJI") emoji
${GREEN_NORMAL}ctrl+u ${COLOR_RESET} Undo the $(_emoji_from_name "$REACTION_EMOJI") emoji reaction
${GREEN_NORMAL}ctrl+x ${COLOR_RESET} Write a comment
${GREEN_NORMAL}ctrl+y ${COLOR_RESET} Checkout
${GREEN_NORMAL}shift+left ${COLOR_RESET} Switch to Issues
${GREEN_NORMAL}shift+right${COLOR_RESET} Switch to Workflow Runs
${GREEN_NORMAL}esc ${COLOR_RESET} Quit
EOF
}
fzf_pr_function() {
GH_PR_PREVIEW=$'gh api graphql --paginate --raw-field id={1} --raw-field query=\'query($id: ID!, $endCursor: String){node(id: $id) {... on PullRequest { additions author { login } body createdAt comments(first: 100) { totalCount nodes { author { login } body createdAt viewerDidAuthor reactionGroups { content reactors(last: 4) { totalCount nodes { ... on Actor { login }}}}}} commits(last: 1) { nodes { commit { statusCheckRollup { state contexts { checkRunCount checkRunCountsByState { state count }}}}}} deletions timelineItems(itemTypes: [CLOSED_EVENT, MERGED_EVENT, ISSUE_COMMENT], first: 100, after: $endCursor) { nodes { ... on ClosedEvent { actor { login } createdAt stateReason } ... on IssueComment { author { login } body createdAt viewerDidAuthor reactionGroups { content reactors(last: 4) { totalCount nodes { ... on Actor { login }}}} reactionGroups { content viewerHasReacted reactors(last: 4) { nodes { ... on Actor { login }} totalCount }}} ... on MergedEvent { actor { login } createdAt mergeRef { repository { name } name } commit { abbreviatedOid }}} pageInfo { hasNextPage endCursor }} labels(first:10) { nodes { name } } number reactionGroups { content viewerHasReacted reactors(last: 4) { nodes { ... on Actor { login }} totalCount }} state title viewerDidAuthor }}}\' --template \'{{- $prefix := .data.node -}}{{- $statePRColor := "green+b" -}}{{- if eq $prefix.state "CLOSED" -}}{{- $statePRColor = "red+b" -}}{{- else if eq $prefix.state "MERGED" -}}{{- $statePRColor = "magenta+b" -}}{{- end -}}{{- $issueAuthor := "[DELETED_USER]" -}}{{- if $prefix.author -}}{{- $issueAuthor = $prefix.author.login -}}{{- end -}}{{- $authorColor := "cyan+hb" -}}{{- if $prefix.viewerDidAuthor -}}{{- $authorColor = "yellow+bh" -}}{{- end -}}{{- tablerow ($prefix.title | color "white+bh") -}}{{- tablerender -}}{{- tablerow (printf "%s #%.0f" $prefix.state $prefix.number | color $statePRColor) ($issueAuthor | color $authorColor) (printf "│ %s ∙ %.0f Comments │" (timeago $prefix.createdAt) $prefix.comments.totalCount | color "gray+h") -}}{{- tablerender -}}{{- $checksDisplay := "No checks" | color "gray+h" -}}{{- $statusCheckRollup := (index $prefix.commits.nodes 0).commit.statusCheckRollup -}}{{- if $statusCheckRollup -}}{{- $totalChecks := $statusCheckRollup.contexts.checkRunCount -}}{{- $failedChecks := 0 -}}{{- range $statusCheckRollup.contexts.checkRunCountsByState -}}{{- if eq .state "FAILURE" -}}{{ $failedChecks = .count -}}{{- end -}}{{- end -}}{{- if eq $statusCheckRollup.state "SUCCESS" -}}{{- $checksDisplay = printf "✓ %.0f Checks passed" $totalChecks | color "green" -}}{{- else if eq $statusCheckRollup.state "PENDING" -}}{{- $checksDisplay = "◌ Checks pending" | color "yellow" -}}{{- else if eq $statusCheckRollup.state "FAILURE" -}}{{- $checksDisplay = printf "x %.0f/%.0f Checks failed" $failedChecks $totalChecks | color "red" -}}{{- else if eq $statusCheckRollup.state "CANCELLED" -}}{{- $checksDisplay = "⊘ Checks cancelled" | color "gray+h" -}}{{- else -}}{{- $checksDisplay = $statusCheckRollup.state | color "white" -}}{{- end -}}{{- end -}}{{- tablerow (printf "%s %s ∙ %s" (printf "+%.0f" $prefix.additions | color "green") (printf "-%.0f" $prefix.deletions | color "red") $checksDisplay) -}}{{- tablerender -}}{{- if $prefix.labels.nodes -}}{{- tablerow ("Labels:" | color "white+b") (pluck "name" $prefix.labels.nodes | join ", " | printf "[ %s ]" | color "white") -}}{{- tablerender -}}{{- end -}}{{- range $prefix.reactionGroups -}}{{ $emoji := .content }}{{ if eq .content "CONFUSED" }}{{ $emoji = "😕" }}{{ else if eq .content "EYES" }}{{ $emoji = "👀" }}{{ else if eq .content "HEART" }}{{ $emoji = "💖" }}{{ else if eq .content "HOORAY" }}{{ $emoji = "🎉" }}{{ else if eq .content "LAUGH" }}{{ $emoji = "😄" }}{{ else if eq .content "THUMBS_DOWN" }}{{ $emoji = "👎" }}{{ else if eq .content "THUMBS_UP" }}{{ $emoji = "👍" }}{{ else if eq .content "ROCKET" }}{{ $emoji = "🚀" }}{{ end }}{{ if gt .reactors.totalCount 0.0 }}{{ $emojiGiver := pluck "login" .reactors.nodes | join ", " | color "green+d" }}{{ if gt (pluck "login" .reactors.nodes | len ) 3 }}{{- /* NOTE: number of names limited */ -}}{{ $emojiGiver = slice (pluck "login" .reactors.nodes) 1 | join ", " | printf "%s, ..." | color "green+d" }}{{end}}{{- $emojiTotal := .reactors.totalCount | color "green+bd" -}}{{- if .viewerHasReacted -}}{{- $emojiTotal = .reactors.totalCount | color "yellow+bh" -}}{{- end -}}{{- tablerow (printf "%s %s" $emojiTotal $emoji) $emojiGiver -}}{{- tablerender -}}{{- end -}}{{- end }}{{- tablerow ("——————————————————————————————————————————————————————————"| color "gray+d") -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- $bodyText := $prefix.body }}{{ if eq ($bodyText | len) 0 -}}{{- $bodyText = " No description provided. " | color "white+i" -}}{{- end -}}{{- $bodyText -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- range $prefix.timelineItems.nodes -}}{{- $actorTimeline := "[DELETED_USER]" -}}{{- if .actor -}}{{- $actorTimeline = .actor.login -}}{{- end -}}{{- $authorTimelineColor := "cyan+hb" -}}{{- if .viewerDidAuthor -}}{{- $authorTimelineColor = "yellow+bh" -}}{{- end -}}{{- if .mergeRef -}}{{- $stateMergedColor := "93+b" -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color $stateMergedColor) -}}{{- tablerender -}}{{- tablerow (printf "%s merged %s into %s %s" ($actorTimeline | color $authorTimelineColor) (.commit.abbreviatedOid | color $stateMergedColor) (printf "%s:%s" .mergeRef.repository.name .mergeRef.name) (timeago .createdAt | color "white+bh")) -}}{{- tablerender -}}{{- tablerow ("◢■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◣"| color $stateMergedColor) -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- else if eq .stateReason "COMPLETED" -}}{{- $stateClosedColor := "red+b" -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color $stateClosedColor) -}}{{- tablerender -}}{{- tablerow (printf "%s closed this %s" ($actorTimeline | color $authorTimelineColor) (timeago .createdAt | color "white+bh")) -}}{{- tablerender -}}{{- tablerow ("◢■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◣"| color $stateClosedColor) -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- else -}}{{- $authorTimeline := "[DELETED_USER]" -}}{{- if .author -}}{{- $authorTimeline = .author.login -}}{{- end -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow ("◥■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■◤"| color "gray+d") -}}{{- tablerender -}}{{- tablerow ($authorTimeline | color $authorTimelineColor) ( timeago .createdAt | color "white+bh") -}}{{- tablerender -}}{{- range .reactionGroups -}}{{ $emojiInComment := .content }}{{ if eq .content "CONFUSED" }}{{ $emojiInComment = "😕" }}{{ else if eq .content "EYES" }}{{ $emojiInComment = "👀" }}{{ else if eq .content "HEART" }}{{ $emojiInComment = "💖" }}{{ else if eq .content "HOORAY" }}{{ $emojiInComment = "🎉" }}{{ else if eq .content "LAUGH" }}{{ $emojiInComment = "😄" }}{{ else if eq .content "THUMBS_DOWN" }}{{ $emojiInComment = "👎" }}{{ else if eq .content "THUMBS_UP" }}{{ $emojiInComment = "👍" }}{{ else if eq .content "ROCKET" }}{{ $emojiInComment = "🚀" }}{{ end }}{{ if gt .reactors.totalCount 0.0 }}{{ $emojiInCommentGiver := pluck "login" .reactors.nodes | join ", " | color "green+d" }}{{ if gt (pluck "login" .reactors.nodes | len ) 3 }}{{- /* NOTE: number of names limited */ -}}{{ $emojiInCommentGiver = slice (pluck "login" .reactors.nodes) 1 | join ", " | printf "%s, ..." | color "green+d" }}{{end}}{{- $emojiInCommentTotal := .reactors.totalCount | color "green+bd" -}}{{- if .viewerHasReacted -}}{{- $emojiInCommentTotal = .reactors.totalCount | color "yellow+bh" -}}{{- end -}}{{- tablerow (printf "%s %s" $emojiInCommentTotal $emojiInComment) $emojiInCommentGiver -}}{{- tablerender -}}{{- end -}}{{- end -}}{{- tablerow ("‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥"| color "gray+d") -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- .body -}}{{- tablerow "" -}}{{- tablerender -}}{{- tablerow "" -}}{{- tablerender -}}{{- end -}}{{- end -}}\''
# fzf allows hiding columns "--with-nth 2...", headers are also affected, so NODE_ID_HIDDEN is included there
GH_PR_COMMAND=$'gh api graphql --raw-field query=\'query($filter: String!) {search(query: $filter, type: ISSUE, first: 35) {issueCount nodes { ... on PullRequest { additions author { login } comments {totalCount} commits(last: 1) { nodes { commit { statusCheckRollup { state }}}} deletions id number updatedAt reactions { totalCount viewerHasReacted } state title viewerCanReact viewerDidAuthor }}}}\' --template \'
{{- $limitCount := .data.search.issueCount -}}
{{- if gt .data.search.issueCount 35.0 -}}{{- $limitCount = 35.0 -}}{{- end -}}
{{- tablerow "NODE_ID_HIDDEN" (printf "%.0f of ∑ %.0f" $limitCount .data.search.issueCount | color "blue+b") ("|" | color "white+d") ("? Help · esc Quit" | color "blue") -}}{{- tablerender -}}
{{- tablerow "" -}}{{- tablerender -}}
{{- $headerColor := "blue+b" -}}
{{- tablerow "NODE_ID_HIDDEN" ("PR" | color $headerColor) ("AUTHOR" | color $headerColor) ("LAST UPDATE" | color $headerColor) "🔖" "💬" "📣" ("+" | color "green+bd") ("-" | color "red+bd") ("TITLE" | color $headerColor) -}}
{{- range .data.search.nodes -}}
{{- $statePullRequestColor := "green" -}}
{{- if eq .state "CLOSED" }}{{ $statePullRequestColor = "red" }}{{- else if eq .state "MERGED" }}{{ $statePullRequestColor = "magenta" }}{{ end -}}
{{- $author := "[DELETED_USER]" -}}
{{- if .author -}}{{- $author = .author.login -}}{{- end -}}
{{- $authorColor := "cyan+h" -}}
{{- if .viewerDidAuthor -}}{{- $authorColor = "yellow" -}}{{- end -}}
{{- $commentCount := "\u00a0" -}}{{- /* NOTE: U+00a0 (non-breaking space) for entries without comments, which the viewer can comment on. Needed to make a if decision with fzf later in the script. */ -}}
{{- if not .viewerCanReact -}}{{- $commentCount = (or (printf "%.0f" .comments.totalCount) "0") -}}
{{- else if .comments.totalCount -}}{{- $commentCount = printf "%.0f" .comments.totalCount -}}
{{- end -}}
{{- $commentColor := "white+bd" -}}
{{- if not .viewerCanReact -}}{{- $commentColor = "red+bd" -}}{{- end -}}
{{- $emojiCount := "" -}}
{{- if not .viewerCanReact -}}{{- $emojiCount = (or (printf "%.0f" .reactions.totalCount) "0") -}}
{{- else if .reactions.totalCount -}}{{- $emojiCount = printf "%.0f" .reactions.totalCount -}}
{{- end -}}
{{- $emojiColor := "green+bd" -}}
{{- if not .viewerCanReact -}}{{- $emojiColor = "red+bd" -}}
{{- else if .reactions.viewerHasReacted -}}{{- $emojiColor = "yellow+b" -}}
{{- end -}}
{{- /* NOTE: Go templates use zero based indexing, like JavaScript */ -}}
{{- $statusCheckRollup := "" -}}
{{- if .commits.nodes -}}
{{- $statusCheckRollup = (index .commits.nodes 0).commit.statusCheckRollup -}}
{{- end -}}
{{- $checksDisplay := "" -}}
{{- if $statusCheckRollup -}}
{{- if eq $statusCheckRollup.state "SUCCESS" -}}
{{- $checksDisplay = "✓" | color "green" -}}
{{- else if eq $statusCheckRollup.state "PENDING" -}}
{{- $checksDisplay = "◌" | color "yellow" -}}
{{- else if eq $statusCheckRollup.state "FAILURE" -}}
{{- $checksDisplay = "x" | color "red" -}}
{{- else if eq $statusCheckRollup.state "CANCELLED" -}}
{{- $checksDisplay = "⊘" | color "gray+h" -}}
{{- else -}}
{{- $checksDisplay = "?" | color "white" -}}
{{- end -}}
{{- end -}}
{{- tablerow .id (printf "#%.0f" .number | color $statePullRequestColor) ($author | color $authorColor) (timeago .updatedAt) $checksDisplay ($commentCount | color $commentColor) ($emojiCount | color $emojiColor) (printf "+%.0f" .additions | color "green") (printf "-%.0f" .deletions | color "red") .title -}}
{{- end -}}\''
INITIAL_PR_PROMPT=$(grep -q A <"$SCRATCH_FILE" && echo "Pull Requests [$OWNER_REPO_NAME] > " || echo "${GREEN_NORMAL}OPEN Pull Requests [$OWNER_REPO_NAME] > ")
OUTPUT_SELECTION=$(FZF_DEFAULT_COMMAND="(grep -q A <$SCRATCH_FILE && $GH_PR_COMMAND --cache ${1:-$CACHE_TIME} --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME $INITIAL_QUERY \" || $GH_PR_COMMAND --cache ${1:-$CACHE_TIME} --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME state:open $INITIAL_QUERY \") || true" \
_fzf_basic_options --disabled --header-lines 3 --with-nth 2.. \
--prompt "$INITIAL_PR_PROMPT" \
--query "$INITIAL_QUERY" --preview "sleep 0.35; $GH_PR_PREVIEW | $LANG_BAT_SETUP md" \
--bind "change:first+reload-sync:sleep 0.25; (grep -q A <$SCRATCH_FILE && $GH_PR_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME \"{q} || $GH_PR_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--bind "?:toggle-preview+change-preview:echo '$(help_pr_function)'" \
--bind "tab:toggle-preview+change-preview:sleep 0.35; $GH_PR_PREVIEW | $LANG_BAT_SETUP md" \
--bind "ctrl-a:rebind(change)+change-prompt(Pull Requests [$OWNER_REPO_NAME] > )+disable-search+execute-silent(echo A >$SCRATCH_FILE)+reload-sync:$GH_PR_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME \"{q} || true" \
--bind "ctrl-b:execute-silent(gh pr view {2} --web --repo $OWNER_REPO_NAME)" \
--bind "ctrl-f:unbind(change)+enable-search+clear-query+change-prompt(${RED_NORMAL}FUZZY SEARCH [$OWNER_REPO_NAME] > )" \
--bind "ctrl-d:toggle-preview+change-preview(gh pr diff {2} --repo $OWNER_REPO_NAME"$' | if type -p delta >/dev/null; then delta --width ${FZF_PREVIEW_COLUMNS:-$COLUMNS}; else cat; fi)' \
--bind "ctrl-o:rebind(change)+change-prompt(${GREEN_NORMAL}OPEN Pull Requests [$OWNER_REPO_NAME] > )+disable-search+execute-silent(echo B >$SCRATCH_FILE)+reload-sync:$GH_PR_COMMAND --cache $CACHE_TIME --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME state:open \"{q} || true" \
--bind "ctrl-p:put(involves:@me)" \
--bind "ctrl-r:reload-sync:(grep -q A <$SCRATCH_FILE && $GH_PR_COMMAND --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME \"{q} || $GH_PR_COMMAND --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--bind "ctrl-t:execute-silent($GH_ADD_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:(grep -q A <$SCRATCH_FILE && $GH_PR_COMMAND --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME \"{q} || $GH_PR_COMMAND --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--bind "ctrl-u:execute-silent($GH_REMOVE_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:(grep -q A <$SCRATCH_FILE && $GH_PR_COMMAND --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME \"{q} || $GH_PR_COMMAND --raw-field filter=\"$SORTING_ORDER type:pr repo:$OWNER_REPO_NAME state:open \"{q}) || true" \
--expect "ctrl-e,ctrl-g,ctrl-x,ctrl-y,enter,shift-left,shift-right") || true
printed_query="$(sed 1q <<<"$OUTPUT_SELECTION")"
expected_key="$(sed '1d;3d' <<<"$OUTPUT_SELECTION")"
TICKET_NUMBER="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $2}' | tr -d "#")"
number_comments="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $7,$8}' | grep -Eo '[0-9]+' || true)"
# reset the variable
OUTPUT_SELECTION=""
case "$expected_key" in
ctrl-e)
gh pr edit "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_QUERY="$printed_query"
fzf_pr_function 0s
;;
ctrl-g)
gh pr merge "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME"
exit 0
;;
ctrl-x)
gh pr comment "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_QUERY="$printed_query"
fzf_pr_function 0s
;;
ctrl-y)
gh pr checkout "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME"
exit 0
;;
enter)
INITIAL_QUERY="$printed_query"
if [ -z "$number_comments" ]; then
_load_indicator $'No comments on this Pull Request\nRestarting'
fzf_pr_function
else
fzf_pr_comment_function
fi
;;
shift-left)
INITIAL_QUERY=""
fzf_issue_function
;;
shift-right)
INITIAL_QUERY=""
fzf_workflow_run_function
;;
esac
}
help_pr_comment_function() {
cat <<EOF
${WHITE_BOLD}Symbols${COLOR_RESET}
📣 Total number of emojis (green reactable; yellow reacted; red locked)
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}tab ${COLOR_RESET} Toggle preview
${GREEN_NORMAL}shift+tab${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+d ${COLOR_RESET} Toggle diff
${GREEN_NORMAL}ctrl+t ${COLOR_RESET} React with $(_emoji_from_name "$REACTION_EMOJI") emoji
${GREEN_NORMAL}ctrl+u ${COLOR_RESET} Undo the $(_emoji_from_name "$REACTION_EMOJI") emoji reaction
${GREEN_NORMAL}ctrl+x ${COLOR_RESET} Write a comment
${GREEN_NORMAL}esc ${COLOR_RESET} Return to Pull Request list
EOF
}
fzf_pr_comment_function() {
GH_PR_COMMENTS_COMMAND=$'gh api graphql --paginate --raw-field query=\'query ($owner: String!, $name: String!, $number: Int!, $endCursor: String) { repository(owner: $owner, name: $name) { pullRequest(number: $number) { additions author { login } createdAt labels(first:10) { nodes { name } } number state title comments(first: 100, after: $endCursor) { totalCount nodes { author { login } body createdAt databaseId id reactions { totalCount viewerHasReacted } viewerCanReact viewerDidAuthor } pageInfo { endCursor hasNextPage }} commits(last: 1) { nodes { commit { statusCheckRollup { state contexts { checkRunCount checkRunCountsByState { state count }}}}}} deletions }}}\' --template \'
{{- $prefix := .data.repository.pullRequest -}}
{{- $statePullRequestCommentColor := "green" -}}
{{- if eq $prefix.state "CLOSED" -}}{{- $statePullRequestCommentColor = "red+b" -}}{{- else if eq $prefix.state "MERGED" }}{{- $statePullRequestCommentColor = "magenta+b" -}}{{- end -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ($prefix.title | color "white+bh") -}}{{- tablerender -}}
{{- $pullRequestAuthor := "[DELETED_USER]" -}}
{{- if $prefix.author -}}{{- $pullRequestAuthor = $prefix.author.login -}}{{- end -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" (printf "%s #%.0f" $prefix.state $prefix.number | color $statePullRequestCommentColor) ($pullRequestAuthor | color "cyan+hb") (printf "│ %s ∙ %.0f Comments │" (timeago $prefix.createdAt) $prefix.comments.totalCount | color "gray+h") ("? Help · esc Quit" | color "blue") -}}{{- tablerender -}}
{{- $checksDisplay := "No checks" | color "gray+h" -}}
{{- $statusCheckRollup := "" -}}
{{- if .commits.nodes -}}
{{- $statusCheckRollup = (index $prefix.commits.nodes 0).commit.statusCheckRollup -}}
{{- end -}}
{{- if $statusCheckRollup -}}
{{- $totalChecks := $statusCheckRollup.contexts.checkRunCount -}}
{{- $failedChecks := 0 -}}
{{- range $statusCheckRollup.contexts.checkRunCountsByState -}}
{{- if eq .state "FAILURE" -}}{{ $failedChecks = .count -}}{{- end -}}
{{- end -}}
{{- if eq $statusCheckRollup.state "SUCCESS" -}}
{{- $checksDisplay = printf "✓ %.0f Checks passed" $totalChecks | color "green" -}}
{{- else if eq $statusCheckRollup.state "PENDING" -}}
{{- $checksDisplay = "◌ Checks pending" | color "yellow" -}}
{{- else if eq $statusCheckRollup.state "FAILURE" -}}
{{- $checksDisplay = printf "x %.0f/%.0f Checks failed" $failedChecks $totalChecks | color "red" -}}
{{- else if eq $statusCheckRollup.state "CANCELLED" -}}
{{- $checksDisplay = "⊘ Checks cancelled" | color "gray+h" -}}
{{- else -}}
{{- $checksDisplay = $statusCheckRollup.state | color "white" -}}
{{- end -}}
{{- end -}}
{{- $dotty := "∙" | color "white" -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" (printf "%s %s %s %s" (printf "+%.0f" $prefix.additions | color "green") (printf "-%.0f" $prefix.deletions | color "red") $dotty $checksDisplay) -}}{{- tablerender -}}
{{- if $prefix.labels.nodes -}}{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ("Labels:" | color "white+b") (pluck "name" $prefix.labels.nodes | join ", " | printf "[ %s ]" | color "white") -}}{{- else -}}{{- tablerow "" -}}{{- end -}}{{- tablerender -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ("———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————" | color "gray+d") -}}{{- tablerender -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" "" -}}{{- tablerender -}}
{{- $headerColor := "blue+b" -}}
{{- tablerow "NODE_ID_HIDDEN" "DATABASE_ID_HIDDEN" ("COMMENTER" | color $headerColor) ("AGE" | color $headerColor) "📣" ("COMMENT" | color $headerColor) -}}
{{- range $prefix.comments.nodes -}}
{{- $author := "[DELETED_USER]" -}}
{{- if .author -}}{{- $author = .author.login -}}{{- end -}}
{{- $authorColor := "cyan+h" -}}
{{- if .viewerDidAuthor -}}{{- $authorColor = "yellow" -}}{{- end -}}
{{- $emojiCount := "" -}}
{{- if not .viewerCanReact -}}{{- $emojiCount = (or (printf "%.0f" .reactions.totalCount) "0") -}}
{{- else if .reactions.totalCount -}}{{- $emojiCount = printf "%.0f" .reactions.totalCount -}}
{{- end -}}
{{- $emojiColor := "green+bd" -}}
{{- if not .viewerCanReact -}}{{- $emojiColor = "red+bd" -}}
{{- else if .reactions.viewerHasReacted -}}{{- $emojiColor = "yellow+b" -}}
{{- end -}}
{{- tablerow .id .databaseId ($author | color $authorColor) (timeago .createdAt) ($emojiCount | color $emojiColor) .body -}}
{{- end -}}\''
OUTPUT_SELECTION=$(FZF_DEFAULT_COMMAND="$GH_PR_COMMENTS_COMMAND --cache ${1:-$CACHE_TIME} --raw-field owner=${OWNER_REPO_NAME%/*} --raw-field name=${OWNER_REPO_NAME#*/} --field number=$TICKET_NUMBER" \
_fzf_basic_options --header-lines 7 --with-nth 3.. \
--query "$INITIAL_COMMENT_QUERY" --prompt "${BLUE_NORMAL}Pull Request 💬 [$OWNER_REPO_NAME] > " \
--preview-window "+5:~5" --preview "$GH_COMMENTS_PREVIEW | $LANG_BAT_SETUP md" \
--bind "?:toggle-preview+change-preview:echo '$(help_pr_comment_function)'" \
--bind "tab:toggle-preview+change-preview:$GH_COMMENTS_PREVIEW | $LANG_BAT_SETUP md" \
--bind "ctrl-b:execute-silent(python -m webbrowser https://github.com/$OWNER_REPO_NAME/pull/$TICKET_NUMBER#issuecomment-{2})" \
--bind "ctrl-d:toggle-preview+change-preview(gh pr diff $TICKET_NUMBER --repo $OWNER_REPO_NAME"$' | if type -p delta >/dev/null; then delta --width ${FZF_PREVIEW_COLUMNS:-$COLUMNS}; else cat; fi)' \
--bind "ctrl-t:execute-silent($GH_ADD_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:$GH_PR_COMMENTS_COMMAND --raw-field owner=${OWNER_REPO_NAME%/*} --raw-field name=${OWNER_REPO_NAME#*/} --field number=$TICKET_NUMBER || true" \
--bind "ctrl-u:execute-silent($GH_REMOVE_REACTION --raw-field id={1} --raw-field emoji=$REACTION_EMOJI)+reload-sync:$GH_PR_COMMENTS_COMMAND --raw-field owner=${OWNER_REPO_NAME%/*} --raw-field name=${OWNER_REPO_NAME#*/} --field number=$TICKET_NUMBER || true" \
--expect "ctrl-x,esc") || true
printed_query="$(sed 1q <<<"$OUTPUT_SELECTION")"
expected_key="$(sed '1d;3d' <<<"$OUTPUT_SELECTION")"
# reset the variable
OUTPUT_SELECTION=""
case "$expected_key" in
ctrl-x)
gh pr comment "$TICKET_NUMBER" --repo "$OWNER_REPO_NAME" || true
_load_indicator
INITIAL_COMMENT_QUERY="$printed_query"
fzf_pr_comment_function 0s
;;
esc)
INITIAL_COMMENT_QUERY=""
fzf_pr_function
;;
esac
}
###################################### WORKFLOW RUNS
help_workflow_run_function() {
cat <<EOF
${WHITE_BOLD}Usage${COLOR_RESET}
gh look [ r | run ] [Flags] [Search term]
${WHITE_BOLD}Flags${COLOR_RESET}
${GREEN_NORMAL}<none>${COLOR_RESET} List Workflow Runs from current directory
${GREEN_NORMAL}-h ${COLOR_RESET} Help
${GREEN_NORMAL}-i ${COLOR_RESET} Interval for updating the list (default: 10)
${GREEN_NORMAL}-n ${COLOR_RESET} Number of listed Runs (default: 10, max: 100)
${GREEN_NORMAL}-p ${COLOR_RESET} Pull Requests are included in the list (default: not included)
${GREEN_NORMAL}-r ${COLOR_RESET} Specify a repository (form: OWNER/REPO)
${GREEN_NORMAL}-w ${COLOR_RESET} Display the preview window upon start (default: hidden)
${WHITE_BOLD}Symbols${COLOR_RESET}
👀 List is updated every ${UPDATE_TIME}s, stop it with a defined hotkey
🔖 Status
${YELLOW_NORMAL}◌${COLOR_RESET} in progress
${GREEN_NORMAL}✓${COLOR_RESET} success
${RED_NORMAL}x${COLOR_RESET} failure
⊘ cancelled
⌛️ Elapsed Time (min)
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}enter ${COLOR_RESET} See logs, press "q" to return
${GREEN_NORMAL}tab ${COLOR_RESET} Toggle preview
${GREEN_NORMAL}shift+tab ${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+a ${COLOR_RESET} Abort watch mode
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+d ${COLOR_RESET} Download attached artifacts
${GREEN_NORMAL}ctrl+g ${COLOR_RESET} Cancel a Run
${GREEN_NORMAL}ctrl+f ${COLOR_RESET} See failed logs
${GREEN_NORMAL}ctrl+o ${COLOR_RESET} Operate in watch mode 👀 update the list every ${UPDATE_TIME}s
${GREEN_NORMAL}ctrl+x ${COLOR_RESET} Delete a Run
${GREEN_NORMAL}shift+left${COLOR_RESET} Switch to Pull Requests
${GREEN_NORMAL}esc ${COLOR_RESET} Quit
EOF
}
fzf_workflow_run_function() {
INITIAL_WORKFLOW_PROMPT="Workflow Runs $(printf "[%s]" "$OWNER_REPO_NAME")"
GH_WORKFLOWS_COMMAND="gh api --cache ${UPDATE_TIME}s --method GET --raw-field exclude_pull_requests=$EXCLUDE_PULL_REQUESTS --field per_page=$NUMBER_WORKFLOW_RUN_LIST repos/$OWNER_REPO_NAME/actions/runs "$' --jq \'
def colors:
{
"gray": "\u001b[90m",
"red": "\u001b[31m",
"green": "\u001b[32m",
"yellow": "\u001b[33m",
"cyan": "\u001b[36m",
"white_bold": "\u001b[1;37m",
"COMMENT": "Header lines must be colored when a column element is colored so that they are aligned correctly. Also, only color codes that have the same length may be used, i.e. - \u001b[1;36m - would only work if the header was colored with - \u001b[0000m -. This is just a very silly workaround for the fact that the column command cannot handle ANSI colors to correctly calculate the actual cell width.",
"none": "\u001b[00m",
"none_bold": "\u001b[0000m",
"reset": "\u001b[0m"
};
def colored(text; color): colors[color] + text + colors.reset;
["ID", "URL", "STATUS", colored(" "; "none"), colored("🔖"; "none"), "⌛️", colored("Workflow"; "none"), colored("Branch"; "none_bold"), "Event", "Title"],
(.workflow_runs[] | [
.id,
.html_url,
.status,
colored((.created_at | fromdateiso8601) as $time_sec | if ((now - $time_sec) / 3600) < 1 then ((now - $time_sec) / 60 | floor) | tostring + "min ago" elif ((now - $time_sec) / 3600) < 24 then ((now - $time_sec) / 3600 | floor) | tostring + "h ago" else ($time_sec | strftime("%d/%b/%y")) end; "gray"),
(if (.conclusion == null and .status == null) then colored(" "; "none") elif .conclusion == null then if .status == "in_progress" then colored("◌"; "yellow") else colored(.status; "none") end else if .conclusion == "success" then colored("✓"; "green") elif .conclusion == "failure" then colored("x"; "red") elif .conclusion == "cancelled" then colored("⊘"; "gray") else colored(.conclusion; "none") end end),
((if .status == "in_progress" then now else (.updated_at | fromdateiso8601) end) - (.created_at | fromdateiso8601) | (./60|floor)+(.%60/60*100|round/100) | .*100|round/100),
colored(.name; "cyan"),
colored(.head_branch; "white_bold"),
.event,
.display_title]
) | @tsv\' | column -ts "\t"'
OUTPUT_SELECTION=$(FZF_DEFAULT_COMMAND="$GH_WORKFLOWS_COMMAND" \
_fzf_basic_options --header-lines 1 --with-nth 4.. \
--header $'\n? Help · esc Quit\n\n' \
--prompt "${GREEN_NORMAL}$INITIAL_WORKFLOW_PROMPT 👀 ${UPDATE_TIME}s > " \
--query "$INITIAL_QUERY" \
--preview "gh run view {1} --repo $OWNER_REPO_NAME" \
--bind "tab:toggle-preview+change-preview:gh run view {1} --repo $OWNER_REPO_NAME" \
--bind "?:toggle-preview+change-preview:echo '$(help_workflow_run_function)'" \
--bind "load:reload-sync:sleep $UPDATE_TIME;$GH_WORKFLOWS_COMMAND || true" \
--bind "ctrl-a:unbind(load)+change-prompt($INITIAL_WORKFLOW_PROMPT > )+reload-sync:$GH_WORKFLOWS_COMMAND || true" \
--bind "ctrl-b:execute-silent:python -m webbrowser {2}" \
--bind "ctrl-d:become(gh run download {1} --repo $OWNER_REPO_NAME)+reload-sync:$GH_WORKFLOWS_COMMAND || true" \
--bind "ctrl-g:execute-silent(gh run cancel {1} --repo $OWNER_REPO_NAME)+down+reload-sync:$GH_WORKFLOWS_COMMAND || true" \
--bind "ctrl-o:rebind(load)+change-prompt(${GREEN_NORMAL}$INITIAL_WORKFLOW_PROMPT 👀 ${UPDATE_TIME}s > )+reload-sync:$GH_WORKFLOWS_COMMAND || true" \
--bind "ctrl-x:execute-silent(gh run delete {1} --repo $OWNER_REPO_NAME)+down+reload-sync:$GH_WORKFLOWS_COMMAND || true" \
--expect "ctrl-f,enter,shift-left") || true
printed_query="$(sed 1q <<<"$OUTPUT_SELECTION")"
expected_key="$(sed '1d;3d' <<<"$OUTPUT_SELECTION")"
output_id="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $1}')"
output_status="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $3}')"
# reset the variable
OUTPUT_SELECTION=""
case "$expected_key" in
enter)
if grep -Eq 'in_progress|queued' <<<"$output_status"; then
gh run watch --repo "$OWNER_REPO_NAME"
elif ! gh run view --log "$output_id" --repo "$OWNER_REPO_NAME" &>/dev/null; then
_load_indicator $'No logs found\nRestarting'
else
GH_PAGER="$LANG_BAT_SETUP log" gh run view --log "$output_id" --repo "$OWNER_REPO_NAME"
fi
INITIAL_QUERY="$printed_query"
fzf_workflow_run_function
;;
ctrl-f)
if ! gh run view --log-failed "$output_id" --repo "$OWNER_REPO_NAME" &>/dev/null; then
_load_indicator $'No failed logs found\nRestarting'
else
GH_PAGER="$LANG_BAT_SETUP log" gh run view --log-failed "$output_id" --repo "$OWNER_REPO_NAME"
fi
INITIAL_QUERY="$printed_query"
fzf_workflow_run_function
;;
shift-left)
INITIAL_QUERY=""
fzf_pr_function
;;
esac
}
###################################### SEARCH
help_search_function() {
cat <<EOF
${WHITE_BOLD}Usage${COLOR_RESET}
gh look [ s | search ] [Flags] [Search term]
${WHITE_BOLD}Flags${COLOR_RESET}
${GREEN_NORMAL}<term>${COLOR_RESET} Search for GitHub repos
${GREEN_NORMAL}-c ${COLOR_RESET} Cache the response, for example "30s", "15m", "1h" (default: 20s)
${GREEN_NORMAL}-h ${COLOR_RESET} Help
${GREEN_NORMAL}-w ${COLOR_RESET} Display the preview window upon start (default: hidden)
${WHITE_BOLD}Symbols${COLOR_RESET}
⭐ Number of stargazers
🏁 Primary language
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}tab ${COLOR_RESET} Preview README
${GREEN_NORMAL}shift+tab ${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+f ${COLOR_RESET} Preview release infos
${GREEN_NORMAL}ctrl+r ${COLOR_RESET} Reload
${GREEN_NORMAL}ctrl+t ${COLOR_RESET} Star repo
${GREEN_NORMAL}ctrl+u ${COLOR_RESET} Unstar repo
${GREEN_NORMAL}shift+left ${COLOR_RESET} Switch to your starred repos
${GREEN_NORMAL}shift+right${COLOR_RESET} Switch to Issues
${GREEN_NORMAL}esc ${COLOR_RESET} Quit
EOF
}
fzf_search_function() {
GH_SEARCH_COMMAND=$'gh api graphql --raw-field query=\'query($filter: String!) { search(query: $filter, type: REPOSITORY, first: 15) { repositoryCount nodes { ... on Repository {description isFork name owner {login} primaryLanguage {name} defaultBranchRef { target { ... on Commit { history(first: 1) { nodes { committedDate }}}}} stargazerCount viewerHasStarred}}}}\' --template=\'
{{- $limitCount := .data.search.repositoryCount -}}
{{- if gt .data.search.repositoryCount 15.0 -}}{{- $limitCount = 15.0 -}}{{- end -}}
{{- tablerow (printf "%.0f of ∑ %.0f Repos" $limitCount .data.search.repositoryCount | color "yellow") ("|" | color "white") ("? Help · esc Quit" | color "blue") -}}{{- tablerender -}}
{{- tablerow "" -}}{{- tablerender -}}
{{- $headerColor := "blue+b" -}}
{{- tablerow ("REPO" | color $headerColor) "⭐" "🏁" ("LAST COMMIT" | color $headerColor) ("DESCRIPTION" | color $headerColor) -}}
{{- range .data.search.nodes -}}
{{- $stargazerCount := printf "%.0f" .stargazerCount -}}
{{- if gt .stargazerCount 1000000.0 -}}{{- $stargazerCount = ">1M" -}}
{{- else if gt .stargazerCount 100000.0 -}}{{- $stargazerCount = printf "%.3sk" $stargazerCount -}}
{{- else if gt .stargazerCount 10000.0 -}}{{- $stargazerCount = printf "%.2sk" $stargazerCount -}}
{{- else if gt .stargazerCount 1000.0 -}}{{- $stargazerCount = printf "%.1sk" $stargazerCount -}}
{{- else if eq .stargazerCount 0.0 -}}{{- $stargazerCount = "" -}}
{{- end -}}
{{- $stargazerColor := "yellow" -}}
{{- if .viewerHasStarred }}{{ $stargazerColor = "011+h:094" }}{{ end -}}
{{- $langName := "" -}}
{{- if .primaryLanguage }}{{ $langName = .primaryLanguage.name }}{{ end -}}
{{- if eq $langName "JavaScript" }}{{- $langName = "" -}}
{{- else if eq $langName "Python" }}{{- $langName = "" -}}
{{ end -}}
{{- $time := "" -}}{{- /* NOTE: If a repo has no defaultBranch and/or commits, you need to conditionalize the .defaultBranchRef and the .committedDate to be safe against nil conditions, for example for repositories that are empty - "shellywu/wtclient". */ -}}
{{- if .defaultBranchRef }}
{{- range .defaultBranchRef.target.history.nodes -}}
{{- if .committedDate -}}{{- $time = (timeago .committedDate) -}}{{- end -}}
{{- end -}}
{{ end -}}
{{- tablerow (printf "%s%s%s" (.owner.login | color "cyan+h") ("/" | color "gray+h") (.name | color "cyan+hb")) ($stargazerCount | color $stargazerColor) ($langName | color "green") ($time | color "gray+h") .description -}}
{{- end -}}\''
OUTPUT_SELECTION=$(FZF_DEFAULT_COMMAND="$GH_SEARCH_COMMAND --raw-field filter='$INITIAL_QUERY' --cache $CACHE_TIME" \
_fzf_basic_options \
--disabled \
--prompt "Search repos > " \
--header-lines 3 \
--preview "gh repo view {1}" --preview-window wrap:"$PREVIEW_WINDOW_VISIBILITY" \
--query "$INITIAL_QUERY" --exact \
--bind "change:first+reload-sync:$GH_SEARCH_COMMAND --raw-field filter={q} --cache $CACHE_TIME || true" \
--bind "?:toggle-preview+change-preview:echo '$(help_search_function)'" \
--bind 'tab:toggle-preview+change-preview(gh repo view {1})' \
--bind "ctrl-b:execute-silent(gh repo view {1} --web)" \
--bind $'ctrl-f:toggle-preview+change-preview([[ $(gh release list -R {1}) ]] && gh release view -R {1})' \
--bind "ctrl-r:reload-sync:$GH_SEARCH_COMMAND --raw-field filter={q} || true" \
--bind "ctrl-t:execute-silent(gh api --method PUT /user/starred/{1})+reload-sync:$GH_SEARCH_COMMAND --raw-field filter={q} --cache 0s || true" \
--bind "ctrl-u:execute-silent(gh api --method DELETE /user/starred/{1})+reload-sync:$GH_SEARCH_COMMAND --raw-field filter={q} --cache 0s || true" \
--expect "shift-left,shift-right") || true
expected_key="$(sed '1d;3d' <<<"$OUTPUT_SELECTION")"
output_textline="$(sed '1d;2d' <<<"$OUTPUT_SELECTION" | awk '{print $1}')"
# reset the variable
OUTPUT_SELECTION=""
case "$expected_key" in
shift-left)
INITIAL_QUERY=""
USER_NAME="$(gh api graphql --cache 1h --raw-field query='{viewer{login}}' --jq '.data.viewer.login')"
fzf_star_function
;;
shift-right)
INITIAL_QUERY=""
OWNER_REPO_NAME=${output_textline:-"$OWNER_REPO_NAME"}
if [ -z "$OWNER_REPO_NAME" ]; then
OWNER_REPO_NAME="$(gh api graphql --cache 1h --field owner=:owner --field name=:repo --raw-field query=$'query ($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { nameWithOwner }}' --jq '.[].repository.nameWithOwner' 2>/dev/null || echo "cli/cli")"
fi
fzf_issue_function 0s
;;
esac
}
###################################### STARS
help_star_function() {
cat <<EOF
${WHITE_BOLD}Usage${COLOR_RESET}
gh look [ st | star ] [Flags] [Search term]
${WHITE_BOLD}Flags${COLOR_RESET}
${GREEN_NORMAL}<none>${COLOR_RESET} List your stars (sorted by the time the user set the star)
${GREEN_NORMAL}-c ${COLOR_RESET} Cache the response, for example "30s", "15m", "1h" (default: 20s)
${GREEN_NORMAL}-h ${COLOR_RESET} Help
${GREEN_NORMAL}-u ${COLOR_RESET} List stars of another user
${GREEN_NORMAL}-w ${COLOR_RESET} Display the preview window upon start (default: hidden)
${WHITE_BOLD}Symbols${COLOR_RESET}
⭐ Number of stargazers
🏁 Primary language
${WHITE_BOLD}Hotkeys${COLOR_RESET}
${GREEN_NORMAL}? ${COLOR_RESET} Toggle help
${GREEN_NORMAL}tab ${COLOR_RESET} Preview README
${GREEN_NORMAL}shift+tab ${COLOR_RESET} Change preview window size
${GREEN_NORMAL}ctrl+b ${COLOR_RESET} Browser
${GREEN_NORMAL}ctrl+f ${COLOR_RESET} Preview release infos