rhysd / tui-textarea

Simple yet powerful multi-line text editor widget for ratatui and tui-rs
https://crates.io/crates/tui-textarea
MIT License
341 stars 63 forks source link

fix hard tab display #33

Closed pm100 closed 1 year ago

pm100 commented 1 year ago

hard tabs always insert 4 spaces

heres is minimal example switched to hard tab mode by adding textarea.set_hard_tab_indent(true);

image

this is the result of typing

the bs should be aligned, they are not

pm100 commented 1 year ago

Tbc,I will fix the tab_len ==0 in prperae_line. I was asking 'should I allow tabs in the case of hard tabs and tab len==0' I think the answer is yes. If the user wants to insert a hard tab, they should be allowed to

rhysd commented 1 year ago

@pm100 Do you mean removing this if guard? Then I'm not sure it should insert tab character. When a tab character is inserted to text buffer, the text displayed to the user has not changed. It would be confusing for the user.

https://github.com/rhysd/tui-textarea/blob/f4cd38ed500b84faaa2153b2131c529d0d508563/src/textarea.rs#L699-L701

Anyway, I feel this is an off-topic for this PR.

pm100 commented 1 year ago

@joshka these tests are in fact incorrect. They are verifying the incorrect behavior that this PR is fixing. But thank you for them. I will fix them

I was thinking 'system test' rather than 'unit test' when I said it was very hard to do.

Question: how did you create these? copilot, I have heard of it but not used it.

rhysd commented 1 year ago

Some kind of system test is easy to implement for this crate because we can use the test backend (or create our own dummy backend). The problem is that I have no time to implement the test cases.

rhysd commented 1 year ago

Thank you for the changes. I merged.

pm100 commented 1 year ago

Some kind of system test is easy to implement for this crate because we can use the test backend (or create our own dummy backend). The problem is that I have no time to implement the test cases.

Having done this on another project I think 'easy' is a little optimistic, certainly doable tho :)

joshka commented 1 year ago

In general, for testing ratatui widget rendering, I'd recommend avoiding using the TestBackend and prefer to save a few steps for every test and hit the buffer directly. The assert_buffer_eq! macro leads to a unit testing approach as the output is intentionally pretty similar to what you'd have to paste into your code to make the test run.

You might want just one TestBackend test though to ensure that everything works as expected, but the majority of the tests could easily use the following approach. Many of the newer tests written in Ratatui use this approach:

  1. setup the widget params
  2. Create an empty buffer of the right size to render into
  3. call widget.render(buffer.area, &mut buffer);
  4. call:
    assert_buffer_eq!(buffer, Buffer::with_lines(vec![
        "expected contents of buffer",
        "line 2",
    ]);

    Combined with an LLM it's pretty quick to crank these out fast.

rhysd commented 1 year ago

Having done this on another project I think 'easy' is a little optimistic, certainly doable tho :)

Yeah, maybe. It would be hard to maintain the tests as Rust code. I would prepare 3 files per each test case: