trueagi-io / metta-wam

A Hyperon MeTTa Interpreter/Transpilier that targets the Warren Abstract Machine
7 stars 9 forks source link

REPL Prompt Overwritten at Odd Times When Using Left Arrow Key or Pasting Text #90

Closed TeamSPoon closed 1 week ago

TeamSPoon commented 3 weeks ago

In the MeTTaLog REPL, there is an issue where the prompt can be overwritten at unpredictable times. This often occurs when using the left arrow key to navigate back toward the prompt or when pasting text into the REPL. The result is that the prompt becomes partially or fully erased, leading to a confusing and inconsistent user experience.

Steps to Reproduce:

  1. Open the MeTTaLog REPL.
  2. Type some text at the prompt.
  3. Use the left arrow key to move the cursor back toward the start of the input.
  4. Alternatively, paste a block of text into the REPL.
  5. In both cases, the prompt may be overwritten or displaced, but the behavior is inconsistent and may happen at odd times.

Expected Behavior:

Actual Behavior:

Priority and Impact:

This issue affects the usability of the REPL, particularly when pasting text or editing input. The inconsistent nature of the problem makes it more frustrating for users, as they may not be able to predict when the prompt will be affected. While it doesn’t cause crashes or data loss, it significantly impacts the user experience and makes the REPL harder to use during complex interactions.

TeamSPoon commented 3 weeks ago

If I were addressing the REPL prompt overwrite issue and history management, I would start by building a minimal echo REPL in SWI-Prolog to better understand and test input handling, cursor management, and history behavior. This approach allows for isolated debugging and ensures clean integration back into the larger system.

Create a Minimal SWI-Prolog REPL

Starting with a simple echo REPL that reads user input, echoes it back, and loops until you exit:

% A minimal echo REPL in SWI-Prolog with setup_call_cleanup/3 for prompt management

repl :-
    setup_call_cleanup(
        prompt(WAS,'REPL> '),
        run_repl,
        prompt(_,WAS)
    ).
run_repl :-
    repeat,
    read_input(Line),
    handle_input(Line),
    Line == "exit".

read_input(Line) :-
    read_line_to_string(user_input, Line).

handle_input("exit") :- !.
handle_input(Line) :-
    format("You typed: ~w~n", [Line]),
    fail.  % Fail to loop back to repeat/0

% Start the REPL
:- repl.

How This Works:

maybe for https://github.com/trueagi-io/metta-wam/issues/91


handle_input(Line) :-
    (Line \== "exit" -> readline:add_history(Line) ; true),
    format("You typed: ~w~n", [Line]),
    fail.

You can load and save history across sessions like this:

:- initialization(load_history).
:- at_halt(save_history).

load_history :-
    readline:read_history('~/.config/metta/repl_history.txt').

save_history :-
    readline:write_history('~/.config/metta/repl_history.txt').

This minimal REPL is a good environment to test:

TeamSPoon commented 3 weeks ago

@TeamSPoon opened the bug, @royward fixed it. I confirm the fix for me worked. @drspro needed it as badly as I did (well everyone did). If it works for him, he can close the issue.

TeamSPoon commented 1 week ago

Closing - good fix - i think we are all good