Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align cyber #533

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions dissect/target/helpers/cyber.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,46 +63,48 @@


class CyberIO(StringIO):
def __init__(self, color: Optional[Color] = None, mask_space: bool = False, run_at_end: bool = False):
def __init__(self, color: Optional[Color] = None, run_at_end: bool = False, **kwargs):
self._color = color
self._mask_space = mask_space
self._run_at_end = run_at_end
self._kwargs = kwargs

Check warning on line 69 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L69

Added line #L69 was not covered by tests
super().__init__()

def write(self, s: str) -> int:
if self._run_at_end:
super().write(s)
else:
cyber_print(s, self._color, self._mask_space)
cyber_print(s, self._color, **self._kwargs)

Check warning on line 76 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L76

Added line #L76 was not covered by tests
return len(s)


@contextmanager
def cyber(color: Optional[Color] = Color.YELLOW, mask_space: bool = False, run_at_end: bool = False) -> None:
stream = CyberIO(color, mask_space, run_at_end)
def cyber(color: Optional[Color] = Color.YELLOW, run_at_end: bool = False, **kwargs) -> Iterator[None]:
stream = CyberIO(color, run_at_end, **kwargs)

Check warning on line 82 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L82

Added line #L82 was not covered by tests
with redirect_stdout(stream):
yield

if run_at_end:
cyber_print(stream.getvalue(), color, mask_space)
cyber_print(stream.getvalue(), color, **kwargs)

Check warning on line 87 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L87

Added line #L87 was not covered by tests


def cyber_print(buf: str, color: Optional[Color] = None, mask_space: bool = False) -> None:
def cyber_print(buf: str, color: Optional[Color] = None, **kwargs) -> None:
if not buf or buf == "\n":
sys.__stdout__.write(buf)
return

if not CAN_CYBER:
sys.__stdout__.write("you're not cybering hard enough\n")
sys.__stdout__.write(buf)
return

Check warning on line 98 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L97-L98

Added lines #L97 - L98 were not covered by tests

if os.getenv("CYBER") == "💊":
matrix(buf, color, mask_space)
matrix(buf, color, **kwargs)

Check warning on line 101 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L101

Added line #L101 was not covered by tests
else:
nms(buf, color, mask_space)
nms(buf, color, **kwargs)

Check warning on line 103 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L103

Added line #L103 was not covered by tests


# https://github.com/bartobri/libnms
def nms(buf: str, color: Optional[Color] = None, mask_space: bool = False) -> None:
def nms(buf: str, color: Optional[Color] = None, mask_space: bool = False, mask_indent: bool = True, **kwargs) -> None:
orig_row, orig_col = (0, 0)
with _set_terminal():
max_rows, max_cols = _get_win_size()
Expand All @@ -118,6 +120,7 @@
character_state = []

try:
is_indent = True

Check warning on line 123 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L123

Added line #L123 was not covered by tests
# Write initial mask
for char, has_ansi, end_ansi in characters:
# Initialize the character state with a mask and reveal time
Expand All @@ -129,7 +132,16 @@
mask = random.choice(NMS_MASK_TABLE)
character_state.append((char, mask, reveal_time, has_ansi))

if ("\n" in char or "\r\n" in char) or (not mask_space and char == " "):
if char != " ":
is_indent = False

Check warning on line 136 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L135-L136

Added lines #L135 - L136 were not covered by tests

if (

Check warning on line 138 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L138

Added line #L138 was not covered by tests
("\n" in char or "\r\n" in char)
or (not mask_space and char == " " and not is_indent and not mask_indent)
or (not mask_indent and is_indent)
):
if "\n" in char:
is_indent = True

Check warning on line 144 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L143-L144

Added lines #L143 - L144 were not covered by tests
sys.__stdout__.write(char)
continue

Expand All @@ -141,11 +153,21 @@
_clear_input()
time.sleep(1)

is_indent = True

Check warning on line 156 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L156

Added line #L156 was not covered by tests
for _ in range((NMS_JUMBLE_SECONDS * 1000) // NMS_JUMBLE_LOOP_SPEED):
_cursor_move(orig_row, orig_col)

for char, _, _, _ in character_state:
if ("\n" in char or "\r\n" in char) or (not mask_space and char == " "):
if char != " ":
is_indent = False

Check warning on line 162 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L161-L162

Added lines #L161 - L162 were not covered by tests

if (

Check warning on line 164 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L164

Added line #L164 was not covered by tests
("\n" in char or "\r\n" in char)
or (not mask_space and char == " ")
or (not mask_indent and is_indent)
):
if "\n" in char:
is_indent = True

Check warning on line 170 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L169-L170

Added lines #L169 - L170 were not covered by tests
sys.__stdout__.write(char)
continue

Expand All @@ -160,8 +182,18 @@
_cursor_move(orig_row, orig_col)
revealed = True

is_indent = True

Check warning on line 185 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L185

Added line #L185 was not covered by tests
for i, (char, mask, time_remaining, has_ansi) in enumerate(character_state):
if ("\n" in char or "\r\n" in char) or (not mask_space and char == " "):
if char != " ":
is_indent = False

Check warning on line 188 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L187-L188

Added lines #L187 - L188 were not covered by tests

if (

Check warning on line 190 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L190

Added line #L190 was not covered by tests
("\n" in char or "\r\n" in char)
or (not mask_space and char == " " and not is_indent and not mask_indent)
or (not mask_indent and is_indent)
):
if "\n" in char:
is_indent = True

Check warning on line 196 in dissect/target/helpers/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/cyber.py#L195-L196

Added lines #L195 - L196 were not covered by tests
sys.__stdout__.write(char)
continue

Expand Down Expand Up @@ -207,7 +239,7 @@


# https://github.com/jsbueno/terminal_matrix
def matrix(buf: str, color: Optional[Color] = None, mask_space: bool = False) -> None:
def matrix(buf: str, color: Optional[Color] = None, **kwargs) -> None:
orig_row, orig_col = (0, 0)
with _set_terminal():
max_rows, max_cols = _get_win_size()
Expand Down
9 changes: 7 additions & 2 deletions dissect/target/loaders/cyber.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import textwrap
from pathlib import Path

from dissect.target import Target
Expand Down Expand Up @@ -30,8 +32,11 @@
return False

def map(self, target: Target) -> None:
with cyber(mask_space=True):
print(HEADER)
cols, _ = os.get_terminal_size()
width = HEADER.index("\n", 1)
header = textwrap.indent(HEADER, " " * ((cols - width) // 2))
with cyber(mask_space=True, mask_indent=False):
print(header)

Check warning on line 39 in dissect/target/loaders/cyber.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/loaders/cyber.py#L35-L39

Added lines #L35 - L39 were not covered by tests

target.props["cyber"] = True
return self._real.map(target)
Loading