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

docs(examples): refactor demo2 #836

Merged
merged 2 commits into from
Jan 24, 2024
Merged

docs(examples): refactor demo2 #836

merged 2 commits into from
Jan 24, 2024

Conversation

joshka
Copy link
Member

@joshka joshka commented Jan 17, 2024

Simplified a bunch of the logic in the demo2 example

  • Moved destroy mode to its own file.
  • Moved error handling to its own file.
  • Removed AppContext
  • Implemented Widget for &App. The app state is small enough that it
    doesn't matter here and we could just copy or clone the app state on
    every frame, but for larger apps this can be a significant performance
    improvement.
  • Made the tabs stateful
  • Made the term module just a collection of functions rather than a
    struct.
  • Changed to use color_eyre for error handling.
  • Changed keyboard shortcuts and rearranged the bottom bar.
  • Use strum for the tabs enum.

@joshka
Copy link
Member Author

joshka commented Jan 17, 2024

Includes #833

@joshka joshka changed the title feat(demo2): refactor demo2 to use RefWidget docs(examples): refactor demo2 Jan 19, 2024
@joshka
Copy link
Member Author

joshka commented Jan 19, 2024

Changed this to the impl Widget for &App approach. works fine.

joshka added a commit that referenced this pull request Jan 20, 2024
Many widgets can be rendered without changing their state.

This commit implements The `Widget` trait for various references to
widgets and changes their implementations to be immutable.

This allows us to render widgets without consuming them by passing a ref
to the widget when calling `Frame::render_widget()`.

```rust
// this might be stored in a struct
let paragraph = Paragraph::new("Hello world!");

let [left, right] = area.split(&Layout::horizontal([20, 20]));
frame.render_widget(&paragraph, left);
frame.render_widget(&paragraph, right); // we can reuse the widget
```

- Clear
- Block
- Tabs
- Sparkline
- Paragraph
- Gauge
- Calendar

Other widgets will be implemented in follow up commits.

Fixes: #164
Replaces PRs: #122 and
  #16
Enables: #132
Validated as a viable working solution by: #836
@joshka joshka force-pushed the refactor-demo2 branch 2 times, most recently from 09c2846 to d750b02 Compare January 24, 2024 15:34
Copy link
Member

@Valentin271 Valentin271 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor comments, otherwise looks good

examples/demo2/app.rs Outdated Show resolved Hide resolved
examples/demo2/app.rs Outdated Show resolved Hide resolved
examples/demo2/app.rs Outdated Show resolved Hide resolved
joshka added a commit that referenced this pull request Jan 24, 2024
Many widgets can be rendered without changing their state.

This commit implements The `Widget` trait for various references to
widgets and changes their implementations to be immutable.

This allows us to render widgets without consuming them by passing a ref
to the widget when calling `Frame::render_widget()`.

```rust
// this might be stored in a struct
let paragraph = Paragraph::new("Hello world!");

let [left, right] = area.split(&Layout::horizontal([20, 20]));
frame.render_widget(&paragraph, left);
frame.render_widget(&paragraph, right); // we can reuse the widget
```

- Clear
- Block
- Tabs
- Sparkline
- Paragraph
- Gauge
- Calendar

Other widgets will be implemented in follow up commits.

Fixes: #164
Replaces PRs: #122 and
  #16
Enables: #132
Validated as a viable working solution by: #836
joshka added a commit that referenced this pull request Jan 24, 2024
Many widgets can be rendered without changing their state.

This commit implements The `Widget` trait for references to
widgets and changes their implementations to be immutable.

This allows us to render widgets without consuming them by passing a ref
to the widget when calling `Frame::render_widget()`.

```rust
// this might be stored in a struct
let paragraph = Paragraph::new("Hello world!");

let [left, right] = area.split(&Layout::horizontal([20, 20]));
frame.render_widget(&paragraph, left);
frame.render_widget(&paragraph, right); // we can reuse the widget
```

Implemented for all widgets except BarChart (which has an implementation
that modifies the internal state and requires a rewrite to fix.

Other widgets will be implemented in follow up commits.

Fixes: #164
Replaces PRs: #122 and
#16
Enables: #132
Validated as a viable working solution by:
#836
Simplified a bunch of the logic in the demo2 example
- Moved destroy mode to its own file.
- Moved error handling to its own file.
- Removed AppContext
- Implemented Widget for &App. The app state is small enough that it
  doesn't matter here and we could just copy or clone the app state on
  every frame, but for larger apps this can be a significant performance
  improvement.
- Made the tabs stateful
- Made the term module just a collection of functions rather than a
  struct.
- Changed to use color_eyre for error handling.
- Changed keyboard shortcuts and rearranged the bottom bar.
- Use strum for the tabs enum.
Copy link

codecov bot commented Jan 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (815757f) 92.3% compared to head (d750b02) 92.3%.

❗ Current head d750b02 differs from pull request most recent head 077492a. Consider uploading reports for the commit 077492a to get more accurate results

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #836   +/-   ##
=====================================
  Coverage   92.3%   92.3%           
=====================================
  Files         61      61           
  Lines      16141   16096   -45     
=====================================
- Hits       14903   14872   -31     
+ Misses      1238    1224   -14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joshka joshka merged commit 847bacf into ratatui:main Jan 24, 2024
32 checks passed
@joshka joshka deleted the refactor-demo2 branch January 24, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants