Skip to content

Commit

Permalink
Align options (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
mssalvatore authored Jan 31, 2025
1 parent 634488f commit 30c8ee3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
17 changes: 15 additions & 2 deletions linodecli/configuration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"""

import configparser
import math
import os
import webbrowser
from functools import partial
from typing import Any, Callable, List, Optional

LEGACY_CONFIG_NAME = ".linode-cli"
Expand Down Expand Up @@ -142,13 +144,14 @@ def _default_thing_input(
exists = current_value is not None

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

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

for ind, thing in enumerate(things):
print(f" {ind + idx_offset} - {thing}")
print(f"{pad(ind + idx_offset)} - {thing}")
print()

while True:
Expand Down Expand Up @@ -184,6 +187,16 @@ def _default_thing_input(
return things[choice_idx]


def _pad_index(idx: int, total: int) -> str:
# NOTE: The implementation of this function could be less opaque if we're
# willing to say, "There will never be a case where total > X, because no
# one could examine and choose from that many options."
max_padding = math.floor(math.log10(total)) + 1
num_spaces = max_padding - math.floor(math.log10(idx))

return " " * num_spaces + str(idx)


def _default_text_input(
ask: str,
default: Optional[str] = None,
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,47 @@ def test_default_thing_input_out_of_range(self, monkeypatch):

assert result == "foo"

def test_default_thing_spacing(self, monkeypatch):
stdout_buf = io.StringIO()
monkeypatch.setattr("sys.stdin", io.StringIO("1\n"))

with contextlib.redirect_stdout(stdout_buf):
_default_thing_input(
"foo\n", [*range(1, 10_001)], "prompt text", "error text"
)

output_lines = stdout_buf.getvalue().splitlines()

assert output_lines[3] == " 1 - 1"
assert output_lines[11] == " 9 - 9"
assert output_lines[12] == " 10 - 10"
assert output_lines[101] == " 99 - 99"
assert output_lines[102] == " 100 - 100"
assert output_lines[1001] == " 999 - 999"
assert output_lines[1002] == " 1000 - 1000"
assert output_lines[10_001] == " 9999 - 9999"
assert output_lines[10_002] == " 10000 - 10000"

def test_default_thing_spacing_with_current(self, monkeypatch):
stdout_buf = io.StringIO()
monkeypatch.setattr("sys.stdin", io.StringIO("1\n"))

with contextlib.redirect_stdout(stdout_buf):
_default_thing_input(
"foo\n",
[*range(1, 10)],
"prompt text",
"error text",
current_value="foo",
)

output_lines = stdout_buf.getvalue().splitlines()

print(output_lines)
assert output_lines[4] == " 2 - 1"
assert output_lines[11] == " 9 - 8"
assert output_lines[12] == " 10 - 9"

def test_default_text_input_optional(self, monkeypatch):
# No value specified
stdout_buf = io.StringIO()
Expand Down

0 comments on commit 30c8ee3

Please sign in to comment.