Skip to content

Commit eed72c0

Browse files
[fix]: line & column parameters (#17)
As proposed in #16 the syntax to open a file at specified line and column has changed: ```sh # Prior codeedit-cli index.html -l 42 -c 4 # New codeedit-cli index.html:42:4 ```
2 parents 9278039 + ca76213 commit eed72c0

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

.swiftlint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
identifier_name:
33
allowed_symbols: ['_']
44

5+
large_tuple:
6+
warning: 5
7+
58
excluded:
69
- .build
710
- .git

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
## Installation
1818

19+
### Download
20+
21+
Download the universal binary from the latest release, extract the zip and move it to `/usr/local/bin/`.
22+
1923
### Build locally
2024

2125
```sh

Sources/CodeEditCLI/Open.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,22 @@ extension CodeEditCLI {
1616
)
1717

1818
@Argument(
19-
help: "The path of a file/folder to open.",
19+
help: """
20+
The path of a file/folder to open.
21+
When opening files, line and column numbers can be appended: `index.html:42:10`
22+
""",
2023
completion: .file()
2124
)
2225
private var path: String?
2326

24-
@Option(name: .shortAndLong, help: "The line number to open a file at. Optional.")
25-
private var line: Int?
26-
27-
@Option(name: .shortAndLong, help: "The column to open a file at. Optional.")
28-
private var column: Int?
29-
3027
func run() throws {
3128
let task = Process()
3229

3330
// use the `open` cli as the executable
3431
task.launchPath = "/usr/bin/open"
3532

3633
if let path {
37-
34+
let (path, line, column) = try extractLineColumn(path)
3835
let openURL = try absolutePath(path, for: task)
3936

4037
// open CodeEdit using the url scheme
@@ -57,5 +54,35 @@ extension CodeEditCLI {
5754
}
5855
return url
5956
}
57+
58+
private func extractLineColumn(_ path: String) throws -> (path: String, line: Int?, column: Int?) {
59+
60+
// split the string at `:` to get line and column numbers
61+
let components = path.split(separator: ":")
62+
63+
// set path to only the first component
64+
guard let first = components.first else {
65+
throw CLIError.invalidFileURL
66+
}
67+
let path = String(first)
68+
69+
// switch on the number of components
70+
switch components.count {
71+
case 1: // no line or column number provided
72+
return (path, nil, nil)
73+
74+
case 2: // only line number provided
75+
guard let row = Int(components[1]) else { throw CLIError.invalidFileURL }
76+
return (path, row, nil)
77+
78+
case 3: // line and column number provided
79+
guard let row = Int(components[1]),
80+
let column = Int(components[2]) else { throw CLIError.invalidFileURL }
81+
return (path, row, column)
82+
83+
default: // any other case throw an error since this is invalid
84+
throw CLIError.invalidFileURL
85+
}
86+
}
6087
}
6188
}

Sources/CodeEditCLI/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
// ##################################################
1212
// This needs to be changed prior to every release!
1313
// ##################################################
14-
let CLI_VERSION = "0.0.3"
14+
let CLI_VERSION = "0.0.5"
1515

1616
struct CodeEditCLI: ParsableCommand {
1717
static let configuration = CommandConfiguration(
@@ -30,6 +30,7 @@ struct CodeEditCLI: ParsableCommand {
3030

3131
enum CLIError: Error {
3232
case invalidWorkingDirectory
33+
case invalidFileURL
3334
}
3435
}
3536

0 commit comments

Comments
 (0)