Skip to content

Commit

Permalink
Add version. Move bold to String.
Browse files Browse the repository at this point in the history
  • Loading branch information
wieczorek1990 committed May 29, 2023
1 parent 535c9d4 commit 7395567
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
31 changes: 21 additions & 10 deletions Sources/stree/stree.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Foundation

extension String {
public func bold() -> String{
"\u{001B}[1;30m\(self)\u{001B}[0;30m"
}
}

extension URL {
var isDirectory: Bool {
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
Expand All @@ -8,22 +14,28 @@ extension URL {

@main
public struct stree {
let VERSION = "1.1.0"
var nesting: String = " "
var hiddenPrefix: Character = "."

private var showAll: Bool = false
private var maximumLevel: Int?
private var path: String?

public func printVersion() {
print("stree@\(VERSION)")
}

public func printHelp() {
print("""
stree -- directory tree viewing program.
Usage:
stree [--help|-a|-L] path
stree [--version|--help|-a|-L] path
Arguments:
--help print help
-a include hidden files
-L limit maximum level of directory tree depth
--version print version,
--help print help,
-a include hidden files,
-L limit maximum level of directory tree depth.
Examples:
stree .
stree -a .
Expand All @@ -46,6 +58,9 @@ public struct stree {
}

switch argument {
case "--version":
self.printVersion()
exit(0)
case "--help":
self.printHelp()
exit(0)
Expand Down Expand Up @@ -83,7 +98,7 @@ public struct stree {
}

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

public func bold(_ item: String) -> String{
"\u{001B}[1;30m\(item)\u{001B}[0;30m"
}

public func printPath(_ path: String, _ level: Int) throws {
if !self.canTraverse(level) {
return
Expand All @@ -123,7 +134,7 @@ public struct stree {
let itemPrefix = String(repeating: self.nesting, count: nextLevel)

if url.isDirectory {
let boldItem = self.bold(item)
let boldItem = item.bold()
print("\(itemPrefix) \(boldItem)")

if !self.canTraverse(nextLevel) {
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mkdir test/dir/.hidden_dir/

make build

run --version
run --help
run test/
run -a test/
Expand Down

0 comments on commit 7395567

Please sign in to comment.