forked from dricard/dolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayOneLoggerPlus.swift
184 lines (122 loc) · 5.55 KB
/
DayOneLoggerPlus.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env xcrun swift
import Foundation
/* *********************************
DOLOG Script
********************************* */
/* *********************************
MODIFY THESE PROPERTIES
AS NEEDED
********************************* */
// the journal to log to in Day One
// To use two words insert \\ after the first word, eg: "Daily\\ Log"
let dayOneJournal = "log"
// the default tag(s) to add to all entries. If you don't
// add at least one default tag, you'll have to modify the code below.
// tags *can* have spaces
let defaultTags = ["dolog", "completed tasks"]
// the entry prefix
let entryPrefix = "completed task:"
// Set this to true to quit Terminal after the script runs
// Change "iTerm" to "Terminal" or whatever you use as a replacement to Terminal
let closeTerminal = true
let terminal = "iTerm"
/* ********************************* */
// requires Swift 4.0
//-- get parameter input
// `argument` holds the text entered in Alfred by the user
// I initialize it with an example of something the user could enter
// for testing.
var argument = "-t workflow @Switched to Mailmate as my email client"
#if swift(>=4.0)
if CommandLine.arguments.count > 1 {
argument = CommandLine.arguments[1]
}
#elseif swift(>=1.0)
print("Unsupported version of Swift (<= 4.0) please update to Swift 4.0")
break
#endif
// MARK: - Utilities
func replaceSpaces(in tag: String) -> String {
return tag.replacingOccurrences(of: "_", with: "\\ ")
}
func removeSpecialCharacters(in tag: String) -> String {
// escape special characters
// ! ? $ % # & * ( ) blank tab | ' ; " < > \ ~ ` [ ] { }
var returnedTag = tag
returnedTag = returnedTag.replacingOccurrences(of: "!", with: "\\!")
returnedTag = returnedTag.replacingOccurrences(of: "?", with: "\\?")
returnedTag = returnedTag.replacingOccurrences(of: "$", with: "\\$")
returnedTag = returnedTag.replacingOccurrences(of: "%", with: "\\%")
returnedTag = returnedTag.replacingOccurrences(of: "#", with: "\\#")
returnedTag = returnedTag.replacingOccurrences(of: "&", with: "\\&")
returnedTag = returnedTag.replacingOccurrences(of: "*", with: "\\*")
returnedTag = returnedTag.replacingOccurrences(of: "(", with: "\\(")
returnedTag = returnedTag.replacingOccurrences(of: ")", with: "\\)")
returnedTag = returnedTag.replacingOccurrences(of: "|", with: "\\|")
returnedTag = returnedTag.replacingOccurrences(of: "'", with: "\\'")
returnedTag = returnedTag.replacingOccurrences(of: ";", with: "\\;")
returnedTag = returnedTag.replacingOccurrences(of: "<", with: "\\<")
returnedTag = returnedTag.replacingOccurrences(of: ">", with: "\\>")
returnedTag = returnedTag.replacingOccurrences(of: "\\", with: "\\\\")
returnedTag = returnedTag.replacingOccurrences(of: "~", with: "\\~")
returnedTag = returnedTag.replacingOccurrences(of: "`", with: "\\`")
returnedTag = returnedTag.replacingOccurrences(of: "[", with: "\\[")
returnedTag = returnedTag.replacingOccurrences(of: "]", with: "\\]")
returnedTag = returnedTag.replacingOccurrences(of: "{", with: "\\{")
returnedTag = returnedTag.replacingOccurrences(of: "}", with: "\\}")
return returnedTag
}
// MARK: - Properties
// variable 'task' will hold the completed task passed in
var task = ""
// `outputString` is the result of the script that will be passed to the CLI,
// we initialize it with the Day One CLI command, setting the default journal
// and the default tags.
var outputString = "dayone2 --journal "
// MARK: - Process
// add journal name and default tags
outputString += dayOneJournal + " --tags "
for defaulTag in defaultTags {
let tag = defaulTag.replacingOccurrences(of: " ", with: "\\ ")
outputString += tag + " "
}
// MARK: - Process input
//-- Process tags if present, otherwise just pass the input
if argument.hasPrefix("-t") {
// find the index of the tags separator
if let endOfTags = argument.index(of: "@") {
// Map the tags into an array. The first tag (index 0) will be the tag option marker (-t) and will be
// omitted
let tags = String(argument.prefix(upTo: endOfTags)).split(separator: " ").map{ String($0) }
// Now process the task part to remove the end of tags marker
// get the task part of the input
let taskSection = String(argument.suffix(from: endOfTags))
// find the index of the tags separator in this string (different than above)
let endTagIndex = taskSection.index(of: "@")!
// The task proper starts after the tags separator
let tagIndex = taskSection.index(after: endTagIndex)
// get the task
task = String(taskSection.suffix(from: tagIndex))
// Now we have the task, we then process and format the tags
// Add the tags to the output string separated by spaces
// skipping the first one which is the `-t` marker
for tag in tags.dropFirst() {
// first we process underscores (_) in tags to replace them with escaped spaces so they're
// treated as a single tag
var processedTag = replaceSpaces(in: tag)
processedTag = removeSpecialCharacters(in: processedTag)
// add this processed tag to the output string
outputString += processedTag + " "
}
} else {
// user forgot the '@' separator so just pass the input string (task) as received
task = argument
}
} else {
// no tags, so just pass the input string (task) as received
task = argument
}
// Add the task to the output string (enclosed in quotes to prevent the CLI to interpret special characters)
outputString += " -- new" + " \"" + entryPrefix + " " + task + "\""
// pass the result of the script, we suppress the newline character in the output
print(outputString, terminator:"")