Handle unknown lexer, remove dead _THEMES list, tighten JSON detection - Catch unknown Pygments lexer names and fall back to plain text - Remove unused _THEMES list from cli.py - Only auto-detect JSON when content starts with { or [ to avoid false positives on trivial values like strings or numbers
This commit is contained in:
@@ -20,22 +20,6 @@ app = typer.Typer(
|
|||||||
rich_markup_mode="rich",
|
rich_markup_mode="rich",
|
||||||
)
|
)
|
||||||
|
|
||||||
_THEMES = [
|
|
||||||
"monokai",
|
|
||||||
"github-dark",
|
|
||||||
"one-dark",
|
|
||||||
"vim",
|
|
||||||
"vs",
|
|
||||||
"nord",
|
|
||||||
"dracula",
|
|
||||||
"solarized-dark",
|
|
||||||
"solarized-light",
|
|
||||||
"default",
|
|
||||||
"friendly",
|
|
||||||
"native",
|
|
||||||
"emacs",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def version_callback(value: bool) -> None:
|
def version_callback(value: bool) -> None:
|
||||||
if value:
|
if value:
|
||||||
|
|||||||
@@ -73,10 +73,12 @@ def detect_type(
|
|||||||
return EXTENSION_MAP[suffix]
|
return EXTENSION_MAP[suffix]
|
||||||
|
|
||||||
if content is not None:
|
if content is not None:
|
||||||
try:
|
stripped = content.lstrip()
|
||||||
json.loads(content[:1000])
|
if stripped and stripped[0] in ("{", "["):
|
||||||
return "json"
|
try:
|
||||||
except (json.JSONDecodeError, ValueError):
|
json.loads(stripped[:1000])
|
||||||
pass
|
return "json"
|
||||||
|
except (json.JSONDecodeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
return "text"
|
return "text"
|
||||||
|
|||||||
@@ -44,9 +44,14 @@ def render(
|
|||||||
content, "json", theme=options.theme, line_numbers=line_numbers
|
content, "json", theme=options.theme, line_numbers=line_numbers
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
renderable = Syntax(
|
try:
|
||||||
content, file_type, theme=options.theme, line_numbers=line_numbers
|
renderable = Syntax(
|
||||||
)
|
content, file_type, theme=options.theme, line_numbers=line_numbers
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
renderable = Syntax(
|
||||||
|
content, "text", theme=options.theme, line_numbers=line_numbers
|
||||||
|
)
|
||||||
|
|
||||||
if use_pager:
|
if use_pager:
|
||||||
with console.pager(styles=True):
|
with console.pager(styles=True):
|
||||||
|
|||||||
Reference in New Issue
Block a user