Skip to content

Commit 28b8a1b

Browse files
exurdwillswire
andauthored
feat: add reverse option (#17)
* Teemoji.swift: add --reverse option * fix: update pr-title-check.yml * test: reverse option should be good. unsure on the second part of the test, but since the prediction is always one space apart from the input string, it does work... --------- Co-authored-by: William Walker <w_walker@icloud.com>
1 parent 7118935 commit 28b8a1b

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

.github/workflows/pr-title-check.yml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@ name: PR Title Check
22

33
on:
44
pull_request:
5-
branches:
6-
- main
5+
branches: [main]
6+
types: [milestoned, opened, edited, synchronize]
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
711

812
jobs:
9-
check-title:
13+
run:
1014
runs-on: ubuntu-latest
11-
1215
steps:
13-
- name: Validate PR Title
16+
- name: Setup Node.js
17+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
18+
19+
- name: Install commitlint
20+
run: |
21+
npm install --save-dev @commitlint/config-conventional@19.6.0
22+
npm install --save-dev @commitlint/cli@19.6.1
23+
24+
- name: Lint PR title
25+
env:
26+
pull_request_title: ${{ github.event.pull_request.title }}
1427
run: |
15-
TITLE="${{ github.event.pull_request.title }}"
16-
echo "PR Title: ${TITLE}"
17-
# Adjust the regex as needed to match your conventional commit format.
18-
if ! echo "${TITLE}" | grep -Eq "^(feat|fix|docs|style|refactor|perf|test|chore)(\([\w\-]+\))?: .+"; then
19-
echo "Error: PR title does not match the conventional commit format."
20-
exit 1
21-
fi
28+
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
29+
# shellcheck disable=SC2154
30+
echo "$pull_request_title" | npx commitlint

Sources/Teemoji.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
struct Teemoji {
1111
struct ArgumentOptions {
1212
let append: Bool
13+
let reverse: Bool
1314
let fileURLs: [URL]
1415
let shouldExit: Bool
1516
}
@@ -20,6 +21,7 @@ struct Teemoji {
2021

2122
let appendFlagIndex = arguments.firstIndex(where: { $0 == "-a" || $0 == "--append" })
2223
let ignoreSigIntIndex = arguments.firstIndex(where: { $0 == "-i" })
24+
let reverseEmojiIndex = arguments.firstIndex(where: { $0 == "-r" || $0 == "--reverse" })
2325
let helpFlagIndex = arguments.firstIndex(where: { $0 == "-h" || $0 == "--help" })
2426

2527
let append = (appendFlagIndex != nil)
@@ -32,13 +34,19 @@ struct Teemoji {
3234
arguments.remove(at: index)
3335
}
3436

37+
let reverse = (reverseEmojiIndex != nil)
38+
if let index = reverseEmojiIndex {
39+
arguments.remove(at: index)
40+
}
41+
3542
if helpFlagIndex != nil {
3643
printUsage()
3744
exit(EXIT_SUCCESS)
3845
}
3946

4047
return ArgumentOptions(
4148
append: append,
49+
reverse: reverse,
4250
fileURLs: arguments.map { URL(fileURLWithPath: $0) },
4351
shouldExit: helpFlagIndex != nil
4452
)
@@ -135,7 +143,11 @@ struct Teemoji {
135143
predictionLabel = ""
136144
}
137145

138-
let outputLine = "\(predictionLabel) \(line)\n"
146+
var outputLine = "\(predictionLabel) \(line)\n"
147+
if options.reverse {
148+
outputLine = "\(line) \(predictionLabel)\n"
149+
}
150+
139151
if fputs(outputLine, stdout) < 0 {
140152
exitStatus = 1
141153
}
@@ -150,12 +162,15 @@ struct Teemoji {
150162
/// Prints usage, matching FreeBSD tee's style.
151163
static func printUsage() {
152164
let usage = """
153-
usage: teemoji [-ai] [file ...]
165+
usage: teemoji [-air] [file ...]
154166
155-
Reads from standard input, writes to standard output and specified files, prepending an emoji
156-
inferred by a Core ML model to each line. Options:
167+
Reads from standard input, writes to standard output and specified files,
168+
prepending an emoji inferred by a Core ML model to each line.
169+
170+
Options:
157171
-a\tAppend to the given file(s), do not overwrite
158172
-i\tIgnore SIGINT
173+
-r\tReverses where the emoji will be placed (at end of line)
159174
-h\tDisplay help (non-standard extension)
160175
"""
161176
print(usage)

Tests/TeemojiTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ final class TeemojiTests: XCTestCase {
8686
XCTAssertTrue(fileContent.contains("Appending Content"))
8787
}
8888

89+
func testReverseOption() throws {
90+
let input = "Hello World"
91+
let output = try runTeemoji(inputs: [input], arguments: ["-r"])
92+
93+
// Verify the output starts with the input text
94+
XCTAssertTrue(
95+
output.starts(with: input),
96+
"With the reverse option enabled, the output should begin with the original input text.")
97+
98+
// Verify the output has changed
99+
XCTAssertFalse(
100+
output.starts(with: "\(input)\n"),
101+
"Output should not end directly with the input, it should have an emoji suffix.")
102+
}
103+
89104
func testIgnoreSigInt() throws {
90105
// Simulate sending SIGINT using a process group or similar to make sure it's ignored
91106
// Note: This test requires specific setup and may not be fully feasible in some test environments

0 commit comments

Comments
 (0)