Skip to content

Commit

Permalink
feat: --include-dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Nunamaker committed Jul 27, 2024
1 parent 1aaf670 commit a4365a6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 275 deletions.
265 changes: 0 additions & 265 deletions stringify.py

This file was deleted.

9 changes: 6 additions & 3 deletions stringify/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def main():
print("Error: rsync is not installed on this system. Please install rsync and try again.")
return

parser = argparse.ArgumentParser(description="Stringify code with rsync and manage presets.")
# Use allow_abbrev=False because rsync options can look like abbreviated stringify options
parser = argparse.ArgumentParser(description="Stringify code with rsync and manage presets.", allow_abbrev=False)
parser.add_argument("-p", "--preset", help="Use a saved preset")
parser.add_argument("-sp", "--save-preset", nargs=2, metavar=("NAME", "ARGS"), help="Save a new preset")
parser.add_argument("-sap", "--save-as-preset", metavar="NAME", help="Save the current command as a preset")
Expand All @@ -26,6 +27,8 @@ def main():
parser.add_argument("-pl", "--preview-length", type=int, metavar="N",
help="Show only the first N lines of each file")
parser.add_argument("-s", "--summary", action="store_true", help="Print a summary including a tree of files")
parser.add_argument("-id", "--include-dirs", action="store_true",
help="Include empty directories in output and summary")

args, unknown_args = parser.parse_known_args()

Expand Down Expand Up @@ -67,7 +70,7 @@ def main():
rsync_args = interactive_mode(rsync_args)

file_list = run_rsync(rsync_args)
result = gather_code(file_list, args.preview_length)
result = gather_code(file_list, args.preview_length, args.include_dirs)

if args.no_clipboard:
print(result)
Expand All @@ -77,7 +80,7 @@ def main():
if args.summary:
print(f"Gathered {len(file_list)} files using rsync options: {' '.join(rsync_args)}")
print("\nFile tree:")
print_tree(file_list)
print_tree(file_list, include_dirs=args.include_dirs)

if args.save_as_preset:
presets[args.save_as_preset] = rsync_args
Expand Down
22 changes: 15 additions & 7 deletions stringify/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@

def load_presets():
if os.path.exists(PRESETS_FILE):
with open(PRESETS_FILE, 'r') as f:
return json.load(f)
try:
with open(PRESETS_FILE, 'r') as f:
return json.load(f)
except json.JSONDecodeError:
logger.error(f"Invalid JSON in {PRESETS_FILE}. Using empty presets.")
print(f"Warning: Invalid JSON in {PRESETS_FILE}. Using empty presets.")
except Exception as e:
logger.error(f"Error reading {PRESETS_FILE}: {e}")
print(f"Warning: Error reading {PRESETS_FILE}. Using empty presets.")
return {}


Expand Down Expand Up @@ -74,7 +81,7 @@ def is_binary(file_path):
return False


def gather_code(file_list, preview_length=None):
def gather_code(file_list, preview_length=None, include_dirs=False):
result = ""
for file_path in file_list:
full_path = file_path
Expand All @@ -93,7 +100,7 @@ def gather_code(file_list, preview_length=None):
result += f"--- {file_path} ---\n\n"
except Exception as e:
logger.error(f"Error reading {file_path}: {e}")
else:
elif include_dirs and os.path.isdir(full_path):
result += f"--- {file_path} ---\n[Directory]\n\n"
return result

Expand Down Expand Up @@ -137,7 +144,7 @@ def interactive_mode(initial_args):
return args


def print_tree(file_list):
def print_tree(file_list, include_dirs=False):
tree = {}
for file_path in file_list:
parts = file_path.split(os.sep)
Expand All @@ -146,7 +153,8 @@ def print_tree(file_list):
if part not in current:
current[part] = {}
current = current[part]
current[parts[-1]] = {}
if include_dirs or os.path.isfile(file_path):
current[parts[-1]] = {}

def print_tree_recursive(node, prefix=""):
items = list(node.items())
Expand All @@ -157,7 +165,7 @@ def print_tree_recursive(node, prefix=""):
else:
print(f"{prefix}├── {name}")
new_prefix = prefix + "│ "
if subtree:
if subtree or (include_dirs and os.path.isdir(os.path.join(*node.keys(), name))):
print_tree_recursive(subtree, new_prefix)

print_tree_recursive(tree)
Expand Down

0 comments on commit a4365a6

Please sign in to comment.