mirror of
https://github.com/ChristianLight/tutor.git
synced 2025-01-06 07:30:40 +00:00
refactor: move format_table function to utils
This commit is contained in:
parent
d91a49e65d
commit
40e5c8da6b
@ -56,17 +56,3 @@ class PluginsTests(unittest.TestCase, TestCommandMixin):
|
||||
self.assertEqual(
|
||||
["all", "alba"], plugins_commands.PluginName(allow_all=True).get_names("al")
|
||||
)
|
||||
|
||||
def test_format_table(self) -> None:
|
||||
rows: t.List[t.Tuple[str, ...]] = [
|
||||
("a", "xyz", "value 1"),
|
||||
("abc", "x", "value 12345"),
|
||||
]
|
||||
formatted = plugins_commands.format_table(rows, separator=" ")
|
||||
self.assertEqual(
|
||||
"""
|
||||
a xyz value 1
|
||||
abc x value 12345
|
||||
""".strip(),
|
||||
formatted,
|
||||
)
|
||||
|
@ -3,6 +3,7 @@ import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from io import StringIO
|
||||
from typing import List, Tuple
|
||||
from unittest.mock import MagicMock, mock_open, patch
|
||||
|
||||
from tutor import exceptions, utils
|
||||
@ -239,3 +240,17 @@ class UtilsTests(unittest.TestCase):
|
||||
self.assertFalse(utils.is_http("/home/user/"))
|
||||
self.assertFalse(utils.is_http("home/user/"))
|
||||
self.assertFalse(utils.is_http("http-home/user/"))
|
||||
|
||||
def test_format_table(self) -> None:
|
||||
rows: List[Tuple[str, ...]] = [
|
||||
("a", "xyz", "value 1"),
|
||||
("abc", "x", "value 12345"),
|
||||
]
|
||||
formatted = utils.format_table(rows, separator=" ")
|
||||
self.assertEqual(
|
||||
"""
|
||||
a xyz value 1
|
||||
abc x value 12345
|
||||
""".strip(),
|
||||
formatted,
|
||||
)
|
||||
|
@ -128,7 +128,7 @@ def list_command(show_enabled_only: bool) -> None:
|
||||
(plugin_info or "").replace("\n", " "),
|
||||
)
|
||||
)
|
||||
fmt.echo(format_table(plugins_table))
|
||||
fmt.echo(utils.format_table(plugins_table))
|
||||
|
||||
|
||||
@click.command(help="Enable a plugin")
|
||||
@ -302,7 +302,7 @@ def search(pattern: str) -> None:
|
||||
plugin.short_description,
|
||||
)
|
||||
)
|
||||
print(format_table(results))
|
||||
print(utils.format_table(results))
|
||||
|
||||
|
||||
@click.command()
|
||||
@ -408,34 +408,6 @@ def index_remove(context: Context, url: str) -> None:
|
||||
fmt.echo_alert("Plugin index not present")
|
||||
|
||||
|
||||
def format_table(rows: t.List[t.Tuple[str, ...]], separator: str = "\t") -> str:
|
||||
"""
|
||||
Format a list of values as a tab-separated table. Column sizes are determined such
|
||||
that row values are vertically aligned.
|
||||
"""
|
||||
formatted = ""
|
||||
if not rows:
|
||||
return formatted
|
||||
columns_count = len(rows[0])
|
||||
# Determine each column size
|
||||
col_sizes = [1] * columns_count
|
||||
for row in rows:
|
||||
for c, value in enumerate(row):
|
||||
col_sizes[c] = max(col_sizes[c], len(value))
|
||||
# Print all values
|
||||
for r, row in enumerate(rows):
|
||||
for c, value in enumerate(row):
|
||||
if c < len(col_sizes) - 1:
|
||||
formatted += f"{value:{col_sizes[c]}}{separator}"
|
||||
else:
|
||||
# The last column is not left-justified
|
||||
formatted += f"{value}"
|
||||
if r < len(rows) - 1:
|
||||
# Append EOL at all lines but the last one
|
||||
formatted += "\n"
|
||||
return formatted
|
||||
|
||||
|
||||
index_command.add_command(index_add)
|
||||
index_command.add_command(index_list)
|
||||
index_command.add_command(index_remove)
|
||||
|
@ -345,3 +345,31 @@ def is_http(url: str) -> bool:
|
||||
Basic test to check whether a string is a web URL. Use only for basic use cases.
|
||||
"""
|
||||
return re.match(r"^https?://", url) is not None
|
||||
|
||||
|
||||
def format_table(rows: List[Tuple[str, ...]], separator: str = "\t") -> str:
|
||||
"""
|
||||
Format a list of values as a tab-separated table. Column sizes are determined such
|
||||
that row values are vertically aligned.
|
||||
"""
|
||||
formatted = ""
|
||||
if not rows:
|
||||
return formatted
|
||||
columns_count = len(rows[0])
|
||||
# Determine each column size
|
||||
col_sizes = [1] * columns_count
|
||||
for row in rows:
|
||||
for c, value in enumerate(row):
|
||||
col_sizes[c] = max(col_sizes[c], len(value))
|
||||
# Print all values
|
||||
for r, row in enumerate(rows):
|
||||
for c, value in enumerate(row):
|
||||
if c < len(col_sizes) - 1:
|
||||
formatted += f"{value:{col_sizes[c]}}{separator}"
|
||||
else:
|
||||
# The last column is not left-justified
|
||||
formatted += f"{value}"
|
||||
if r < len(rows) - 1:
|
||||
# Append EOL at all lines but the last one
|
||||
formatted += "\n"
|
||||
return formatted
|
||||
|
Loading…
Reference in New Issue
Block a user