10
10
11
11
from logger .logger import logger
12
12
from utils .style import color_yellow , color_green , color_red , color_yellow_bold
13
+ from utils .ignore import IgnoreMatcher
13
14
from database .database import get_saved_command_run , save_command_run
14
15
from helpers .exceptions .TooDeepRecursionError import TooDeepRecursionError
15
16
from helpers .exceptions .TokenLimitError import TokenLimitError
@@ -340,32 +341,41 @@ def check_if_command_successful(convo, command, cli_response, response, exit_cod
340
341
341
342
return response
342
343
343
- def build_directory_tree (path , prefix = '' , is_root = True , ignore = None ):
344
+ def build_directory_tree (path , prefix = '' , root_path = None ) -> str :
344
345
"""Build the directory tree structure in a simplified format.
345
346
346
- Args:
347
- - path: The starting directory path.
348
- - prefix: Prefix for the current item, used for recursion.
349
- - is_root: Flag to indicate if the current item is the root directory.
350
- - ignore: a list of directories to ignore
351
-
352
- Returns:
353
- - A string representation of the directory tree.
347
+ :param path: The starting directory path.
348
+ :param prefix: Prefix for the current item, used for recursion.
349
+ :param root_path: The root directory path.
350
+ :return: A string representation of the directory tree.
354
351
"""
355
352
output = ""
356
353
indent = ' '
357
354
355
+ if root_path is None :
356
+ root_path = path
357
+
358
+ matcher = IgnoreMatcher (root_path = root_path )
359
+
358
360
if os .path .isdir (path ):
359
- if is_root :
361
+ if root_path == path :
360
362
output += '/'
361
363
else :
362
364
dir_name = os .path .basename (path )
363
365
output += f'{ prefix } /{ dir_name } '
364
366
365
367
# List items in the directory
366
368
items = os .listdir (path )
367
- dirs = [item for item in items if os .path .isdir (os .path .join (path , item )) and item not in ignore ]
368
- files = [item for item in items if os .path .isfile (os .path .join (path , item ))]
369
+ dirs = []
370
+ files = []
371
+ for item in items :
372
+ item_path = os .path .join (path , item )
373
+ if matcher .ignore (item_path ):
374
+ continue
375
+ if os .path .isdir (item_path ):
376
+ dirs .append (item )
377
+ elif os .path .isfile (item_path ):
378
+ files .append (item )
369
379
dirs .sort ()
370
380
files .sort ()
371
381
@@ -374,7 +384,7 @@ def build_directory_tree(path, prefix='', is_root=True, ignore=None):
374
384
for index , dir_item in enumerate (dirs ):
375
385
item_path = os .path .join (path , dir_item )
376
386
new_prefix = prefix + indent # Updated prefix for recursion
377
- output += build_directory_tree (item_path , new_prefix , is_root = False , ignore = ignore )
387
+ output += build_directory_tree (item_path , new_prefix , root_path )
378
388
379
389
if files :
380
390
output += f"{ prefix } { ', ' .join (files )} \n "
@@ -387,36 +397,6 @@ def build_directory_tree(path, prefix='', is_root=True, ignore=None):
387
397
return output
388
398
389
399
390
- def res_for_build_directory_tree (path , files = None ):
391
- return ' - ' + files [os .path .basename (path )].description + ' ' if files and os .path .basename (path ) in files else ''
392
-
393
-
394
- def build_directory_tree_with_descriptions (path , prefix = "" , ignore = None , is_last = False , files = None ):
395
- """Build the directory tree structure in tree-like format.
396
- Args:
397
- - path: The starting directory path.
398
- - prefix: Prefix for the current item, used for recursion.
399
- - ignore: List of directory names to ignore.
400
- - is_last: Flag to indicate if the current item is the last in its parent directory.
401
- Returns:
402
- - A string representation of the directory tree.
403
- """
404
- ignore |= []
405
- if os .path .basename (path ) in ignore :
406
- return ""
407
- output = ""
408
- indent = '| ' if not is_last else ' '
409
- # It's a directory, add its name to the output and then recurse into it
410
- output += prefix + f"|-- { os .path .basename (path )} { res_for_build_directory_tree (path , files )} /\n "
411
- if os .path .isdir (path ):
412
- # List items in the directory
413
- items = os .listdir (path )
414
- for index , item in enumerate (items ):
415
- item_path = os .path .join (path , item )
416
- output += build_directory_tree (item_path , prefix + indent , ignore , index == len (items ) - 1 , files )
417
- return output
418
-
419
-
420
400
def execute_command_and_check_cli_response (convo , command : dict ):
421
401
"""
422
402
Execute a command and check its CLI response.
0 commit comments