@@ -49,7 +49,7 @@ def parse_args() -> argparse.Namespace:
4949 action = "store_true" ,
5050 help = "Enable spaced layout"
5151 )
52-
52+
5353 parser .add_argument (
5454 "--not-spaced" ,
5555 action = "store_true" ,
@@ -126,10 +126,17 @@ def main() -> int:
126126 # Try to get default username from config
127127 username = config_manager .get_default_username ()
128128 if not username :
129- username = _prompt_username ()
130- if not username :
131- print ("Error: Username is required" , file = sys .stderr )
132- return 1
129+ # Fall back to authenticated user
130+ try :
131+ username = fetcher .get_authenticated_user ()
132+ # Save as default for future use
133+ config_manager .set_default_username (username )
134+ config_manager .save ()
135+ except Exception :
136+ username = _prompt_username ()
137+ if not username :
138+ print ("Error: Username is required" , file = sys .stderr )
139+ return 1
133140
134141 # Fetch data (with or without cache)
135142 try :
@@ -147,7 +154,8 @@ def main() -> int:
147154 stale_stats = cache_manager .get_stale_cached_stats (username )
148155 if stale_user_data is not None and stale_stats is not None :
149156 # Display stale cache immediately
150- formatter .display (username , stale_user_data , stale_stats , spaced = spaced )
157+ formatter .display (username , stale_user_data ,
158+ stale_stats , spaced = spaced )
151159 print ("\n 🔄 Refreshing data in background..." ,
152160 file = sys .stderr )
153161
@@ -195,8 +203,8 @@ def _prompt_username() -> Optional[str]:
195203
196204def _initialize_gitfetch (config_manager : ConfigManager ) -> bool :
197205 """
198- Initialize gitfetch by creating config directory and asking for
199- default username .
206+ Initialize gitfetch by creating config directory and setting
207+ the authenticated user as default .
200208
201209 Args:
202210 config_manager: ConfigManager instance
@@ -205,14 +213,14 @@ def _initialize_gitfetch(config_manager: ConfigManager) -> bool:
205213 True if initialization succeeded, False otherwise
206214 """
207215 try :
208- # Prompt for default username
209- print ( "Please enter your default GitHub username." )
210- print ( "(You can override this later by passing a username as "
211- "an argument)" )
212- username = input ( " \n GitHub username: " ). strip ( )
213-
214- if not username :
215- print ("Error: Username cannot be empty" , file = sys . stderr )
216+ # Try to get authenticated user from GitHub CLI
217+ fetcher = GitHubFetcher ( )
218+ try :
219+ username = fetcher . get_authenticated_user ( )
220+ print ( f"Using authenticated GitHub user: { username } " )
221+ except Exception as e :
222+ print ( f"Could not get authenticated user: { e } " )
223+ print ("Please ensure GitHub CLI is authenticated with: gh auth login" )
216224 return False
217225
218226 # Save configuration
@@ -221,9 +229,6 @@ def _initialize_gitfetch(config_manager: ConfigManager) -> bool:
221229
222230 return True
223231
224- except (KeyboardInterrupt , EOFError ):
225- print ("\n Initialization cancelled." )
226- return False
227232 except Exception as e :
228233 print (f"Error during initialization: { e } " , file = sys .stderr )
229234 return False
0 commit comments