Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenfreund committed Mar 28, 2024
1 parent bac7731 commit 62d2d71
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/chatdbg/chatdbg_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
from traitlets import TraitError

from .assistant.assistant import Assistant
from .util.capture import CaptureInput, CaptureOutput
from .pdb.capture import CaptureInput, CaptureOutput
from .util.config import chatdbg_config
from .util.locals import extract_locals
from .pdb.locals import extract_locals
from .util.log import ChatDBGLog
from .util.printer import ChatDBGPrinter
from .util.prompts import pdb_instructions
from .util.text import (format_limited, strip_color, truncate_proportionally)
from .pdb.prompts import pdb_instructions
from .pdb.text import (format_limited, strip_color, truncate_proportionally)


def load_ipython_extension(ipython):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 0 additions & 24 deletions src/chatdbg/util/text.py → src/chatdbg/pdb/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,3 @@ def truncate_proportionally(text, maxlen=32000, top_proportion=0.5):
return text[:pre] + "..." + text[len(text) - post :]
return text


def word_wrap_except_code_blocks(text: str, width: int = 80) -> str:
"""
Wraps text except for code blocks for nice terminal formatting.
Splits the text into paragraphs and wraps each paragraph,
except for paragraphs that are inside of code blocks denoted
by ` ``` `. Returns the updated text.
Args:
text (str): The text to wrap.
width (int): The width of the lines to wrap at, passed to `textwrap.fill`.
Returns:
The wrapped text.
"""
blocks = text.split("```")
for i in range(len(blocks)):
if i % 2 == 0:
paras = blocks[i].split("\n")
wrapped = [textwrap.fill(para, width=width) for para in paras]
blocks[i] = "\n".join(wrapped)

return "```".join(blocks)
2 changes: 1 addition & 1 deletion src/chatdbg/util/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from traitlets import Bool, Int, TraitError, Unicode
from traitlets import Bool, Int, Unicode
from traitlets.config import Configurable


Expand Down
4 changes: 2 additions & 2 deletions src/chatdbg/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import yaml

from ..assistant.listeners import BaseAssistantListener
from ..util.capture import CaptureOutput
from .text import word_wrap_except_code_blocks
from ..pdb.capture import CaptureOutput
from .wrap import word_wrap_except_code_blocks


class ChatDBGLog(BaseAssistantListener):
Expand Down
6 changes: 3 additions & 3 deletions src/chatdbg/util/printer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import textwrap
from assistant.listeners import BaseAssistantListener
from util.streamwrap import StreamingTextWrapper
from util.text import word_wrap_except_code_blocks
from ..assistant.listeners import BaseAssistantListener
from .stream import StreamingTextWrapper
from .wrap import word_wrap_except_code_blocks


class ChatDBGPrinter(BaseAssistantListener):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import textwrap
import re
import sys
from .text import word_wrap_except_code_blocks
from .wrap import word_wrap_except_code_blocks


class StreamingTextWrapper:
Expand Down
27 changes: 27 additions & 0 deletions src/chatdbg/util/wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import textwrap


def word_wrap_except_code_blocks(text: str, width: int = 80) -> str:
"""
Wraps text except for code blocks for nice terminal formatting.
Splits the text into paragraphs and wraps each paragraph,
except for paragraphs that are inside of code blocks denoted
by ` ``` `. Returns the updated text.
Args:
text (str): The text to wrap.
width (int): The width of the lines to wrap at, passed to `textwrap.fill`.
Returns:
The wrapped text.
"""
blocks = text.split("```")
for i in range(len(blocks)):
if i % 2 == 0:
paras = blocks[i].split("\n")
wrapped = [textwrap.fill(para, width=width) for para in paras]
blocks[i] = "\n".join(wrapped)

return "```".join(blocks)

0 comments on commit 62d2d71

Please sign in to comment.