Skip to content

Commit

Permalink
Removes dependency from output-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesiscoding committed May 2, 2023
1 parent 0890b8f commit 5d340a5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
12 changes: 4 additions & 8 deletions src/Available.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
8114EE4629C0E79A0099578E /* Commands in Frameworks */ = {isa = PBXBuildFile; productRef = 8114EE4529C0E79A0099578E /* Commands */; };
8114EE4829C107860099578E /* Status.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8114EE4729C107860099578E /* Status.swift */; };
813D755429CA4AD600E165BB /* Stub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813D755329CA4AD600E165BB /* Stub.swift */; };
813D755929CA4AE700E165BB /* Commands in Frameworks */ = {isa = PBXBuildFile; productRef = 813D755829CA4AE700E165BB /* Commands */; };
813D755C29CA4B0B00E165BB /* output-cli in CopyFiles */ = {isa = PBXBuildFile; fileRef = 813D755129CA4AD600E165BB /* output-cli */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
813D755E29CA4B4B00E165BB /* Output.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813D755D29CA4B4B00E165BB /* Output.swift */; };
813D756329CA4CC000E165BB /* ANSIAttr.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813D756229CA4CC000E165BB /* ANSIAttr.swift */; };
Expand All @@ -30,6 +29,7 @@
815771DF29C0E3860062A092 /* System.swift in Sources */ = {isa = PBXBuildFile; fileRef = 815771D929C0E3860062A092 /* System.swift */; };
815771E329C0E4310062A092 /* available-cli in CopyFiles */ = {isa = PBXBuildFile; fileRef = 815771C129C0E2500062A092 /* available-cli */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
816BD9D329F9738A00D042B6 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 816BD9D229F9738A00D042B6 /* ArgumentParser */; };
81B619112A01C91600944193 /* Terminal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81B619102A01C91600944193 /* Terminal.swift */; };
81CB173229FAB0D3007FA51E /* Stub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81CB173129FAB0D3007FA51E /* Stub.swift */; };
81CB173429FADDC3007FA51E /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 81CB173329FADDC3007FA51E /* ArgumentParser */; };
81E7632529DC8C3300A5C034 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81E7632429DC8C3300A5C034 /* User.swift */; };
Expand Down Expand Up @@ -108,6 +108,7 @@
815771D829C0E3860062A092 /* Online.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Online.swift; sourceTree = "<group>"; };
815771D929C0E3860062A092 /* System.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = System.swift; sourceTree = "<group>"; };
816BD9D529F97C7B00D042B6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
81B619102A01C91600944193 /* Terminal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Terminal.swift; sourceTree = "<group>"; };
81CB173129FAB0D3007FA51E /* Stub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stub.swift; sourceTree = "<group>"; };
81CB173529FADE60007FA51E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
81E7632429DC8C3300A5C034 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
Expand All @@ -127,7 +128,6 @@
buildActionMask = 2147483647;
files = (
816BD9D329F9738A00D042B6 /* ArgumentParser in Frameworks */,
813D755929CA4AE700E165BB /* Commands in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -211,6 +211,7 @@
813D755D29CA4B4B00E165BB /* Output.swift */,
813D756229CA4CC000E165BB /* ANSIAttr.swift */,
816BD9D529F97C7B00D042B6 /* Info.plist */,
81B619102A01C91600944193 /* Terminal.swift */,
);
path = "output-cli";
sourceTree = "<group>";
Expand Down Expand Up @@ -279,7 +280,6 @@
);
name = "output-cli";
packageProductDependencies = (
813D755829CA4AE700E165BB /* Commands */,
816BD9D229F9738A00D042B6 /* ArgumentParser */,
);
productName = "output-cli";
Expand Down Expand Up @@ -382,6 +382,7 @@
buildActionMask = 2147483647;
files = (
813D756329CA4CC000E165BB /* ANSIAttr.swift in Sources */,
81B619112A01C91600944193 /* Terminal.swift in Sources */,
813D755E29CA4B4B00E165BB /* Output.swift in Sources */,
813D755429CA4AD600E165BB /* Stub.swift in Sources */,
);
Expand Down Expand Up @@ -766,11 +767,6 @@
package = 8114EE4229C0E7940099578E /* XCRemoteSwiftPackageReference "swift-commands" */;
productName = Commands;
};
813D755829CA4AE700E165BB /* Commands */ = {
isa = XCSwiftPackageProductDependency;
package = 8114EE4229C0E7940099578E /* XCRemoteSwiftPackageReference "swift-commands" */;
productName = Commands;
};
816BD9D229F9738A00D042B6 /* ArgumentParser */ = {
isa = XCSwiftPackageProductDependency;
package = 816BD9D129F9738A00D042B6 /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
Expand Down
18 changes: 6 additions & 12 deletions src/output-cli/Output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

import Foundation
import Commands
import ArgumentParser

public enum OutputContext: String, CaseIterable {
Expand All @@ -20,17 +19,12 @@ public class Output {
private let line: String = "----------------------------------------------------------------------------"
public var notifying: Int = 0
public var dateFormat = "yyyy-MM-dd HH:mm:ss"
private lazy var color: Bool = {
let result = Commands.Bash.run("[ -n \"$TERM\" ] && [ \"$TERM\" != \"dumb\" ] && /usr/bin/tput -T$TERM colors")
if result.isFailure {
return false
} else if result.output.isEmpty {
return false
} else if 8 >= Int(result.output) ?? 0 {
return false
}
return true
}()
private var color: Bool {
self.terminal.colors > 0
}
private var terminal: Terminal {
return Terminal()
}

public init(level: Int) {
self.level = level
Expand Down
1 change: 0 additions & 1 deletion src/output-cli/Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import Commands
import ArgumentParser

enum OutputType: String, CaseIterable, EnumerableFlag {
Expand Down
59 changes: 59 additions & 0 deletions src/output-cli/Terminal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Terminal.swift
//
//
// Created by Aaron Jones on 4/28/23.
//

import Foundation

enum TerminalType: String {
case dumb = "dumb"
case xterm = "xterm"
case file = "file"
case other = "other"
}

struct Terminal {
var name: String
var type: TerminalType

var colors: Int {
switch self.name {
case "xterm":
return 8
case "xterm-16color":
return 16
case "xterm-256color":
return 256
default:
return 0
}
}

var columns: Int {
if let colStr = ProcessInfo.processInfo.environment["COLUMNS"] {
if let colInt: Int = Int(colStr) {
return colInt
}
}

return 80
}

init() {
if let term = ProcessInfo.processInfo.environment["TERM"]?.lowercased().trimmingCharacters(in: .whitespacesAndNewlines) {
self.name = term.isEmpty ? "dumb" : term
if term.isEmpty || ["dumb", "cons25", "emacs"].contains(term) {
self.type = .dumb
} else if term.contains("xterm") {
self.type = .xterm
} else {
self.type = .other
}
} else {
self.name = "dumb"
self.type = .dumb
}
}
}

0 comments on commit 5d340a5

Please sign in to comment.