Skip to content

Commit 6e75dfe

Browse files
authored
Merge pull request #46 from museun/example_runner
make the doc tests actually compile
2 parents 09d0ef8 + a19e8e0 commit 6e75dfe

File tree

8 files changed

+74
-33
lines changed

8 files changed

+74
-33
lines changed

readme.md renamed to README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55

66
too -- a different kind of tui library
77

8+
## Feature flags
9+
10+
| Flag | Description | Default |
11+
| ---------- | ----------------------------------------------------------------------------------- | ------- |
12+
| `terminal` | enable the terminal backend | `true` |
13+
| `sync` | enable `Send`+`Sync` wrappers | `false` |
14+
| `profile` | enable [`profiling`](https://docs.rs/profiling/1.0.16/profiling/index.html) support | `false` |
15+
16+
---
17+
818
## Simple examples
919

1020
### Centering some text:
@@ -50,12 +60,12 @@ struct App {
5060

5161
impl App {
5262
fn view(&mut self, ui: &Ui) {
53-
ui.slider(&mut value);
63+
ui.slider(&mut self.value);
5464
}
5565
}
5666

5767
fn main() -> std::io::Result<()> {
58-
let mut app = App::default()
68+
let mut app = App::default();
5969
too::run(|ui| app.view(ui))
6070
}
6171
```
@@ -70,7 +80,7 @@ struct State {
7080
value: f32
7181
}
7282

73-
struct App ;
83+
struct App;
7484

7585
impl App {
7686
fn view(&self, state: &mut State, ui: &Ui) {
@@ -87,6 +97,8 @@ fn main() -> std::io::Result<()> {
8797

8898
Some pre-made views are provided in: [`too::views`](crate::views)
8999

100+
---
101+
90102
License: MIT OR Apache-2.0
91103

92104
[docs_badge]: https://docs.rs/too/badge.svg

src/lib.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
//!
1010
//! # Simple examples
1111
//! ## Centering some text:
12-
//! ```rust,no_run
12+
//! ```no_run
1313
//! fn main() -> std::io::Result<()> {
1414
//! too::run(|ui| {
1515
//! ui.center(|ui| ui.label("hello world"));
1616
//! })
1717
//! }
1818
//! ```
1919
//! ## A pair of buttons to increment and decrement a counter
20-
//! ```rust,no_run
20+
//! ```no_run
2121
//! fn main() -> std::io::Result<()> {
2222
//! let mut counter = 0;
2323
//! too::run(|ui| {
@@ -36,7 +36,7 @@
3636
//! }
3737
//! ```
3838
//! ## Storing state in a struct
39-
//! ```rust,no_run
39+
//! ```no_run
4040
//! use too::view::Ui;
4141
//!
4242
//! #[derive(Default)]
@@ -51,20 +51,20 @@
5151
//! }
5252
//!
5353
//! fn main() -> std::io::Result<()> {
54-
//! let mut app = App::default()
54+
//! let mut app = App::default();
5555
//! too::run(|ui| app.view(ui))
5656
//! }
5757
//! ```
5858
//! ## Storing state seperately from an application
59-
//! ```rust,no_run
59+
//! ```no_run
6060
//! use too::view::Ui;
6161
//!
6262
//! #[derive(Default)]
6363
//! struct State {
6464
//! value: f32
6565
//! }
6666
//!
67-
//! struct App ;
67+
//! struct App;
6868
//!
6969
//! impl App {
7070
//! fn view(&self, state: &mut State, ui: &Ui) {
@@ -103,9 +103,19 @@ mod str;
103103
pub use str::Str;
104104

105105
#[doc(hidden)]
106-
pub use compact_str::format_compact as __dont_use_this_because_semver;
106+
pub use compact_str::format_compact as ඞ_dont_use_this_because_semver;
107107

108108
#[cfg(feature = "terminal")]
109109
mod run;
110110
#[cfg(feature = "terminal")]
111111
pub use run::{application, run, RunConfig};
112+
113+
#[doc(hidden)]
114+
pub fn ඞrun_in_docs<R: 'static>(app: impl FnMut(&crate::view::Ui) -> R) -> std::io::Result<()> {
115+
view::State::default().build(math::rect(math::vec2(80, 25)), app);
116+
Ok(())
117+
}
118+
119+
#[doc = include_str!("../README.md")]
120+
#[cfg(doctest)]
121+
pub struct ReadmeDocTests;

src/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ where
6060
#[macro_export]
6161
macro_rules! format_str {
6262
($($arg:tt)*) => {
63-
$crate::Str::from($crate::__dont_use_this_because_semver!($($arg)*))
63+
$crate::Str::from($crate::ඞ_dont_use_this_because_semver!($($arg)*))
6464
}
6565
}

src/view/builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use super::{EventCtx, Handled, Interest, IntrinsicSize, Layout, Render, Response
2020
/// 'v lifetime. Views cannot borrow data as they must be 'static
2121
///
2222
/// ### Builders can also be views
23-
/// ```rust,no_run
23+
/// ```rust
24+
/// use too::view::{Builder, View, Ui};
25+
///
2426
/// // views must be `Debug`, builders don't have to be
2527
/// #[derive(Debug)]
2628
/// struct Foo;

src/view/debug.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ fn evaluate<R: 'static>(
644644
/// Generate a pretty tree of the views (nodes) for this application
645645
///
646646
/// Example:
647-
/// ```rust,no_run
647+
/// ```rust
648+
/// use too::{layout::Align2, views::fill};
649+
///
648650
/// too::view::debug::pretty_tree(|ui| {
649651
/// ui.center(|ui| ui.label("hello world"));
650652
///
@@ -653,10 +655,10 @@ fn evaluate<R: 'static>(
653655
/// });
654656
///
655657
/// ui.show(fill("#F0F", [10.0, 10.0]));
656-
/// })
658+
/// });
657659
/// ```
658660
/// produces:
659-
/// ```text
661+
/// ```text,no_run
660662
/// ┌──────────────┐
661663
/// │ 1v1 Root │
662664
/// ╞══════════════╡
@@ -726,7 +728,9 @@ pub fn pretty_tree<R: 'static>(app: impl FnMut(&Ui) -> R) -> TreeOutput {
726728
/// Generate a compact tree of the views (nodes) for this application
727729
///
728730
/// Example:
729-
/// ```rust,no_run
731+
/// ```rust
732+
/// use too::{layout::Align2, views::fill};
733+
///
730734
/// too::view::debug::compact_tree(|ui| {
731735
/// ui.center(|ui| ui.label("hello world"));
732736
///
@@ -735,10 +739,10 @@ pub fn pretty_tree<R: 'static>(app: impl FnMut(&Ui) -> R) -> TreeOutput {
735739
/// });
736740
///
737741
/// ui.show(fill("#F0F", [10.0, 10.0]));
738-
/// })
742+
/// });
739743
/// ```
740744
/// produces:
741-
/// ```text
745+
/// ```text,no_run
742746
/// Root(1v1)
743747
/// ├─ Aligned(2v1)
744748
/// │ └─ Label(3v1)
@@ -761,7 +765,9 @@ pub fn compact_tree<R: 'static>(app: impl FnMut(&Ui) -> R) -> TreeOutput {
761765
/// This includes the rectangles for each view
762766
///
763767
/// Example:
764-
/// ```rust,no_run
768+
/// ```rust
769+
/// use too::{layout::Align2, views::fill};
770+
///
765771
/// too::view::debug::compact_tree_sizes(|ui| {
766772
/// ui.center(|ui| ui.label("hello world"));
767773
///
@@ -770,10 +776,10 @@ pub fn compact_tree<R: 'static>(app: impl FnMut(&Ui) -> R) -> TreeOutput {
770776
/// });
771777
///
772778
/// ui.show(fill("#F0F", [10.0, 10.0]));
773-
/// })
779+
/// });
774780
/// ```
775781
/// produces:
776-
/// ```text
782+
/// ```text,no_run
777783
/// Root(1v1) ║ x: 0 y: 0 ┊ w: 80 h: 25
778784
/// ├─ Aligned(2v1) ║ x: 0 y: 0 ┊ w: 80 h: 25
779785
/// │ └─ Label(3v1) ║ x: 35 y: 12 ┊ w: 11 h: 1
@@ -794,19 +800,21 @@ pub fn compact_tree_sizes<R: 'static>(app: impl FnMut(&Ui) -> R) -> TreeOutput {
794800
/// Generate the paint list this application would do
795801
///
796802
/// Example:
797-
/// ```rust,no_run
798-
/// too::view::render_tree(|ui| {
803+
/// ```rust
804+
/// use too::{layout::Align2, views::fill};
805+
///
806+
/// too::view::debug::render_tree(|ui| {
799807
/// ui.center(|ui| ui.label("hello world"));
800808
///
801809
/// ui.aligned(Align2::RIGHT_TOP, |ui| {
802810
/// ui.button("click me");
803811
/// });
804812
///
805813
/// ui.show(fill("#F0F", [10.0, 10.0]));
806-
/// })
814+
/// });
807815
/// ```
808816
/// produces:
809-
/// ```rust,no_run
817+
/// ```text,no_run
810818
/// ViewId(3v1): Text { rect: { x: 35, y: 12, w: 11, h: 1 }, shape: TextShape { label: "hello world", fg: Set(rgb(255, 255, 255, 255)), bg: Reuse, attribute: None } }
811819
/// ViewId(5v1): FillBg { rect: { x: 70, y: 0, w: 10, h: 1 }, color: rgb(77, 77, 77, 255) }
812820
/// ViewId(5v1): Text { rect: { x: 71, y: 0, w: 8, h: 1 }, shape: TextShape { label: "click me", fg: Set(rgb(255, 255, 255, 255)), bg: Reuse, attribute: None } }

src/view/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'a> Filterable for Layout<'a> {
2626
/// - return that size
2727
///
2828
/// ## In your code:
29-
/// ```rust,no_run
29+
/// ```compile_fail
3030
/// let current = layout.nodes.get_current();
3131
/// let mut size = Size::ZERO;
3232
/// for &child in &current.children {

src/view/state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ use super::{
2929
/// You can have multiple state, they are built with a [`Rect`] as their constraining bounds.
3030
///
3131
/// ## State life cycles
32-
/// ```rust,no_run
32+
/// ```compile_fail
33+
/// use too::view::State;
34+
///
3335
/// let mut state = State::new(Palette::default(), Animations::default());
3436
///
3537
/// // provide a way to read events from a backend

src/view/style.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use crate::renderer::Rgba;
99
/// ## Convention
1010
///
1111
/// Conventional types:
12-
/// ```rust,no_run
12+
/// ```ignore
1313
/// struct Style;
1414
/// type Class = fn(&Palette) -> Style;
15-
/// fn class(mut self, Class) -> Self;
16-
/// fn style(mut self, Style) -> Self;
15+
/// fn class(mut self, class: Class) -> Self;
16+
/// fn style(mut self, style: Style) -> Self;
1717
/// ```
1818
///
1919
/// 3## Description
@@ -33,7 +33,13 @@ use crate::renderer::Rgba;
3333
///
3434
/// ### Example
3535
/// A simple example:
36-
/// ```rust,no_run
36+
/// ```rust
37+
/// use too::{
38+
/// layout::Axis,
39+
/// renderer::{Rgba, Pixel},
40+
/// view::{Palette, StyleKind, Builder, View, Render}
41+
/// };
42+
///
3743
/// #[derive(Copy, Clone, Debug)]
3844
/// struct MyStyle {
3945
/// background: Rgba,
@@ -114,8 +120,9 @@ use crate::renderer::Rgba;
114120
/// render.fill_with(Pixel::new(style.fill).bg(style.background));
115121
/// }
116122
/// }
117-
///
118-
/// // then a user can do:
123+
///```
124+
/// Then a user can do:
125+
/// ```ignore
119126
/// // for a deferred style:
120127
/// ui.show(builder().class(MyStyle::hash_at));
121128
/// // or for a pre-computed style

0 commit comments

Comments
 (0)