Skip to content

Commit

Permalink
Add summary.
Browse files Browse the repository at this point in the history
  • Loading branch information
wieczorek1990 committed Aug 31, 2023
1 parent 7395567 commit cedb88d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Sources/stree/stree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public struct stree {
private var showAll: Bool = false
private var maximumLevel: Int?
private var path: String?
private var summary: Bool = false
private var maximumLevelReached: Int?

public func printVersion() {
print("stree@\(VERSION)")
Expand All @@ -30,20 +32,33 @@ public struct stree {
print("""
stree -- directory tree viewing program.
Usage:
stree [--version|--help|-a|-L] path
stree [--version|--help|-a|-L|-s] path
Arguments:
--version print version,
--help print help,
-a include hidden files,
-L limit maximum level of directory tree depth.
-s print summary
Examples:
stree .
stree -a .
stree -L 1 .
stree -s .
"""
)
}

public func printSummary() {
if !self.summary {
return
}
if self.maximumLevelReached != nil {
print("Maximum level reached: \(self.maximumLevelReached!).")
} else {
print("Was not traversing.")
}
}

public init(_ arguments: Array<String>) {
var skipNext: Bool = false
var pathFound: Bool = false
Expand Down Expand Up @@ -86,6 +101,8 @@ public struct stree {
}

self.maximumLevel = maximumLevel
case "-s":
self.summary = true
default:
if pathFound {
break
Expand All @@ -97,7 +114,8 @@ public struct stree {
}
}

public func printPathStart(_ path: String) throws {
public mutating func printPathStart(_ path: String) throws {
self.maximumLevelReached = 0
let boldPath = path.bold()
print(boldPath)
try self.printPath(path, 0)
Expand All @@ -116,11 +134,12 @@ public struct stree {
item[item.index(item.startIndex, offsetBy: 0)] == self.hiddenPrefix
}

public func printPath(_ path: String, _ level: Int) throws {
public mutating func printPath(_ path: String, _ level: Int) throws {
if !self.canTraverse(level) {
return
}

self.maximumLevelReached = level
let nextLevel = level + 1
let items = try FileManager.default.contentsOfDirectory(atPath: path)

Expand Down Expand Up @@ -148,9 +167,10 @@ public struct stree {
}

public static func main() throws {
let application = stree(CommandLine.arguments)
var application = stree(CommandLine.arguments)
if let path = application.path {
try application.printPathStart(path)
application.printSummary()
} else {
application.printHelp()
}
Expand Down
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ run -L 2 test/
run -a -L 0 test/
run -a -L 1 test/
run -a -L 2 test/
run -s test/
run -s -L 0 test/
run --help -s test/

make clean
rm -r test/

0 comments on commit cedb88d

Please sign in to comment.