Skip to content

Commit

Permalink
prep to use as library
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdeanmartin committed Dec 11, 2023
1 parent b18182f commit 83fa344
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 52 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# dedlin

Dedlin is an interactive line-by-line text editor and a DSL. Line editors
suck, but they are easy to write and the DSL is mildly interesting.
Dedlin is an interactive line-by-line text editor and a DSL, similar to edlin or ed. It is perfect for
letting AI chatbots edit documents without sending the whole document back and forth.

While this is a clone of [edlin](https://en.wikipedia.org/wiki/Edlin), this is not intended to be backwards compatible
with anything. I have made changes to make the app less user hostile, but there is a `--vim_mode`
It is scriptable making it similar to `sed` for doing find, insert, replace, delete operations on existing files.

Soon it will support non-line number ranges, e.g. `"/Done/ DELETE`

Dedlin extends on [edlin](https://en.wikipedia.org/wiki/Edlin) enough that it is not backwards compatible.

I have made changes to make the app less user hostile than classic ed or edlin, but there is a `--vim_mode`
where all help, warnings, feedback will be suppressed.

## Badges
Expand Down
23 changes: 23 additions & 0 deletions dedlin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dedlin.command_sources import CommandGenerator, InteractiveGenerator
from dedlin.document_sources import PrefillInputter, SimpleInputter, input_with_prefill
from dedlin.flash import title_screen
from dedlin.logging_utils import configure_logging
from dedlin.main import Dedlin
from dedlin.outputters import rich_output, talking_outputter
from dedlin.outputters.plain import plain_printer

# All the parts necessary to implement an alternative to __main__
__all__ = [
"CommandGenerator",
"InteractiveGenerator",
"StringCommandGenerator",
"PrefillInputter",
"SimpleInputter",
"input_with_prefill",
"title_screen",
"configure_logging",
"Dedlin",
"rich_output",
"talking_outputter",
"plain_printer",
]
27 changes: 19 additions & 8 deletions dedlin/command_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
from dedlin.parsers import parse_command
from dedlin.pygments_code import EdLexer

# from prompt_toolkit.shortcuts import prompt
# from pygments.lexers import guess_lexer_for_filename
# from prompt_toolkit.completion import Completer, Completion
# from prompt_toolkit.formatted_text import HTML

# thing = guess_lexer_for_filename("cats.py","")
style = style_from_pygments_cls(get_style_by_name("borland"))


Expand Down Expand Up @@ -126,5 +120,22 @@ def generate(
yield from self.commands


if __name__ == "__main__":
print("This is a module, not a program.")
class StringCommandGenerator:
"""Get a typed command from a string"""

def __init__(self, source: str):
"""Initialize the generator"""
self.source: str = source
self.current_line: int = 0
self.document_length: int = 0
self.prompt: str = "> "

def generate(
self,
) -> Generator[Command, None, None]:
"""Turn a sring into a bunch of commands"""
for line in self.source.split("\n"):
command = parse_command(
line.strip("\r"), current_line=self.current_line, document_length=self.document_length
)
yield command
5 changes: 4 additions & 1 deletion dedlin/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ def lorem(self, line_range: Optional[LineRange]) -> None:
def undo(self) -> None:
"""Undo last change"""
self.lines = self.previous_lines
self.previous_current_line = self.current_line
if self.previous_current_line < 1:
self.current_line = 1
else:
self.current_line = self.previous_current_line
logger.debug("Undid last step")

def sort(self) -> None:
Expand Down
11 changes: 9 additions & 2 deletions dedlin/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def bare_command(command: str) -> Optional[Command]:
def parse_command(command: str, current_line: int, document_length: int) -> Command:
"""Parse a command"""
original_text = command

# Handle empty text.
if not command:
return Command(
command=Commands.EMPTY,
Expand All @@ -253,13 +255,14 @@ def parse_command(command: str, current_line: int, document_length: int) -> Comm
original_text_upper = command.upper()
command = command.upper().strip()

# Handle comments
if not command or command.startswith("#"):
return Command(
command=Commands.EMPTY,
original_text=original_text,
)

# bare number is insert.
# Handle shortcuts, bare number is insert.
candidate_int = try_parse_int(command)
if candidate_int is not None:
target = candidate_int
Expand All @@ -277,7 +280,10 @@ def parse_command(command: str, current_line: int, document_length: int) -> Comm
original_text=original_text,
)

# TODO: maybe use regex.
# Divide into
# - front part (pre command)
# - command
# - phrases (post command)
front_part_chars = []
found_first_alpha = False
just_command_chars = []
Expand All @@ -295,6 +301,7 @@ def parse_command(command: str, current_line: int, document_length: int) -> Comm
just_command = "".join(just_command_chars)
location_of_command = original_text_upper.find(just_command)

# Handle post command phrases
end_part = original_text[location_of_command + len(just_command) :]
if end_part:
# must preserve case!
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[tool.poetry]
name = "dedlin"
version = "1.14.0"
version = "1.15.0"
description = "Line editor, edlin clone with many improvements"
authors = ["Matthew Martin <matthewdeanmartin@gmail.com>"]
keywords = ["editor", "edlin", "line editor",]
classifiers = [
"Development Status :: 3 - Alpha",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
Expand Down
35 changes: 0 additions & 35 deletions scripts/a.py

This file was deleted.

0 comments on commit 83fa344

Please sign in to comment.