Files
cli-anything-codimd/cli_anything/codimd/utils/__init__.py

64 lines
1.5 KiB
Python

"""
Utility functions for CodiMD CLI.
"""
import sys
import json
from typing import Any, Dict
def output_json(data: Dict[str, Any]) -> None:
"""Output data as JSON."""
json.dump(data, sys.stdout, indent=2)
sys.stdout.write("\n")
def format_note_list(notes: list) -> str:
"""Format note list for display."""
if not notes:
return "No notes found."
lines = []
for note in notes:
note_id = note.get("id", "unknown")
title = note.get("text", "Untitled")
created = note.get("createdAt", "")
short_id = note.get("shortId", "")
lines.append(f"ID: {note_id}")
lines.append(f" Title: {title}")
lines.append(f" Short ID: {short_id}")
lines.append(f" Created: {created}")
lines.append("")
return "\n".join(lines)
def format_user_info(user: Dict[str, Any]) -> str:
"""Format user info for display."""
lines = ["User Information:"]
lines.append(f" ID: {user.get('id', 'N/A')}")
# CodiMD returns name directly, not nested in profile
name = user.get("name") or user.get("profile", {}).get("name", "N/A")
lines.append(f" Name: {name}")
lines.append(f" Email: {user.get('email', 'N/A')}")
return "\n".join(lines)
def format_error(message: str) -> Dict[str, Any]:
"""Format error as JSON response."""
return {
"status": "error",
"error": message
}
def format_success(data: Any) -> Dict[str, Any]:
"""Format success as JSON response."""
return {
"status": "success",
"data": data
}