Procházet zdrojové kódy

add builder pattern and update examples

Evan Almloff před 3 roky
rodič
revize
9d9aa33e25
2 změnil soubory, kde provedl 29 přidání a 6 odebrání
  1. 1 3
      examples/tui_color_test.rs
  2. 28 3
      packages/tui/src/config.rs

+ 1 - 3
examples/tui_color_test.rs

@@ -3,9 +3,7 @@ use dioxus::prelude::*;
 fn main() {
 fn main() {
     dioxus::tui::launch_cfg(
     dioxus::tui::launch_cfg(
         app,
         app,
-        dioxus::tui::Config {
-            rendering_mode: dioxus::tui::RenderingMode::Ansi,
-        },
+        dioxus::tui::Config::default().with_rendering_mode(dioxus::tui::RenderingMode::Ansi),
     );
     );
 }
 }
 
 

+ 28 - 3
packages/tui/src/config.rs

@@ -1,11 +1,36 @@
 #[derive(Clone, Copy)]
 #[derive(Clone, Copy)]
+#[non_exhaustive]
 pub struct Config {
 pub struct Config {
-    pub rendering_mode: RenderingMode,
+    pub(crate) rendering_mode: RenderingMode,
     /// Controls if the terminal quit when the user presses `ctrl+c`?
     /// Controls if the terminal quit when the user presses `ctrl+c`?
     /// To handle quiting on your own, use the [crate::TuiContext] root context.
     /// To handle quiting on your own, use the [crate::TuiContext] root context.
-    pub ctrl_c_quit: bool,
+    pub(crate) ctrl_c_quit: bool,
     /// Controls if the terminal should dislay anything, usefull for testing.
     /// Controls if the terminal should dislay anything, usefull for testing.
-    pub headless: bool,
+    pub(crate) headless: bool,
+}
+
+impl Config {
+    pub fn new() -> Self {
+        Self::default()
+    }
+
+    pub fn with_rendering_mode(self, rendering_mode: RenderingMode) -> Self {
+        Self {
+            rendering_mode,
+            ..self
+        }
+    }
+
+    pub fn with_ctrl_c_quit(self, ctrl_c_quit: bool) -> Self {
+        Self {
+            ctrl_c_quit,
+            ..self
+        }
+    }
+
+    pub fn with_headless(self, headless: bool) -> Self {
+        Self { headless, ..self }
+    }
 }
 }
 
 
 impl Default for Config {
 impl Default for Config {