Skip to content

Commit 4564cfd

Browse files
committed
More refactor
1 parent 2e80662 commit 4564cfd

File tree

4 files changed

+62
-35
lines changed

4 files changed

+62
-35
lines changed

src/mcpm/commands/edit.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from rich.prompt import Confirm
1313

1414
from mcpm.clients.client_registry import ClientRegistry
15-
from mcpm.utils.display import print_server_config
15+
from mcpm.utils.display import print_client_error, print_error, print_server_config
1616

1717
console = Console()
1818

@@ -39,16 +39,15 @@ def edit():
3939

4040
# Check if client is supported
4141
if client_manager is None:
42-
console.print("[bold red]Error:[/] Unsupported active client")
43-
console.print("Please switch to a supported client using 'mcpm client <client-name>'")
42+
print_client_error(client_name)
4443
return
4544

4645
# Get the client config file path
4746
config_path = client_manager.config_path
4847

4948
# Check if the client is installed
5049
if not client_manager.is_client_installed():
51-
console.print(f"[bold red]Error:[/] {client_name} installation not detected.")
50+
print_error(f"{client_name} installation not detected.")
5251
return
5352

5453
# Check if config file exists
@@ -87,7 +86,7 @@ def edit():
8786
console.print("[green]Successfully created config file![/]\n")
8887
config_exists = True
8988
except Exception as e:
90-
console.print(f"[bold red]Error creating config file:[/] {str(e)}")
89+
print_error("Error creating config file", str(e))
9190
return
9291

9392
# Show the current configuration if it exists
@@ -117,7 +116,7 @@ def edit():
117116
console.print("[yellow]Warning: Config file contains invalid JSON[/]")
118117

119118
except Exception as e:
120-
console.print(f"[bold red]Error reading config file:[/] {str(e)}")
119+
print_error("Error reading config file", str(e))
121120

122121
# Prompt to edit if file exists
123122
should_edit = False
@@ -137,5 +136,5 @@ def edit():
137136

138137
console.print(f"[italic]After editing, {client_name} must be restarted for changes to take effect.[/]")
139138
except Exception as e:
140-
console.print(f"[bold red]Error opening editor:[/] {str(e)}")
139+
print_error("Error opening editor", str(e))
141140
console.print(f"You can manually edit the file at: {config_path}")

src/mcpm/commands/search.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import click
66
from rich.console import Console
7-
from rich.table import Table
87

8+
from mcpm.utils.display import print_error, print_servers_table
99
from mcpm.utils.repository import RepositoryManager
1010

1111
console = Console()
@@ -50,37 +50,13 @@ def search(query, detailed=False):
5050
if detailed:
5151
_display_detailed_results(servers)
5252
else:
53-
_display_table_results(servers)
53+
print_servers_table(servers)
5454

5555
# Show summary count
5656
console.print(f"\n[green]Found {len(servers)} server(s) matching search criteria[/]")
5757

5858
except Exception as e:
59-
console.print(f"[bold red]Error searching for servers:[/] {str(e)}")
60-
61-
62-
def _display_table_results(servers):
63-
"""Display search results in a compact table"""
64-
table = Table(show_header=True, header_style="bold")
65-
table.add_column("Name", style="cyan")
66-
table.add_column("Description")
67-
table.add_column("Categories/Tags", overflow="fold")
68-
69-
for server in sorted(servers, key=lambda s: s["name"]):
70-
# Get server data
71-
name = server["name"]
72-
display_name = server.get("display_name", name)
73-
description = server.get("description", "No description")
74-
75-
# Build categories and tags
76-
categories = server.get("categories", [])
77-
tags = server.get("tags", [])
78-
meta_info = ", ".join([f"[dim]{c}[/]" for c in categories] + [f"[dim]{t}[/]" for t in tags])
79-
80-
# Add row to table
81-
table.add_row(f"{display_name}\n[dim]({name})[/]", description, meta_info)
82-
83-
console.print(table)
59+
print_error("Error searching for servers", str(e))
8460

8561

8662
def _display_detailed_results(servers):

src/mcpm/utils/display.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from rich.console import Console
66
from rich.markup import escape
7+
from rich.table import Table
78

89
console = Console()
910

@@ -43,3 +44,53 @@ def print_server_config(server_name, server_info, is_stashed=False):
4344

4445
# Add a separator line between servers
4546
console.print(" " + "-" * 50)
47+
48+
49+
def print_servers_table(servers):
50+
"""Display a formatted table of server information.
51+
52+
Args:
53+
servers: List of server dictionaries containing server information
54+
"""
55+
table = Table(show_header=True, header_style="bold")
56+
table.add_column("Name", style="cyan")
57+
table.add_column("Description")
58+
table.add_column("Categories/Tags", overflow="fold")
59+
60+
for server in sorted(servers, key=lambda s: s["name"]):
61+
# Get server data
62+
name = server["name"]
63+
display_name = server.get("display_name", name)
64+
description = server.get("description", "No description")
65+
66+
# Build categories and tags
67+
categories = server.get("categories", [])
68+
tags = server.get("tags", [])
69+
meta_info = ", ".join([f"[dim]{c}[/]" for c in categories] + [f"[dim]{t}[/]" for t in tags])
70+
71+
# Add row to table
72+
table.add_row(f"{display_name}\n[dim]({name})[/]", description, meta_info)
73+
74+
console.print(table)
75+
76+
77+
def print_error(message, details=None):
78+
"""Print a standardized error message.
79+
80+
Args:
81+
message: The main error message
82+
details: Optional additional error details
83+
"""
84+
console.print(f"[bold red]Error:[/] {message}")
85+
if details:
86+
console.print(f"[red]{details}[/]")
87+
88+
89+
def print_client_error(client_name):
90+
"""Print a standardized client-related error message.
91+
92+
Args:
93+
client_name: Name of the client that caused the error
94+
"""
95+
console.print(f"[bold red]Error:[/] Unsupported active client")
96+
console.print(f"Please switch to a supported client using 'mcpm client <client-name>'")

tests/test_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ def test_search_error_handling(monkeypatch):
144144
result = runner.invoke(search, [])
145145

146146
assert result.exit_code == 0
147-
assert "Error searching for servers: Test error" in result.output
147+
assert "Error: Error searching for servers" in result.output
148+
assert "Test error" in result.output
148149
mock_repo_manager.search_servers.assert_called_once_with(None)
149150

150151

0 commit comments

Comments
 (0)