Skip to content

Commit

Permalink
Add CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Mazanov <[email protected]>
  • Loading branch information
melonamin committed Oct 1, 2023
1 parent e23d9d6 commit eb53bbd
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 42 deletions.
233 changes: 201 additions & 32 deletions Esse/Esse.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
}
},
{
"identity" : "sttextview",
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/STTextView",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "2f26c8a53db4014555bcf5e4bfa1896b285c4dcc",
"version" : "0.8.14"
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>EsseCommandLine.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>FA853DCA2A33AD71007D6889</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
10 changes: 8 additions & 2 deletions Esse/Esse/Esse.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array/>
<array>
<string>iCloud.com.ameba.esse</string>
<string>iCloud.co.ameba.esse</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array/>
<array>
<string>iCloud.com.ameba.esse</string>
<string>iCloud.co.ameba.esse</string>
</array>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
Expand Down
94 changes: 94 additions & 0 deletions Esse/Esse/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.ameba.esse.open</string>
<key>CFBundleURLSchemes</key>
<array>
<string>esse</string>
</array>
</dict>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Esse</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundleGetInfoString</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>218</string>
<key>INIntentsSupported</key>
<array>
<string>RunFunctionIntent</string>
<string>RunFunctionFileIntent</string>
</array>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 Ameba Labs. All rights reserved.</string>
<key>NSServices</key>
<array>
<dict>
<key>NSKeyEquivalent</key>
<dict>
<key>default</key>
<string>~@^E</string>
</dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Send to Esse</string>
</dict>
<key>NSMessage</key>
<string>textServiceHandler</string>
<key>NSPortName</key>
<string>Esse</string>
<key>NSRequiredContext</key>
<dict/>
<key>NSSendTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
</dict>
</array>
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.ameba.esse</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>Esse</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
<key>NSUserActivityTypes</key>
<array>
<string>RunFunctionFileIntent</string>
<string>RunFunctionIntent</string>
<string>TextFunctionIntent</string>
<string>TextIntentIntent</string>
</array>
</dict>
</plist>
8 changes: 4 additions & 4 deletions Esse/Esse/macOS/Main/MacMainView.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#if os(macOS)
import DSFQuickActionBar
import STTextViewUI
import STTextView
#endif
import EsseCore
import SwiftUI
Expand Down Expand Up @@ -43,12 +41,14 @@ struct MacMainView: View {
}
}
.onChange(of: selectedFunction) { _, value in
if isMultiEditorMode, let value {
guard let value else {return}
if isMultiEditorMode {
selectedFunctions.append(value)
} else {
editableText = value?.run(editableText) ?? ""
editableText = value.run(editableText)
self.fireFunctionTrigger()
}
selectedFunction = nil
}
.onChange(of: selectedFunctions) { _, value in
if isMultiEditorMode {
Expand Down
5 changes: 5 additions & 0 deletions Esse/EsseCommandLine/EsseCommandLine.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
108 changes: 108 additions & 0 deletions Esse/EsseCommandLine/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import Foundation
import ArgumentParser
import EsseCore

let storage = Storage.sharedInstance

struct Esse: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Swiss army knife of text transformation.",
version: "Esse version 2020.5",
subcommands: [List.self]
)

@Option(name: .short, help: "Transformation(s) to execute.")
var transformations: String?

@Option(name: .short, help: "Text to transform.")
var input: String?

mutating func run() throws {
var functions: [TextFunction] = []
transformations?.split(separator: ",").forEach { id in
guard let f = storage.pAllFunctions.first(where: {$0.id.lowercased().contains(id.lowercased())}) else {return}
functions.append(f)
}

if let text = input {
print(runFunctions(text: text, functions: functions), terminator:"")
return
}

let input = FileHandle.standardInput
if let text = String(bytes: input.availableData, encoding: .utf8) {
print(runFunctions(text: text, functions: functions), terminator:"")
return
}
}

func runFunctions(text: String, functions: [TextFunction]) -> String{
let actions = functions.compactMap {$0.actions}.flatMap {$0}
return actions.reduce(text) {$1($0)}
}
}

extension Esse {
struct List: ParsableCommand, Decodable {
static var configuration =
CommandConfiguration(abstract: "Print list of available transformations.")

@Flag(help: "Print only id.")
var onlyid: Int

@Flag(help: "Alfred compatible output.")
var alfred: Int

@Argument(help: "Transfromation.")
var transformation: String?


mutating func run() {
storage.reloadExternalFunctions()
var functions = storage.pAllFunctions
if let transformation = transformation {
functions = storage.pAllFunctions.filter({$0.id.lowercased().contains(transformation.lowercased())})
}

if functions.isEmpty {
print("No functions found.")
return
}

if onlyid != 0{
functions.forEach{print($0.id)}
return
}

if alfred != 0 {
let alfredItems = functions.map{$0.alfred}
let alfredOutput = TextFunction.AlfredOutput(items: alfredItems)

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
guard let jsonData = try? encoder.encode(alfredOutput), let str = String(data: jsonData, encoding: .utf8) else {return}
print(str)
return
}
functions.forEach{print($0.description)}
}
}
}

extension TextFunction {
struct AlfredOutput: Codable {
let items: [AlfredItem]
}
struct AlfredItem: Codable {
let uid: String
let title: String
let subtitle: String
var match: String
var arg: String
var autocomplete: String
}
var alfred: AlfredItem {
return AlfredItem(uid: id, title: title, subtitle: desc, match: "\(title) \(desc)", arg: id, autocomplete: title)
}
}
Esse.main()

0 comments on commit eb53bbd

Please sign in to comment.