Skip to content

Commit de1e932

Browse files
Merge pull request #633 from Ciphey/bee-cleanup-colours
Clean up colours and make Ciphey more friendly
2 parents 35e3de8 + 2db6bba commit de1e932

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

ciphey/basemods/Checkers/human.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
from typing import Dict, Optional
22

33
from ciphey.iface import Checker, Config, ParamSpec, registry
4+
from rich.console import Console
5+
from rich.markup import escape
6+
7+
console = Console()
48

59

610
@registry.register
711
class HumanChecker(Checker[str]):
8-
@staticmethod
9-
def getParams() -> Optional[Dict[str, ParamSpec]]:
10-
pass
1112

12-
def check(self, text: str) -> Optional[str]:
13+
"""
14+
Uses the person's decision to determine plaintext
15+
"""
16+
17+
def check(self, ctext: str) -> Optional[str]:
1318
with self._config().pause_spinner_handle():
14-
response = input(f"Result {text.__repr__()} (y/N): ").lower()
19+
response = console.input(
20+
f"Possible plaintext: [blue bold]{escape(ctext.__repr__())}[/blue bold] ([green]y[/green]/[red]N[/red]): "
21+
)
1522
if response == "y":
1623
return ""
1724
elif response in ("n", ""):
1825
return None
1926
else:
20-
return self.check(text)
27+
return self.check(ctext)
2128

2229
def getExpectedRuntime(self, text: str) -> float:
2330
return 1 # About a second
2431

32+
@staticmethod
33+
def getParams() -> Optional[Dict[str, ParamSpec]]:
34+
return None
35+
2536
def __init__(self, config: Config):
2637
super().__init__(config)

ciphey/basemods/Checkers/what.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
from ciphey.iface import Checker, Config, ParamSpec, T, registry
44
from loguru import logger
55
from pywhat import identifier
6+
from rich.console import Console
7+
8+
console = Console()
69

710

811
@registry.register
912
class What(Checker[str]):
1013

1114
"""
1215
Uses PyWhat to determine plaintext with regexes
16+
https://github.com/bee-san/pyWhat
1317
"""
1418

1519
def check(self, ctext: T) -> Optional[str]:
1620
logger.trace("Trying PyWhat checker")
1721
returned_regexes = self.id.identify(ctext, api=True)
1822
if len(returned_regexes["Regexes"]) > 0:
19-
return returned_regexes["Regexes"][0]["Regex Pattern"]["Name"]
23+
console.print(
24+
f'\nI think the plaintext is a [yellow]{returned_regexes["Regexes"][0]["Regex Pattern"]["Name"]}[/yellow].'
25+
)
26+
return f'The plaintext is a [yellow]{returned_regexes["Regexes"][0]["Regex Pattern"]["Name"]}[/yellow].'
2027
return None
2128

2229
def getExpectedRuntime(self, text: T) -> float:

ciphey/ciphey.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
import click
1919
from appdirs import AppDirs
2020
from loguru import logger
21-
from rich import print
21+
from rich.console import Console
2222
from yaspin import yaspin
2323
from yaspin.spinners import Spinners
2424

2525
from . import iface
2626

2727
warnings.filterwarnings("ignore")
2828

29+
console = Console()
30+
2931

3032
def decrypt(config: iface.Config, ctext: Any) -> Union[str, bytes]:
3133
"""A simple alias for searching a ctext and makes the answer pretty"""
@@ -264,4 +266,4 @@ def all_procedure(ctx):
264266
if result is None:
265267
result = "Could not find any solutions."
266268

267-
print(result)
269+
console.print(result)

ciphey/iface/_modules.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from abc import ABC, abstractmethod
22
from typing import Any, Dict, Generic, List, NamedTuple, Optional, Set, Type, TypeVar
33

4+
from rich import box
5+
from rich.console import Console
6+
from rich.markup import escape
7+
from rich.table import Table
8+
49
from ._fwd import config as Config
510

611
T = TypeVar("T")
712
U = TypeVar("U")
813

14+
console = Console()
15+
916

1017
class ParamSpec(NamedTuple):
1118
"""
@@ -303,44 +310,58 @@ def __init__(self, config: Config):
303310

304311
def pretty_search_results(res: SearchResult, display_intermediate: bool = False) -> str:
305312
ret: str = ""
306-
if len(res.check_res) != 0:
307-
ret += f"Checker: {res.check_res}\n"
308-
ret += "Format used:\n"
313+
table = Table(show_header=False, box=box.ROUNDED, safe_box=False)
314+
# Only print the checker if we need to. Normal people don't know what
315+
# "quadgrams", "brandon", "json checker" is.
316+
# We print the checker if its regex or another language, so long as it starts with:
317+
# "The" like "The plaintext is a Uniform Resource Locator (URL)."
318+
if len(res.check_res) != 0 and "The" == res.check_res[0:3]:
319+
ret += f"{res.check_res}\n"
309320

310321
def add_one():
311-
nonlocal ret
312-
ret += f" {i.name}"
322+
out = ""
323+
if i.name == "utf8":
324+
out += f" [#808080]{i.name}[/#808080]"
325+
else:
326+
out += f" {i.name}"
313327
already_broken = False
314328
if i.result.key_info is not None:
315-
ret += f":\n Key: {i.result.key_info}\n"
329+
out += f":\n Key: {i.result.key_info}\n"
316330
already_broken = True
317331
if i.result.misc_info is not None:
318332
if not already_broken:
319-
ret += ":\n"
320-
ret += f" Misc: {i.result.misc_info}\n"
333+
out += ":\n"
334+
out += f" Misc: {i.result.misc_info}\n"
321335
already_broken = True
322336
if display_intermediate:
323337
if not already_broken:
324-
ret += ":\n"
325-
ret += f' Value: "{i.result.value}"\n'
338+
out += ":\n"
339+
out += f' Value: "{i.result.value}"\n'
326340
already_broken = True
327341
if not already_broken:
328-
ret += "\n"
342+
out += "\n"
343+
return out
329344

330345
# Skip the 'input' and print in order
346+
out = ""
331347
for i in res.path[1:]:
332-
add_one()
348+
out += add_one()
349+
350+
if out:
351+
if len(out.split("\n")) > 1:
352+
ret += "Formats used:\n"
353+
else:
354+
ret += "Format used:\n"
355+
ret += out
333356

334357
# Remove trailing newline
335358
ret = ret[:-1]
336359

337360
# If we didn't show intermediate steps, then print the final result
338361
if not display_intermediate:
339-
ret += (
340-
f"""\nFinal result: [bold green]"{res.path[-1].result.value}"[bold green]"""
341-
)
342-
343-
return ret
362+
ret += f"""\nPlaintext: [bold green]"{escape(res.path[-1].result.value)}"[bold green]"""
363+
table.add_row(ret)
364+
return table
344365

345366

346367
# Some common collection types

0 commit comments

Comments
 (0)