Skip to content

Commit

Permalink
[fix]: line & column parameters (#17)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
austincondiff authored Dec 7, 2022
2 parents 9278039 + ca76213 commit eed72c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
identifier_name:
allowed_symbols: ['_']

large_tuple:
warning: 5

excluded:
- .build
- .git
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

## Installation

### Download

Download the universal binary from the latest release, extract the zip and move it to `/usr/local/bin/`.

### Build locally

```sh
Expand Down
43 changes: 35 additions & 8 deletions Sources/CodeEditCLI/Open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@ extension CodeEditCLI {
)

@Argument(
help: "The path of a file/folder to open.",
help: """
The path of a file/folder to open.
When opening files, line and column numbers can be appended: `index.html:42:10`
""",
completion: .file()
)
private var path: String?

@Option(name: .shortAndLong, help: "The line number to open a file at. Optional.")
private var line: Int?

@Option(name: .shortAndLong, help: "The column to open a file at. Optional.")
private var column: Int?

func run() throws {
let task = Process()

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

if let path {

let (path, line, column) = try extractLineColumn(path)
let openURL = try absolutePath(path, for: task)

// open CodeEdit using the url scheme
Expand All @@ -57,5 +54,35 @@ extension CodeEditCLI {
}
return url
}

private func extractLineColumn(_ path: String) throws -> (path: String, line: Int?, column: Int?) {

// split the string at `:` to get line and column numbers
let components = path.split(separator: ":")

// set path to only the first component
guard let first = components.first else {
throw CLIError.invalidFileURL
}
let path = String(first)

// switch on the number of components
switch components.count {
case 1: // no line or column number provided
return (path, nil, nil)

case 2: // only line number provided
guard let row = Int(components[1]) else { throw CLIError.invalidFileURL }
return (path, row, nil)

case 3: // line and column number provided
guard let row = Int(components[1]),
let column = Int(components[2]) else { throw CLIError.invalidFileURL }
return (path, row, column)

default: // any other case throw an error since this is invalid
throw CLIError.invalidFileURL
}
}
}
}
3 changes: 2 additions & 1 deletion Sources/CodeEditCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
// ##################################################
// This needs to be changed prior to every release!
// ##################################################
let CLI_VERSION = "0.0.3"
let CLI_VERSION = "0.0.5"

struct CodeEditCLI: ParsableCommand {
static let configuration = CommandConfiguration(
Expand All @@ -30,6 +30,7 @@ struct CodeEditCLI: ParsableCommand {

enum CLIError: Error {
case invalidWorkingDirectory
case invalidFileURL
}
}

Expand Down

0 comments on commit eed72c0

Please sign in to comment.