Skip to content

Commit 28ea070

Browse files
authored
Remove redundancy in module (cli.py/cli -> app.py/cli) (#34)
2 parents c592c5b + e537a9b commit 28ea070

File tree

8 files changed

+127
-111
lines changed

8 files changed

+127
-111
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ WORKDIR /app
44

55
COPY . /app
66

7-
RUN pip install --upgrade pip \
8-
&& pip install . \
7+
RUN pip install --upgrade pip poetry \
8+
&& poetry install \
99
&& apt-get clean
1010

1111
ENTRYPOINT ["searchcode"]

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sc search "import module"
1515

1616
```python
1717
from pprint import pprint
18-
from searchcode import Searchcode
18+
from src.searchcode import Searchcode
1919

2020
sc = Searchcode(user_agent="My-Searchcode-script")
2121
search = sc.search(query="import module")
@@ -70,7 +70,7 @@ searchcode "import module"
7070

7171
```python
7272
from pprint import pprint
73-
from searchcode import Searchcode
73+
from src.searchcode import Searchcode
7474

7575
sc = Searchcode(user_agent="My-Searchcode-script")
7676
search = sc.search(query="import module")
@@ -92,7 +92,7 @@ searchcode "import module" --languages java,javascript
9292

9393
```python
9494
from pprint import pprint
95-
from searchcode import Searchcode
95+
from src.searchcode import Searchcode
9696

9797
sc = Searchcode(user_agent="My-Searchcode-script")
9898
search = sc.search(query="import module", languages=["Java", "JavaScript"])
@@ -115,7 +115,7 @@ searchcode "import module" --sources bitbucket,codeplex
115115

116116
```python
117117
from pprint import pprint
118-
from searchcode import Searchcode
118+
from src.searchcode import Searchcode
119119

120120
sc = Searchcode(user_agent="My-Searchcode-script")
121121
search = sc.search(query="import module", sources=["BitBucket", "CodePlex"])
@@ -138,7 +138,7 @@ searchcode "import module" --lines-of-code-gt 500 --lines-of-code-lt 1000
138138

139139
```python
140140
from pprint import pprint
141-
from searchcode import Searchcode
141+
from src.searchcode import Searchcode
142142

143143
sc = Searchcode(user_agent="My-Searchcode-script")
144144
search = sc.search(query="import module", lines_of_code_gt=500, lines_of_code_lt=1000)
@@ -161,7 +161,7 @@ searchcode "import module" --callback myCallback
161161

162162
```python
163163
from pprint import pprint
164-
from searchcode import Searchcode
164+
from src.searchcode import Searchcode
165165

166166
sc = Searchcode(user_agent="My-Searchcode-script")
167167
search = sc.search(query="import module", callback="myCallback")
@@ -222,7 +222,7 @@ searchode code 4061576
222222

223223
```python
224224

225-
from searchcode import Searchcode
225+
from src.searchcode import Searchcode
226226

227227
sc = Searchcode(user_agent="My-Searchcode-script")
228228
data = sc.code(4061576)

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "searchcode"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
description = "Simple, comprehensive code search."
55
authors = ["Ritchie Mwewa <rly0nheart@duck.com>"]
66
license = "GPLv3+"
@@ -30,5 +30,5 @@ requires = ["poetry-core"]
3030
build-backend = "poetry.core.masonry.api"
3131

3232
[tool.poetry.scripts]
33-
sc = "searchcode.cli:cli"
34-
searchcode = "searchcode.cli:cli"
33+
sc = "searchcode.__app:cli"
34+
searchcode = "searchcode.__app:cli"
Lines changed: 18 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
"""
1717

18-
import os
19-
import subprocess
2018
import typing as t
21-
from types import SimpleNamespace
2219

2320
import rich_click as click
24-
from rich import box
2521
from rich.console import Console
26-
from rich.panel import Panel
2722
from rich.pretty import pprint
2823
from rich.syntax import Syntax
2924

30-
from . import License, __pkg__, __version__
25+
from . import License
26+
from .__lib import (
27+
__pkg__,
28+
__version__,
29+
update_window_title,
30+
clear_screen,
31+
print_jsonp,
32+
print_panels,
33+
)
3134
from .api import Searchcode
3235

3336
sc = Searchcode(user_agent=f"{__pkg__}-sdk/cli")
@@ -46,7 +49,7 @@ def cli():
4649
Simple, comprehensive code search.
4750
"""
4851

49-
__update_window_title("Source code search engine.")
52+
update_window_title(text="Source code search engine.")
5053

5154

5255
@cli.command("license")
@@ -59,8 +62,8 @@ def licence(
5962
"""
6063
Show license information
6164
"""
62-
__clear_screen()
63-
__update_window_title(
65+
clear_screen()
66+
update_window_title(
6467
text="Terms and Conditions" if conditions else "Warranty" if warranty else None
6568
)
6669
if conditions:
@@ -133,8 +136,8 @@ def search(
133136
134137
e.g., sc search "import module"
135138
"""
136-
__clear_screen()
137-
__update_window_title(text=query)
139+
clear_screen()
140+
update_window_title(text=query)
138141

139142
with console.status(
140143
f"Querying code index with search string: [green]{query}[/]..."
@@ -154,9 +157,9 @@ def search(
154157
)
155158

156159
(
157-
__print_jsonp(jsonp=response)
160+
print_jsonp(jsonp=response)
158161
if callback
159-
else (pprint(response) if pretty else __print_panels(data=response.results))
162+
else (pprint(response) if pretty else print_panels(data=response.results))
160163
)
161164

162165

@@ -168,8 +171,8 @@ def code(id: int):
168171
169172
e.g., sc code 4061576
170173
"""
171-
__clear_screen()
172-
__update_window_title(text=str(id))
174+
clear_screen()
175+
update_window_title(text=str(id))
173176
with console.status(f"Fetching data for code file with ID: [cyan]{id}[/]..."):
174177
data = sc.code(id)
175178
lines = data.code
@@ -179,86 +182,3 @@ def code(id: int):
179182
code=lines, lexer=language, line_numbers=True, theme="dracula"
180183
)
181184
console.print(syntax)
182-
183-
184-
def __print_jsonp(jsonp: str) -> None:
185-
"""
186-
Pretty-prints a raw JSONP string.
187-
188-
:param jsonp: A complete JSONP string.
189-
"""
190-
syntax = Syntax(jsonp, "text", line_numbers=True)
191-
console.print(syntax)
192-
193-
194-
def __print_panels(data: t.List[SimpleNamespace]):
195-
"""
196-
Render a list of code records as rich panels with syntax highlighting.
197-
Line numbers are preserved and displayed alongside code content.
198-
199-
:param data: A list of dictionaries, where each dictionary represents a code record
200-
"""
201-
202-
def extract_code_string_with_linenumbers(lines_dict: t.Dict[str, str]) -> str:
203-
"""
204-
Convert a dictionary of line_number: code_line into a single
205-
multiline string sorted by line number.
206-
207-
Each line is right-aligned to maintain visual alignment in output.
208-
209-
:param lines_dict: Dictionary where keys are line numbers (as strings) and values are lines of code.
210-
:return: Multiline string with original line numbers included.
211-
"""
212-
sorted_lines = sorted(lines_dict.items(), key=lambda x: int(x[0]))
213-
numbered_lines = [
214-
f"{line_no.rjust(4)} {line.rstrip()}" for line_no, line in sorted_lines
215-
]
216-
return "\n".join(numbered_lines)
217-
218-
for item in data:
219-
filename = item.filename
220-
repo = item.repo
221-
language = item.language
222-
lines_count = item.linescount
223-
lines = item.lines
224-
225-
code_string = extract_code_string_with_linenumbers(lines_dict=lines.__dict__)
226-
227-
syntax = Syntax(
228-
code=code_string,
229-
lexer=language,
230-
word_wrap=False,
231-
indent_guides=True,
232-
theme="dracula",
233-
)
234-
235-
panel = Panel(
236-
renderable=syntax,
237-
box=box.ROUNDED,
238-
title=f"[bold]{filename}[/] ([blue]{repo}[/]) {language} ⸱ [cyan]{lines_count}[/] lines",
239-
highlight=True,
240-
)
241-
242-
console.print(panel)
243-
244-
245-
def __update_window_title(text: str):
246-
"""
247-
Update the current window title with the specified text.
248-
249-
:param text: Text to update the window with.
250-
"""
251-
console.set_window_title(f"{__pkg__.capitalize()} - {text}")
252-
253-
254-
def __clear_screen():
255-
"""
256-
Clear the screen.
257-
258-
Not using console.clear() because it doesn't really clear the screen.
259-
It instead creates a space between the items on top and below,
260-
then moves the cursor to the items on the bottom, thus creating the illusion of a "cleared screen".
261-
262-
Using subprocess might be a bad idea, but I'm yet to see how bad of an idea that is.
263-
"""
264-
subprocess.run(["cls" if os.name == "nt" else "clear"])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .api import Searchcode
2121

2222
__pkg__ = "searchcode"
23-
__version__ = "0.5.0"
23+
__version__ = "0.5.1"
2424
__author__ = "Ritchie Mwewa"
2525

2626

src/searchcode/__lib.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import os
2+
import subprocess
3+
import typing as t
4+
from types import SimpleNamespace
5+
6+
from rich import box
7+
from rich.console import Console
8+
from rich.panel import Panel
9+
from rich.syntax import Syntax
10+
11+
from . import __pkg__, __version__
12+
13+
console = Console()
14+
15+
16+
def print_jsonp(jsonp: str) -> None:
17+
"""
18+
Pretty-prints a raw JSONP string.
19+
20+
:param jsonp: A complete JSONP string.
21+
"""
22+
syntax = Syntax(jsonp, "text", line_numbers=True)
23+
console.print(syntax)
24+
25+
26+
def print_panels(data: t.List[SimpleNamespace]):
27+
"""
28+
Render a list of code records as rich panels with syntax highlighting.
29+
Line numbers are preserved and displayed alongside code content.
30+
31+
:param data: A list of dictionaries, where each dictionary represents a code record
32+
"""
33+
34+
def extract_code_string_with_linenumbers(lines_dict: t.Dict[str, str]) -> str:
35+
"""
36+
Convert a dictionary of line_number: code_line into a single
37+
multiline string sorted by line number.
38+
39+
Each line is right-aligned to maintain visual alignment in output.
40+
41+
:param lines_dict: Dictionary where keys are line numbers (as strings) and values are lines of code.
42+
:return: Multiline string with original line numbers included.
43+
"""
44+
sorted_lines = sorted(lines_dict.items(), key=lambda x: int(x[0]))
45+
numbered_lines = [
46+
f"{line_no.rjust(4)} {line.rstrip()}" for line_no, line in sorted_lines
47+
]
48+
return "\n".join(numbered_lines)
49+
50+
for item in data:
51+
filename = item.filename
52+
repo = item.repo
53+
language = item.language
54+
lines_count = item.linescount
55+
lines = item.lines
56+
57+
code_string = extract_code_string_with_linenumbers(lines_dict=lines.__dict__)
58+
59+
syntax = Syntax(
60+
code=code_string,
61+
lexer=language,
62+
word_wrap=False,
63+
indent_guides=True,
64+
theme="dracula",
65+
)
66+
67+
panel = Panel(
68+
renderable=syntax,
69+
box=box.ROUNDED,
70+
title=f"[bold]{filename}[/] ([blue]{repo}[/]) {language} ⸱ [cyan]{lines_count}[/] lines",
71+
highlight=True,
72+
)
73+
74+
console.print(panel)
75+
76+
77+
def update_window_title(text: str):
78+
"""
79+
Update the current window title with the specified text.
80+
81+
:param text: Text to update the window with.
82+
"""
83+
console.set_window_title(f"{__pkg__.capitalize()} v{__version__} - {text}")
84+
85+
86+
def clear_screen():
87+
"""
88+
Clear the screen.
89+
90+
Not using console.clear() because it doesn't really clear the screen.
91+
It instead creates a space between the items on top and below,
92+
then moves the cursor to the items on the bottom, thus creating the illusion of a "cleared screen".
93+
94+
Using subprocess might be a bad idea, but I'm yet to see how bad of an idea that is.
95+
"""
96+
subprocess.run(["cls" if os.name == "nt" else "clear"])
File renamed without changes.

0 commit comments

Comments
 (0)