8
8
import Foundation
9
9
import Cocoa
10
10
import Rainbow
11
+ import Utility
12
+ import Basic
13
+
14
+ private enum copyFormat {
15
+ case markdown
16
+ case html
17
+ case plain
18
+ }
11
19
12
20
// A class managing the commmand line interface for this project
13
21
public class CommandLineInterface {
14
- public let argc : Int32
15
- public let argv : [ String ]
22
+ public let argc = CommandLine . argc
23
+ public let argv = CommandLine . arguments
16
24
private let api = ImgurAPI ( )
17
25
26
+ private let parser : ArgumentParser
27
+ private let useMDFormat : OptionArgument < Bool >
28
+ private let useHTMLFormat : OptionArgument < Bool >
29
+ private let doNotCopy : OptionArgument < Bool >
30
+
18
31
public init ( ) {
19
- self . argc = CommandLine . argc
20
- self . argv = CommandLine . arguments
32
+ // Init the argument parser and register flags
33
+ let overview = " clip2imgur is a simple CLI that uploads your image in clipboard " +
34
+ " to Imgur. "
35
+ self . parser = ArgumentParser ( usage: " <flags> " ,
36
+ overview: overview)
37
+ self . useMDFormat = self . parser. add (
38
+ option: " --markdown " ,
39
+ shortName: " -m " ,
40
+ kind: Bool . self,
41
+ usage: " Copy the image url in Markdown format "
42
+ )
43
+
44
+ self . useHTMLFormat = self . parser. add (
45
+ option: " --html " ,
46
+ shortName: " -t " ,
47
+ kind: Bool . self,
48
+ usage: " Copy the image url in HTML format "
49
+ )
50
+
51
+ self . doNotCopy = self . parser. add (
52
+ option: " --nocopy " ,
53
+ shortName: " -n " ,
54
+ kind: Bool . self,
55
+ usage: " Do not copy the image url after submitting "
56
+ )
21
57
}
22
58
23
- // The main logic is implemented here
59
+ // The app main logic is implemented here
24
60
public func run( ) {
25
- let cliImage = ClipboardImage ( )
26
- let url = self . postImage ( from: cliImage. getClipboardImageBase64 ( ) )
27
- copyToClipboard ( from: url)
61
+ do {
62
+ // Parse the arguments
63
+ let parsedArguments = try parser. parse ( Array ( self . argv. dropFirst ( ) ) )
64
+
65
+ // Post the user's image
66
+ let cliImage = ClipboardImage ( )
67
+ let url = self . postImage ( from: cliImage. getClipboardImageBase64 ( ) )
68
+
69
+ // Decide how to copy the returned url
70
+ if ( parsedArguments. get ( self . doNotCopy) == true ) {
71
+ return
72
+ } else if ( parsedArguments. get ( self . useHTMLFormat) == true ) {
73
+ copyToClipboard ( from: url, using: . html)
74
+ } else if ( parsedArguments. get ( self . useMDFormat) == true ) {
75
+ copyToClipboard ( from: url, using: . markdown)
76
+ } else {
77
+ copyToClipboard ( from: url, using: . plain)
78
+ }
79
+ } catch let error {
80
+ printError ( error. localizedDescription)
81
+ self . parser. printUsage ( on: stdoutStream)
82
+ }
28
83
print ( " The image url is coppied to your clipboard. " . blue. bold)
29
84
}
30
85
@@ -38,7 +93,7 @@ public class CommandLineInterface{
38
93
var response : String ?
39
94
while ( true ) {
40
95
print ( " [Enter 'yes' to start authorization, enter 'no' to post anonymously] " )
41
- print ( " > " . blink , terminator: " " )
96
+ print ( " > " . bold , terminator: " " )
42
97
response = readLine ( )
43
98
let legalResponses = [ " yes " , " no " , " \' yes \' " , " \' no \' " , " y " , " n " ]
44
99
if ( response != nil && legalResponses. contains ( response!) ) {
@@ -75,9 +130,19 @@ public func printError(_ errorMessage: String){
75
130
fputs ( " Error: \( errorMessage) \n " . red. bold, stderr)
76
131
}
77
132
78
- // Copy the string to user's clipboard
79
- private func copyToClipboard( from str : String ) {
133
+ // Copy the url to user's clipboard
134
+ private func copyToClipboard( from url : String , using format : copyFormat ) {
80
135
let clipboard = NSPasteboard . general
81
136
clipboard. declareTypes ( [ . string] , owner: nil )
82
- clipboard. setString ( str, forType: . string)
137
+ var formatedURL : String
138
+ // Copy the url in the specified format
139
+ switch format {
140
+ case . html:
141
+ formatedURL = " <img src= \" \( url) \" > "
142
+ case . markdown:
143
+ formatedURL = "  ) "
144
+ case . plain:
145
+ formatedURL = url
146
+ }
147
+ clipboard. setString ( formatedURL, forType: . string)
83
148
}
0 commit comments