Skip to content

Commit

Permalink
wip: new
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Mar 30, 2024
1 parent 81bc108 commit 1f6afb0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/bin/grexd-hugefileviewer
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ E = KeyPressEvent
class HugeFileViewerWidget:
def __init__(self, filename: os.PathLike[str]):
self.filename = filename
self.control = grexd.HugeFileViewerUIControl(filename=filename)
with open(self.filename, "rb") as fd:
self.control = grexd.HugeFileViewerUIControl(fd=fd)
self.window = Window(self.control)

def __pt_container__(self) -> Container:
Expand Down
16 changes: 10 additions & 6 deletions src/bin/grexd-regexbuild
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ A console regular expression editor
import argparse
import asyncio
import logging
import os
import re
from typing import Any, Optional
from typing import Optional

import grexd
from prompt_toolkit import Application
Expand All @@ -30,9 +31,10 @@ MAX_LINES = 1000


class FileviewControl(grexd.HugeFileViewerUIControl):
def __init__(self, *args: Any, **kwargs: Any) -> None:
def __init__(self, filename: os.PathLike[str]) -> None:
self.regex: Optional[re.Pattern[bytes]] = None
grexd.HugeFileViewerUIControl.__init__(self, *args, **kwargs)
with open(filename, "rb") as fd:
grexd.HugeFileViewerUIControl.__init__(self, fd=fd)

def update_lines(self) -> None:
if not self.regex:
Expand Down Expand Up @@ -69,7 +71,7 @@ class FileviewControl(grexd.HugeFileViewerUIControl):


class Fileview:
def __init__(self, filename: str):
def __init__(self, filename: os.PathLike[str]):
self.filename = filename
self.control = FileviewControl(filename)
self.window = Window(self.control)
Expand Down Expand Up @@ -133,12 +135,14 @@ class RegexWindow:
return self.window


async def grexd_run(filename: str, regex_str: Optional[str] = None) -> None:
async def grexd_run(
filename: os.PathLike[str], regex_str: Optional[str] = None
) -> None:
fileview = Fileview(filename)
regexwin = RegexWindow(fileview, regex_str or "")
root_container = HSplit(
[
Frame(title=filename, body=fileview),
Frame(title=str(filename), body=fileview),
Frame(title="Regular expression", body=regexwin, height=7),
]
)
Expand Down
7 changes: 3 additions & 4 deletions src/grexd/hugefilevieweruicontrol.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Control for the huge file widget"""

import io
import mmap
import os
import re
from typing import TYPE_CHECKING, List, Optional

Expand All @@ -23,9 +23,8 @@
class HugeFileViewerUIControl(UIControl):
"""UIControl optimized for huge file visualization"""

def __init__(self, filename: os.PathLike[str]):
self._filename = filename
self._fd = open(self._filename, "r")
def __init__(self, fd: io.BufferedReader):
self._fd = fd
self._mm = mmap.mmap(self._fd.fileno(), 0, access=mmap.ACCESS_READ)
self._size = self._mm.size()
self._offset = 0
Expand Down

0 comments on commit 1f6afb0

Please sign in to comment.