Repository: pwr-Solaar/Solaar

Issue:
CLI: solaar show <ID> does not work correctly
```
solaar version 1.1.9+git2025-4160b0e
```

also:

```
solaar version 1.1.10
```

<details>

```
$ cat ~/.config/solaar/config.yaml 
- 1.1.9+git2025-4160b0e
- _NAME: MX Master 3 Wireless Mouse
  _absent: [hi-res-scroll, lowres-scroll-mode, onboard_profiles, report_rate, pointer_speed, speed-change, backlight, backlight-timed, fn-swap, persistent-remappable-keys,
    disable-keyboard-keys, crown-smooth, divert-crown, divert-gkeys, m-key-leds, mr-key-led, multiplatform, gesture2-gestures, gesture2-divert, gesture2-params,
    sidetone, equalizer, adc_power_management]
  _battery: 4096
  _modelId: B02340820000
  _sensitive: {dpi: false, hires-scroll-mode: ignore, hires-smooth-invert: ignore, hires-smooth-resolution: ignore, scroll-ratchet: false}
  _unitId: 253FAA66
  change-host: null
  divert-keys: {82: 0, 83: 0, 86: 0, 195: 0, 196: 0}
  dpi: 1000
  hires-scroll-mode: false
  hires-smooth-invert: false
  hires-smooth-resolution: true
  reprogrammable-keys: {80: 80, 81: 81, 82: 82, 83: 83, 86: 86, 195: 195, 196: 196}
  scroll-ratchet: 2
  smart-shift: 10
  thumb-scroll-invert: false
  thumb-scroll-mode: false
- _NAME: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
  _absent: [hi-res-scroll, lowres-scroll-mode, hires-smooth-invert, hires-smooth-resolution, hires-scroll-mode, scroll-ratchet, smart-shift, thumb-scroll-invert,
    thumb-scroll-mode, report_rate, pointer_speed, dpi, speed-change, backlight, backlight-timed, fn-swap, reprogrammable-keys, persistent-remappable-keys,
    disable-keyboard-keys, crown-smooth, divert-crown, multiplatform, gesture2-gestures, gesture2-divert, gesture2-params, sidetone, equalizer, adc_power_management]
  _battery: 4097
  _modelId: B35F408EC343
  _sensitive: {m-key-leds: true, mr-key-led: false, onboard_profiles: false}
  _unitId: A74780FA
  change-host: null
  divert-gkeys: false
  divert-keys: {209: 0, 210: 0}
  m-key-leds: {1: false, 2: false, 4: false}
  mr-key-led: false
  onboard_profiles: 1
- _NAME: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
  _modelId: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
  _sensitive: {hires-scroll-mode: ignore, hires-smooth-invert: ignore, hires-smooth-resolution: ignore}
  _unitId: '64050300'
  change-host: null
  divert-gkeys: false
  divert-keys: {209: 0, 210: 0}
  m-key-leds: {1: false, 2: false, 4: false}
  mr-key-led: false
  onboard_profiles: 1
- _NAME: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
  _modelId: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
  _sensitive: {hires-scroll-mode: ignore, hires-smooth-invert: ignore, hires-smooth-resolution: ignore}
  _unitId: 070F0000
  divert-gkeys: false
  m-key-leds: {1: false, 2: false, 4: false}
  mr-key-led: false
  onboard_profiles: 1
```
</details> 

**Describe the bug**
When specifying a Device ID (e.g. "1"), instead of querying the device that is shown with ID 1, it uses this character in a search of device *names*.

**To Reproduce**
Steps to reproduce the behavior:
1. Run `solaar show all` and note the two devices with IDs "1" and "2":

```
solaar version 1.1.9+git2025-4160b0e

USB and Bluetooth Devices

  1: MX Master 3 Wireless Mouse
     Device path  : /dev/hidraw4

...

  2: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
     Device path  : /dev/hidraw5

...
```

2. Attempt to query device "1" with `solaar show 1`:

```
$ solaar show 1
...
  255: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
     Device path  : /dev/hidraw5
...
```

It actually queried device 2 (but called it "255"), with the name "G915...". I believe it is string-matching the "1" on the name, as "915" has a 1, so it has returned the wrong device.

3. Test this hypothesis, try device "9":

```
$ solaar show 9
...
  255: G915 TKL LIGHTSPEED Wireless RGB Mechanical Gaming Keyboard
...
```

That is not expected.

4. Now try to show device 2:

```
$ solaar show 2
solaar: error: Traceback (most recent call last):
  File "/usr/share/solaar/lib/solaar/cli/__init__.py", line 209, in run
    m.run(c, args, _find_receiver, _find_device)
  File "/usr/share/solaar/lib/solaar/cli/show.py", line 316, in run
    raise Exception("no device found matching '%s'" % device_name)
Exception: no device found matching '2'
```

I would have expected this to return the second device, the G915, but it seems to find nothing.

I see the same issue with 1.1.10.



Discussion:
This aspect of `solaar show` has not been updated for USB and Bluetooth devices.  The number shown for them is not the number that `solaar show` is looking for so no number is found and the matching then looks in the device name.  Something should be done here, but it's not obvious what.

Resolving patch:
diff --git a/lib/solaar/cli/show.py b/lib/solaar/cli/show.py
--- a/lib/solaar/cli/show.py
+++ b/lib/solaar/cli/show.py
@@ -89,7 +89,10 @@ def _print_device(dev, num=None):
         print('  %s: Device not found' % num or dev.number)
         return
 
-    print('  %d: %s' % (num or dev.number, dev.name))
+    if num or dev.number < 8:
+        print('  %d: %s' % (num or dev.number, dev.name))
+    else:
+        print('%s' % dev.name)
     print('     Device path  :', dev.path)
     if dev.wpid:
         print('     WPID         : %s' % dev.wpid)
@@ -290,7 +293,6 @@ def run(devices, args, find_receiver, find_device):
     device_name = args.device.lower()
 
     if device_name == 'all':
-        dev_num = 1
         for d in devices:
             if isinstance(d, _receiver.Receiver):
                 _print_receiver(d)
@@ -298,17 +300,14 @@ def run(devices, args, find_receiver, find_device):
                 if count:
                     for dev in d:
                         print('')
-                        _print_device(dev)
+                        _print_device(dev, dev.number)
                         count -= 1
                         if not count:
                             break
                 print('')
             else:
-                if dev_num == 1:
-                    print('USB and Bluetooth Devices')
                 print('')
-                _print_device(d, num=dev_num)
-                dev_num += 1
+                _print_device(d)
         return
 
     dev = find_receiver(devices, device_name)


Test patch: