Make suitable SSH key listing easier to distinguish
Marco Ricci

Marco Ricci commited on 2024-11-26 00:31:20
Zeige 1 geänderte Dateien mit 7 Einfügungen und 6 Löschungen.


On the one hand, truncate and align the listing as two columns, not
three, by combining key type and (truncated) key data into one column.
For heterogenous lists with different key types, this nicely sets off
the comment column (which the user can change to help distinguish the
keys) from the key data (which the user cannot change).

On the other hand, if truncating the key data for the display, truncate
the *front* of the data, not the back.  For homogenous lists, this
generally leads to better distinguishable key listings: the front
contains information common to all keys (the wire-encoded key type), but
the back contains key-specific information (for RSA, Ed25519 and Ed448
keys at least).
... ...
@@ -48,7 +48,7 @@ __version__ = dpp.__version__
48 48
 __all__ = ('derivepassphrase',)
49 49
 
50 50
 PROG_NAME = 'derivepassphrase'
51
-KEY_DISPLAY_LENGTH = 30
51
+KEY_DISPLAY_LENGTH = 50
52 52
 
53 53
 # Error messages
54 54
 _INVALID_VAULT_CONFIG = 'Invalid vault config'
... ...
@@ -640,13 +640,14 @@ def _select_ssh_key(
640 640
     for key, comment in suitable_keys:
641 641
         keytype = unstring_prefix(key)[0].decode('ASCII')
642 642
         key_str = base64.standard_b64encode(key).decode('ASCII')
643
-        key_prefix = (
644
-            key_str
645
-            if len(key_str) < KEY_DISPLAY_LENGTH + len('...')
646
-            else key_str[:KEY_DISPLAY_LENGTH] + '...'
643
+        remaining_key_display_length = KEY_DISPLAY_LENGTH - 1 - len(keytype)
644
+        key_extract = min(
645
+            key_str,
646
+            '...' + key_str[-remaining_key_display_length:],
647
+            key=len,
647 648
         )
648 649
         comment_str = comment.decode('UTF-8', errors='replace')
649
-        key_listing.append(f'{keytype} {key_prefix} {comment_str}')
650
+        key_listing.append(f'{keytype} {key_extract}  {comment_str}')
650 651
     choice = _prompt_for_selection(
651 652
         key_listing,
652 653
         heading='Suitable SSH keys:',
653 654