-
Notifications
You must be signed in to change notification settings - Fork 1
/
bt.nu
executable file
·51 lines (45 loc) · 1.48 KB
/
bt.nu
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
#!/home/marchall/.cargo/bin/nu
# List bluetooth devices and connection status
export def list [] {
let connected = (connected)
bluetoothctl devices |
split row -r '\n' |
each {|it| $it | parse "{device} {address} {name}"} |
flatten |
select address name |
insert connected {|x| (not ($connected | is-empty)) and ($x.address == (connected | get address.0))}
}
# Output connected device
export def connected [] {
bluetoothctl devices Connected |
str trim |
parse "{device} {address} {name}"
}
# Wrapper for `bluetoothctl disconnect`
export def disconnect [] {
if (connected | length) > 0 {
let output = (bluetoothctl disconnect | complete)
if $output.exit_code != 0 {
print "Disconnect failed"
}
}
}
# Display prompt to connect to device
export def connect [] {
let choice = (list | input list -d name)
if ($choice | is-empty) { return }
let connected = (connected)
if $choice.address == ($connected | get address) and (not ($connected | is-empty)) {
print $"Already connected to ($choice.name)... exiting"
return
}
print $"Connecting to: ($choice.name) \(($choice.address)\)"
let connection_status = (bluetoothctl connect $choice.address | complete)
if $connection_status.exit_code != 0 {
print "Connection unsuccessful. Output:"
print $"\n(ansi red_bold)($connection_status.stdout)"
}
}
export def main [] {
list
}