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

feat: response formatting #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
51 changes: 48 additions & 3 deletions localmentor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,65 @@
from . import mentor
from pprint import pprint

def format_and_print_output(output):
OKGREEN = '\033[92m' # Green
ENDC = '\033[0m' # End color formatting
BOLD = '\033[1m' # Bold text
# Function to wrap text lines to a specified width
def wrap_line(line, width):
words = line.split()
wrapped_lines = []
current_line = []

for word in words:
# Check if adding the word would exceed the width
if sum(len(w) for w in current_line) + len(word) + len(current_line) > width:
wrapped_lines.append(' '.join(current_line))
current_line = [word]
else:
current_line.append(word)

if current_line:
wrapped_lines.append(' '.join(current_line))

return wrapped_lines


# Splitting the output to extract the prompt and the response
prompt_end_index = output.find("'") + 1
prompt_end_index = output.find("'", prompt_end_index) + 1

prompt = output[:prompt_end_index].strip()
response = output[prompt_end_index:].strip()

# Define the maximum line width (adjust as needed)
max_line_width = 80

# Printing the formatted output
for paragraph in response.split('\n'):
wrapped_paragraph = wrap_line(paragraph, max_line_width)
for line in wrapped_paragraph:
print(f"{BOLD}{OKGREEN}{line}{ENDC}")
print()

def main():
parser = argparse.ArgumentParser(description="LocalMentor CLI tool")

subparsers_action = parser.add_subparsers(dest="action", help="Top-level actions")

# Parser for 'ask' action
do_parser = subparsers_action.add_parser("ask", help="Ask a question")
do_parser.add_argument("--prompt", required=True, help="Prompt")
ask_parser = subparsers_action.add_parser("ask", help="Ask a question")
ask_parser.add_argument("--prompt", required=True, help="Prompt")
ask_parser.add_argument("-nf", "--no-format", action="store_true", help="Skip formatting and pretty-print raw results")

args = parser.parse_args()

if args.action == "ask":
results = mentor(args.prompt)
pprint(results)
if args.no_format:
pprint(results)
else:
format_and_print_output(results)
else:
print("Invalid action")

Expand Down