Skip to content

Commit ce895be

Browse files
feat: split view in edit cmd
1 parent 9aa9913 commit ce895be

File tree

5 files changed

+250
-102
lines changed

5 files changed

+250
-102
lines changed

src/commands/daily.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ def daily(
1010
full: bool = typer.Option(
1111
False, "--full", "-f", help="Show full problem description"
1212
),
13-
save: bool = typer.Option(
14-
False, "--save", "-s", help="Save problem description to a file"
15-
),
1613
no_editor: bool = typer.Option(False, "--no-editor", help="Skip opening editor"),
1714
):
1815
"""
@@ -35,7 +32,7 @@ def daily(
3532
question = result["data"]["activeDailyCodingChallengeQuestion"]
3633
typer.echo("\r" + " " * 30 + "\r", nl=False)
3734

38-
show(problem=question["question"]["titleSlug"], save=save, compact=not full)
35+
show(problem=question["question"]["titleSlug"], save=False, compact=not full)
3936
except Exception as e:
4037
typer.echo(
4138
"\n"

src/commands/edit.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import os
2+
import subprocess
3+
14
import typer
25

36
from ..server.auth import Auth
47
from ..server.config import LANGUAGE_MAP
58
from ..server.solution_manager import SolutionManager
9+
from .show import _save_problem_to_file
610

711
solution_manager = SolutionManager(Auth().get_session())
812

@@ -27,6 +31,15 @@ def edit(
2731
problem if problem.isdigit() else question_data.get("questionId")
2832
)
2933

34+
# Save problem description using _save_problem_to_file function
35+
try:
36+
_save_problem_to_file(question_data)
37+
except Exception as e:
38+
typer.echo(f"Warning: Could not save problem description: {str(e)}")
39+
40+
problem_title_slug = question_data.get("titleSlug", problem)
41+
markdown_file = f"{problem_title_slug}.md"
42+
3043
def create_file_with_template(lang: str):
3144
filename = f"{filename_prefix}.{lang}"
3245
with open(filename, "w") as f:
@@ -35,9 +48,44 @@ def create_file_with_template(lang: str):
3548
f.write(snippet.get("code"))
3649
return
3750
typer.echo(f"No template found for language {lang}")
51+
return filename
3852

39-
create_file_with_template(lang)
53+
code_file_path = f"{filename_prefix}.{lang}"
54+
55+
if not os.path.exists(markdown_file):
56+
typer.echo(f"Warning: Problem description file not found: {markdown_file}")
57+
subprocess.run([editor, code_file_path])
58+
return
4059

41-
import subprocess
60+
try:
61+
vim_like_editors = ["vim", "nvim", "gvim", "neovim", "mvim", "lvim", "vimr"]
62+
editor_name = os.path.basename(editor.split()[0]).lower()
4263

43-
subprocess.run([editor, f"{filename_prefix}.{lang}"])
64+
if editor_name in vim_like_editors:
65+
subprocess.run([editor, "-O", code_file_path, markdown_file])
66+
typer.echo(
67+
f"Tip: In {editor_name}, you might need a plugin to preview markdown. "
68+
"Try ':set ft=markdown' to at least get syntax highlighting."
69+
)
70+
elif editor == "code":
71+
subprocess.run(
72+
[
73+
editor,
74+
"--goto",
75+
code_file_path,
76+
"-r",
77+
"--goto",
78+
f"{markdown_file}:1",
79+
]
80+
)
81+
subprocess.run([editor, "--command", "markdown.showPreview"])
82+
elif editor == "nano":
83+
typer.echo(
84+
"Note: Nano doesn't support split view or markdown preview. Opening code file only."
85+
)
86+
subprocess.run([editor, code_file_path])
87+
else:
88+
typer.echo(f"Note: Split view might not work properly in {editor}.")
89+
subprocess.run([editor, code_file_path, markdown_file])
90+
except Exception as e:
91+
typer.echo(f"Failed to open editor: {str(e)}")

src/lib/solution_ui.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SolutionUI:
1818
"Author": 18,
1919
"Date": 12,
2020
"Stats": 20,
21-
"Tags": 25,
21+
"Tags": 35,
2222
}
2323
MAX_TAGS = 4
2424
STYLES = {
@@ -68,7 +68,7 @@ def _format_reactions(self, reactions):
6868
up_formatted = self._format_number(upvotes)
6969
down_formatted = self._format_number(downvotes)
7070

71-
return f"[green]{up_formatted}[/green] [red]▼{down_formatted}[/red]"
71+
return f"[green]{up_formatted}[/green] [red]▼ {down_formatted}[/red]"
7272

7373
def _truncate_text(self, text, max_length):
7474
"""Truncate text with ellipsis if longer than max_length"""
@@ -118,14 +118,14 @@ def _open_solution_url(self, slug):
118118
webbrowser.open(url)
119119

120120
def show_solution(self):
121+
console.print("\n")
122+
121123
table = Table(
122-
box=box.SIMPLE_HEAD,
123-
expand=True,
124-
border_style=self.STYLES["table_border"],
125-
header_style="bold white on blue",
126-
padding=(0, 1),
127-
collapse_padding=False,
128-
show_edge=False,
124+
title=f"Solutions Found: {self.total_solutions}",
125+
box=box.ROUNDED,
126+
border_style="cyan",
127+
pad_edge=False,
128+
show_edge=True,
129129
)
130130

131131
for column in self.COLUMNS:
@@ -134,12 +134,9 @@ def show_solution(self):
134134
width=self.COLUMN_WIDTHS.get(column, None),
135135
justify="left" if column != "#" else "right",
136136
no_wrap=column not in ["Tags"],
137+
style="bold" if column in ["Title", "#"] else None,
137138
)
138139

139-
console.print(
140-
f"\n[bold cyan]Solutions Found: {self.total_solutions}[/bold cyan]\n"
141-
)
142-
143140
for i, solution in enumerate(self.solutions, 1):
144141
node = solution.get("node", {})
145142
slug = node.get("slug", "")
@@ -158,15 +155,15 @@ def show_solution(self):
158155
hit_count = node.get("hitCount", 0)
159156
hit_count_formatted = self._format_number(hit_count)
160157
reactions = self._format_reactions(node.get("reactions", []))
161-
stats = f"[bold blue]👁️{hit_count_formatted}[/bold blue] {reactions}"
158+
stats = f"[bold blue]{hit_count_formatted}[/bold blue] {reactions}"
162159

163160
tags = self._format_tags(node.get("tags", []))
164161

165162
table.add_row(str(i), title, author, date, stats, tags)
166163

167164
console.print(table)
168165
console.print(
169-
"\n[dim italic]Click on solution titles to open them in your browser[/dim italic]"
166+
"\n[dim italic]Click on solution titles to open them in your browser[/dim italic]\n"
170167
)
171168

172169
def handle_solution_selection(self, index):

0 commit comments

Comments
 (0)