@@ -46,11 +46,21 @@ gh_api_get() {
4646 " https://api.github.com/repos/$REPO /$endpoint "
4747}
4848
49+ # Function to restore terminal to a sane state
50+ restore_terminal () {
51+ printf " \033[?25h" # Show cursor
52+ stty sane # Restore terminal to normal state
53+ printf " \033[H\033[2J" # Clear screen
54+ }
55+
4956# Function to select a milestone
5057select_milestone () {
58+ # Ensure terminal is in a normal state for initial question
59+ restore_terminal
60+
5161 # Ask about including closed milestones
5262 milestone_state=" open" # Default to open
53- echo -e " ${BLUE} Do you want to include closed milestones? (y/N) ${NC} "
63+ echo -e " Do you want to include closed milestones?\n(Type 'y' for 'yes', 'n' for 'no', then press Enter. Defaults to no.) "
5464 read -r include_closed
5565 # Only change if explicitly answered yes
5666 if [[ " $include_closed " =~ ^[Yy]$ ]]; then
@@ -60,7 +70,15 @@ select_milestone() {
6070 echo -e " ${BLUE} Fetching milestones...${NC} "
6171 milestones=$( gh_api_get " milestones?state=$milestone_state &sort=created&direction=desc" )
6272
63- if [ " $( echo " $milestones " | jq length) " -eq 0 ]; then
73+ # Check if milestones is empty or invalid
74+ if [ -z " $milestones " ]; then
75+ echo -e " ${RED} Error fetching milestones${NC} "
76+ exit 1
77+ fi
78+
79+ # More robust length check
80+ milestone_count=$( echo " $milestones " | jq ' . | length' 2> /dev/null)
81+ if [ -z " $milestone_count " ] || [ " $milestone_count " = " null" ] || [ " $milestone_count " -eq 0 ]; then
6482 echo -e " ${RED} No milestones found${NC} "
6583 exit 1
6684 fi
@@ -84,16 +102,16 @@ select_milestone() {
84102 # Save terminal state and setup cleanup
85103 saved_tty=" $( stty -g 2> /dev/null) "
86104
87- cleanup () {
105+ cleanup_terminal () {
88106 printf " \033[?25h" # Show cursor
89107 if [ -n " $saved_tty " ]; then
90108 stty " $saved_tty " 2> /dev/null
91109 fi
92110 }
93111
94- trap cleanup EXIT INT TERM
112+ trap cleanup_terminal EXIT INT TERM
95113
96- # Prepare terminal
114+ # Prepare terminal for interactive selection
97115 printf " \033[?25l" # Hide cursor
98116 stty raw -echo 2> /dev/null
99117
@@ -104,7 +122,7 @@ select_milestone() {
104122 while true ; do
105123 # Clear screen and display header
106124 printf " \033[H\033[2J" # Move to top and clear screen
107- printf " ${BLUE} Use arrows to select a milestone (Enter to confirm, 'q' to quit):${NC} \n\n"
125+ printf " Use arrows to select a milestone (Enter to confirm, 'q' to quit):\n\n"
108126
109127 # Display all options with absolute positioning
110128 for (( i= 0 ; i< total; i++ )) ; do
@@ -136,7 +154,7 @@ select_milestone() {
136154 esac
137155 ;;
138156 ' ' |$' \x0a ' ) # Enter
139- cleanup
157+ cleanup_terminal
140158 printf " \033[H\033[2J" # Clear screen
141159 SELECTED_OPTION=" ${options[$selected]} "
142160 MILESTONE=$( echo " ${SELECTED_OPTION} " | cut -d' )' -f1)
@@ -145,7 +163,7 @@ select_milestone() {
145163 return 0
146164 ;;
147165 ' q' |$' \x03 ' ) # q or Ctrl-C
148- cleanup
166+ cleanup_terminal
149167 printf " \033[H\033[2J" # Clear screen
150168 printf " Selection cancelled\n"
151169 return 1
@@ -166,6 +184,7 @@ main() {
166184 select_milestone
167185
168186 if [ $? -ne 0 ]; then
187+ restore_terminal
169188 exit 1
170189 fi
171190
0 commit comments