Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List on browser #23

Merged
merged 6 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions Sources/Commands/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,46 @@ import SwiftyTextTable
import Rainbow
import Models

#if canImport(AppKit)
import AppKit
#endif

struct List: ParsableCommand {
static let configuration = CommandConfiguration(commandName: Constants.commandName, abstract: Constants.abstract, discussion: Constants.discussion)

#if canImport(AppKit)
@Flag(help: "List the bookmarklets on Safari browser")
var onSafari = false
#endif
}

extension List {
func run() throws {
var w = winsize()
let bookmarklets: [(uuid: String, title: String, url: String)] =
try Folder(path: Constants.hondanaDirURL).createSubfolderIfNeeded(at: "Bookmarklets/")
let jsFiles = try Folder(path: Constants.hondanaDirURL)
.createSubfolderIfNeeded(at: "Bookmarklets/")
.files
.filter { $0.extension == "js" }
guard !jsFiles.isEmpty else {
print("No bookmarklet exist")
return
}
#if canImport(AppKit)
guard !onSafari else {
let htmlFile = try Utils.generateHTML(from: jsFiles)
try NSWorkspace.shared.open([htmlFile.url], withApplicationAt: URL(string: "file://~/Applications/Safari.app")!, configuration: [:])
return
}
#endif
var winsize = winsize()
let bookmarklets: [(uuid: String, title: String, url: String)] =
try jsFiles
.map { (uuid: $0.nameExcludingExtension.components(separatedBy: "+").first!,
title: $0.nameExcludingExtension.components(separatedBy: "+")[1],
url: String(try $0.readAsString(encodedAs: .utf8)
.withoutJSPrefix.minified.prefix(
ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w) == 0 ?
Int(w.ws_col) - 30 : 30)))
ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &winsize) == 0 ?
Int(winsize.ws_col) - 30 : 30)))
}
guard !bookmarklets.isEmpty else {
print("No bookmarklet exist")
return
}
let titleCol = TextTableColumn(header: "Title".bold)
let urlCol = TextTableColumn(header: "URL".bold)
var table = TextTable(columns: [titleCol, urlCol], header: "Bookmarklets".bold)
Expand Down
28 changes: 28 additions & 0 deletions Sources/Commands/Utils/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Files
import Foundation
enum Utils {
static func generateHTML(from jsFiles: [File]) throws -> File {
let rawHTMLstring = """
<!doctype html
<html>
<title>Bookmarklets</title>
<h1>Bookmarklets</h1>
""" +
(try jsFiles
.map { jsFile in
return aTag(url: try jsFile.readAsString(encodedAs: .utf8).withJSPrefix.minified,
title: jsFile.nameExcludingExtension.components(separatedBy: "+")[1])
}
.joined(separator: "\n"))
+ """
</html>
"""
return try Folder(path: "~")
.createSubfolderIfNeeded(at: ".Hondana")
.createFile(at: "bookmarklets.html", contents: rawHTMLstring.data(using: .utf8))
}

private static func aTag(url: String, title: String) -> String {
return "<a href=\"\(url)\">\(title)</a>"
}
}
2 changes: 1 addition & 1 deletion Sources/Commands/Utils/Version.swift
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let version = "0.0.6-d"
let version = "0.0.6"
43 changes: 38 additions & 5 deletions Tests/HondanaTests/HondanaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ final class HondanaTests: XCTestCase {
}

func testVersionFlag() throws {
try AssertExecuteCommand(command: "hondana --version", expected: "0.0.6-d")
try AssertExecuteCommand(command: "hondana --version", expected: "0.0.6")
}

func testListWithoutJSFiles() throws {
// swiftlint:disable line_length
func testList() throws {
// No Bookmarklets
try AssertExecuteCommand(command: "hondana list", expected: """
No bookmarklet exist
""")
}

// swiftlint:disable line_length
func testListWithJSFiles() throws {
// 1 Bookmarklet
let bookmarksHtmlPath = try File(path: #file).parent!.parent!.url.appendingPathComponent("Fixtures/+test.js")
try Folder(path: "~/.Hondana/Bookmarklets/").createFile(at: "+test.js", contents: try Data(contentsOf: bookmarksHtmlPath))
try AssertExecuteCommand(command: "hondana list", expected: """
Expand All @@ -43,6 +43,39 @@ final class HondanaTests: XCTestCase {
try File(path: "~/.Hondana/Bookmarklets/+test.js").delete()
}

func testHelp() throws {
#if canImport(AppKit)
try AssertExecuteCommand(command: "hondana list --help", expected: """
OVERVIEW: `hondana list` lists every bookmarklet present in
`~/.Hondana/Bookmarklets/`

`hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it,
and outputs the filtered result in the table.

USAGE: hondana list [--on-safari]

OPTIONS:
--on-safari List the bookmarklets on Safari browser
--version Show the version.
-h, --help Show help information.
""")
#else
try AssertExecuteCommand(command: "hondana list --help", expected: """
OVERVIEW: `hondana list` lists every bookmarklet present in
`~/.Hondana/Bookmarklets/`

`hondana list` accesses to `~/.Hondana/Bookmarklets/`, reads the files in it,
and outputs the filtered result in the table.

USAGE: hondana list

OPTIONS:
--version Show the version.
-h, --help Show help information.
""")
#endif
}

/// Returns path to the built products directory.
var productsDirectory: URL {
#if os(macOS)
Expand Down