Skip to content

Commit

Permalink
Merge pull request #3 from p-x9/feature/file-tree-depth
Browse files Browse the repository at this point in the history
Add `--depth` option to `file tree` command
  • Loading branch information
p-x9 authored Sep 11, 2023
2 parents b29f7f8 + 71f1d9f commit 9ee6b50
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,25 @@ app info
(lldb) file tree -h
usage: tree
[-h]
[-b]
[-l]
[--documents]
[--tmp TMP]
[--tmp]
[--depth DEPTH]
optional arguments:
-h, --help
show this help message and exit
-b, --bundle
bundle directory (default: False)
-l, --library
library directory (default: False)
--documents
documents directory (default: False)
--tmp TMP
tmp directory (default: None)
--tmp
tmp directory (default: False)
--depth DEPTH
Maximum depth to be displayed (default: None)
```

#### Example
Expand Down
19 changes: 14 additions & 5 deletions src/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def parse_args(args: list[str]) -> argparse.Namespace:
tree_command.add_argument("-l", "--library", action="store_true", help="library directory")
tree_command.add_argument("--documents", action="store_true", help="documents directory")
tree_command.add_argument("--tmp", action="store_true", help="tmp directory")
tree_command.add_argument("--depth", type=int, help="Maximum depth to be displayed")

open_command = subparsers.add_parser("open",
help="Open directory with Finder (Simulator Only)",
Expand Down Expand Up @@ -68,16 +69,24 @@ def tree(args: argparse.Namespace, debugger: lldb.SBDebugger, result: lldb.SBCom
script_ret = subprocess.run(f"cat {dir_name}/swift/file.swift", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

script = script_ret.stdout

depth = 'nil'
if args.depth is not None:
try:
depth = str(int(args.depth))
except ValueError:
pass

if args.bundle:
script += "listFilesInDirectory(Bundle.main.bundleURL)"
script += f"listFilesInDirectory(Bundle.main.bundleURL, depth: {depth})"
elif args.library:
script += "listFilesInDirectory(FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!)"
script += f"listFilesInDirectory(FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!, depth: {depth})"
elif args.documents:
script += "listFilesInDirectory(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!)"
script += f"listFilesInDirectory(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!, depth: {depth})"
elif args.tmp:
script += "listFilesInDirectory(FileManager.default.temporaryDirectory)"
script += f"listFilesInDirectory(FileManager.default.temporaryDirectory, depth: {depth})"
elif args.path:
script += f"listFilesInDirectory(URL(fileURLWithPath: {args.path}))"
script += f"listFilesInDirectory(URL(fileURLWithPath: {args.path}), depth: {depth})"

_ = util.exp_script(
debugger,
Expand Down
9 changes: 7 additions & 2 deletions src/swift/file.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import Foundation

func listFilesInDirectory(_ directoryURL: URL, indentation: String = "") {
func listFilesInDirectory(_ directoryURL: URL, indentation: String = "", depth: Int? = nil) {
if indentation.isEmpty {
let isDirectory = (try? directoryURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
print("\(isDirectory ? "πŸ“ " : "πŸ“„ ")" + directoryURL.lastPathComponent)
}

let currentDepth = indentation.replacingOccurrences(of: "β”‚", with: " ").count / 3
if let depth, currentDepth >= depth {
return
}

do {
let fileManager = FileManager.default
let contents = try fileManager.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: nil, options: [])
Expand All @@ -20,7 +25,7 @@ func listFilesInDirectory(_ directoryURL: URL, indentation: String = "") {
fileHierarchy += "πŸ“ \(url.lastPathComponent)"
print(fileHierarchy)

listFilesInDirectory(url, indentation: indentation + (isLast ? " " : "β”‚ "))
listFilesInDirectory(url, indentation: indentation + (isLast ? " " : "β”‚ "), depth: depth)
} else {
fileHierarchy += "πŸ“„ \(url.lastPathComponent)"
print(fileHierarchy)
Expand Down

0 comments on commit 9ee6b50

Please sign in to comment.