Skip to content

Commit 30c8ee3

Browse files
authored
Align options (#706)
1 parent 634488f commit 30c8ee3

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

linodecli/configuration/helpers.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
"""
44

55
import configparser
6+
import math
67
import os
78
import webbrowser
9+
from functools import partial
810
from typing import Any, Callable, List, Optional
911

1012
LEGACY_CONFIG_NAME = ".linode-cli"
@@ -142,13 +144,14 @@ def _default_thing_input(
142144
exists = current_value is not None
143145

144146
idx_offset = int(exists) + 1
147+
pad = partial(_pad_index, total=len(things) + idx_offset)
145148

146149
# If there is a current value, users should have the option to clear it
147150
if exists:
148-
print(" 1 - No Default")
151+
print(f"{pad(1)} - No Default")
149152

150153
for ind, thing in enumerate(things):
151-
print(f" {ind + idx_offset} - {thing}")
154+
print(f"{pad(ind + idx_offset)} - {thing}")
152155
print()
153156

154157
while True:
@@ -184,6 +187,16 @@ def _default_thing_input(
184187
return things[choice_idx]
185188

186189

190+
def _pad_index(idx: int, total: int) -> str:
191+
# NOTE: The implementation of this function could be less opaque if we're
192+
# willing to say, "There will never be a case where total > X, because no
193+
# one could examine and choose from that many options."
194+
max_padding = math.floor(math.log10(total)) + 1
195+
num_spaces = max_padding - math.floor(math.log10(idx))
196+
197+
return " " * num_spaces + str(idx)
198+
199+
187200
def _default_text_input(
188201
ask: str,
189202
default: Optional[str] = None,

tests/unit/test_configuration.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,47 @@ def test_default_thing_input_out_of_range(self, monkeypatch):
519519

520520
assert result == "foo"
521521

522+
def test_default_thing_spacing(self, monkeypatch):
523+
stdout_buf = io.StringIO()
524+
monkeypatch.setattr("sys.stdin", io.StringIO("1\n"))
525+
526+
with contextlib.redirect_stdout(stdout_buf):
527+
_default_thing_input(
528+
"foo\n", [*range(1, 10_001)], "prompt text", "error text"
529+
)
530+
531+
output_lines = stdout_buf.getvalue().splitlines()
532+
533+
assert output_lines[3] == " 1 - 1"
534+
assert output_lines[11] == " 9 - 9"
535+
assert output_lines[12] == " 10 - 10"
536+
assert output_lines[101] == " 99 - 99"
537+
assert output_lines[102] == " 100 - 100"
538+
assert output_lines[1001] == " 999 - 999"
539+
assert output_lines[1002] == " 1000 - 1000"
540+
assert output_lines[10_001] == " 9999 - 9999"
541+
assert output_lines[10_002] == " 10000 - 10000"
542+
543+
def test_default_thing_spacing_with_current(self, monkeypatch):
544+
stdout_buf = io.StringIO()
545+
monkeypatch.setattr("sys.stdin", io.StringIO("1\n"))
546+
547+
with contextlib.redirect_stdout(stdout_buf):
548+
_default_thing_input(
549+
"foo\n",
550+
[*range(1, 10)],
551+
"prompt text",
552+
"error text",
553+
current_value="foo",
554+
)
555+
556+
output_lines = stdout_buf.getvalue().splitlines()
557+
558+
print(output_lines)
559+
assert output_lines[4] == " 2 - 1"
560+
assert output_lines[11] == " 9 - 8"
561+
assert output_lines[12] == " 10 - 9"
562+
522563
def test_default_text_input_optional(self, monkeypatch):
523564
# No value specified
524565
stdout_buf = io.StringIO()

0 commit comments

Comments
 (0)