Skip to content

Commit 6860e79

Browse files
LostExcaliburLostExcalibur
andauthored
[ssh-completion] Support blocks that define more than one Host (#1223)
It is a valid ssh config to have a block defining properties for more than one host, such as : ``` Host home home-git other-on-same-addr HostName example.com ``` This correctly parses such configs. I also took the liberty to fix the annoying "HostName" text that was appearing in front of the actual hostname in the completion, because `find` was not called with `-n` and thus the splitting was done incorrectly. Co-authored-by: LostExcalibur <lostexcalibur-admin@proton.me>
1 parent f704022 commit 6860e79

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

custom-completions/ssh/ssh-completions.nu

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export extern "ssh" [
3333
]
3434

3535
module ssh-completion-utils {
36-
def extract-host []: list<string> -> record<name: string, addr: string> {
36+
def extract-host []: list<string> -> record<names: list<string>, addr: string> {
3737
# Host is a list of lines, like:
3838
# ╭───┬──────────────────────────────╮
3939
# │ 0 │ Host quanweb │
@@ -43,22 +43,22 @@ module ssh-completion-utils {
4343
# │ 4 │ │
4444
# ╰───┴──────────────────────────────╯
4545
let host = $in
46-
let $first_line = try { $host | first | str trim } catch { null }
46+
let first_line = try { $host | first | str trim } catch { null }
4747
# Don't accept blocks like "Host *"
4848
if ($first_line | is-empty) or '*' in $first_line {
4949
null
5050
} else {
51-
let name = $first_line | split row -r '\s+' | get 1
51+
let names = $first_line | split row -r '\s+' | reject 0
5252
# May not contain hostname
53-
match ($host | slice 1.. | find -ir '^\s*Hostname\s') {
53+
match ($host | slice 1.. | find -n -ir '^\s*Hostname\s') {
5454
[] => null,
55-
$addr => { name: $name, addr: ($addr | str trim | split row -n 2 -r '\s+' | get 1) }
55+
$addr => { names: $names, addr: ($addr | str trim | split row -n 2 -r '\s+' | get 1) }
5656
}
5757
}
5858
}
5959

6060
# Process a SSH config file
61-
export def process []: string -> record<hosts: list<record<name: string, addr: string>>, includes: list<string>> {
61+
export def process []: string -> record<hosts: table<names: list<string>, addr: string>, includes: list<string>> {
6262
let lines = $in | lines
6363
# Get 'Include' lines
6464
let include_lines = $lines | find -n -ir '^Include\s' | str trim | each { $in | split row -n 2 -r '\s+' | get 1 | str trim -c '"'}
@@ -105,5 +105,5 @@ def "nu-complete ssh-host" [] {
105105
})
106106

107107
let hosts = $first_result.hosts ++ $included_hosts
108-
$hosts | each { {value: $in.name, description: $in.addr } }
108+
$hosts | each { {value: $in.names, description: $in.addr } } | flatten
109109
}

0 commit comments

Comments
 (0)