appcui 0.5.0

A feature-rich and cross-platform TUI/CUI framework for Rust, enabling modern terminal-based applications on Windows, Linux, and macOS. Includes built-in UI components like buttons, menus, list views, tree views, checkboxes, and more. Perfect for building fast and interactive CLI tools and text-based interfaces.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::backend;
use crate::graphics::*;
use crate::system::*;
use crate::ui::common::traits::*;
use crate::ui::common::*;

pub struct InternalBuilder {
    pub(crate) size: Option<Size>,
    pub(crate) backend: Option<crate::backend::Type>,
    pub(crate) debug_script: Option<String>,
    pub(crate) title: Option<String>,
    pub(crate) desktop_manager: Option<ControlManager>,
    pub(crate) has_app_bar: bool,
    pub(crate) has_command_bar: bool,
    pub(crate) single_window: bool,
    pub(crate) theme: Theme,
    pub(crate) max_timer_count: u8,
    pub(crate) log_file: Option<String>,
    pub(crate) log_append: bool,
    pub(crate) use_color_schema: bool,
    pub(crate) restore_screen: bool,
    #[cfg(test)]
    pub(crate) runtime_check: Option<fn()>,
}
impl InternalBuilder {
    pub(crate) fn new() -> Self {
        Self {
            size: None,
            title: None,
            backend: None,
            debug_script: None,
            desktop_manager: None,
            has_app_bar: false,
            has_command_bar: false,
            single_window: false,
            max_timer_count: 4,
            theme: Theme::new(Themes::Default),
            log_file: None,
            log_append: false,
            use_color_schema: true,
            restore_screen: true,
            #[cfg(test)]
            runtime_check: None,
        }
    }
    #[inline(always)]
    pub(crate) fn build(self) -> Result<App, Error> {
        #[cfg(test)]
        {
            let check = self.runtime_check;
            let app = App::create(self)?;
            if let Some(check) = check {
                check();
            }
            Ok(app)
        }
        #[cfg(not(test))]
        {
            App::create(self)
        }
    }
    #[inline(always)]
    pub(crate) fn size(&mut self, terminal_size: Size) {
        self.size = Some(terminal_size);
    }
    #[inline(always)]
    pub(crate) fn title(&mut self, title: &str) {
        self.title = Some(String::from(title));
    }
    #[inline(always)]
    pub(crate) fn app_bar(&mut self) {
        self.has_app_bar = true;
    }
    #[inline(always)]
    pub(crate) fn command_bar(&mut self) {
        self.has_command_bar = true;
    }
    #[inline(always)]
    pub(crate) fn single_window(&mut self) {
        self.single_window = true;
    }
    #[inline(always)]
    pub(crate) fn desktop<T>(&mut self, desktop: T)
    where
        T: Control + DesktopControl + 'static,
    {
        self.desktop_manager = Some(ControlManager::new(desktop));
    }
    #[inline(always)]
    pub(crate) fn theme(&mut self, theme: Theme) {
        self.theme = theme;
    }
    #[inline(always)]
    pub(crate) fn timers_count(&mut self, count: u8) {
        self.max_timer_count = count.max(1);
    }
    #[inline(always)]
    pub(crate) fn log_file(&mut self, name: &str, append: bool) {
        self.log_file = Some(String::from(name));
        self.log_append = append;
    }
    #[inline(always)]
    pub(crate) fn color_schema(&mut self, enabled: bool) {
        self.use_color_schema = enabled;
    }
    #[inline(always)]
    pub(crate) fn restore_screen(&mut self, enable: bool) {
        self.restore_screen = enable;
    }
    #[inline(always)]
    pub(crate) fn backend(&mut self, backend: backend::Type) {
        self.backend = Some(backend);
    }
    #[inline(always)]
    pub(crate) fn debug_script(&mut self, script: &str) {
        self.debug_script = Some(String::from(script));
    }
    #[inline(always)]
    #[cfg(test)]
    pub(crate) fn runtime_check(&mut self, check: fn()) {
        self.runtime_check = Some(check);
    }
}

macro_rules! impl_terminal_builder_methods {
    () => {
        /// Sets the size of the terminal in character cells.
        ///
        /// If not specified, the backend uses the current terminal size (or a
        /// backend-specific default).
        ///
        /// # Parameters
        /// * `terminal_size` - Width and height of the terminal, in character cells.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().size(Size::new(80, 25));
        /// ```
        #[inline(always)]
        pub fn size(mut self, terminal_size: crate::graphics::Size) -> Self {
            self.builder.size(terminal_size);
            self
        }

        /// Sets the title shown by the terminal window or browser tab.
        ///
        /// Support depends on the selected backend. If not specified, a backend
        /// default title is used.
        ///
        /// # Parameters
        /// * `title` - The application title.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().title("My Application");
        /// ```
        #[inline(always)]
        pub fn title(mut self, title: &str) -> Self {
            self.builder.title(title);
            self
        }

        /// Sets the log file used when the crate is compiled in debug mode.
        ///
        /// This option has no effect in release builds.
        ///
        /// # Parameters
        /// * `name` - Path of the log file.
        /// * `append` - If `true`, new logs are appended; if `false`, the file is overwritten.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().log_file("appcui.log", false);
        /// ```
        #[inline(always)]
        pub fn log_file(mut self, name: &str, append: bool) -> Self {
            self.builder.log_file(name, append);
            self
        }

        /// Enables or disables the terminal color schema.
        ///
        /// When enabled (the default), the backend may map AppCUI colors through
        /// the terminal's color schema. Disable this to keep the exact colors
        /// defined by the theme.
        ///
        /// # Parameters
        /// * `enabled` - `true` to use the terminal color schema, `false` to disable it.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().color_schema(false);
        /// ```
        #[inline(always)]
        pub fn color_schema(mut self, enabled: bool) -> Self {
            self.builder.color_schema(enabled);
            self
        }

        /// Controls whether the original screen is restored when the application ends.
        ///
        /// When enabled (the default), the backend attempts to restore the original
        /// screen content and cursor position. When disabled, the screen is cleared
        /// on exit.
        ///
        /// # Parameters
        /// * `enable` - `true` to restore the original screen, `false` to clear it.
        ///
        /// # Remarks
        /// Not all backends can restore the original screen. Backends without this
        /// support always clear the screen when the application ends.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().restore_screen(false);
        /// ```
        #[inline(always)]
        pub fn restore_screen(mut self, enable: bool) -> Self {
            self.builder.restore_screen(enable);
            self
        }

        /// Selects the terminal backend used to render the application.
        ///
        /// If not specified, AppCUI picks a backend appropriate for the current
        /// platform and enabled crate features.
        ///
        /// # Parameters
        /// * `backend` - The [`crate::backend::Type`] to use.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// #[cfg(target_os = "windows")]
        /// let _builder = App::new().backend(appcui::backend::Type::WindowsVT);
        /// #[cfg(not(target_os = "windows"))]
        /// let _builder = App::new();
        /// ```
        #[inline(always)]
        pub fn backend(mut self, backend: crate::backend::Type) -> Self {
            self.builder.backend(backend);
            self
        }

        /// Configures a debug script that simulates input for unit tests.
        ///
        /// Each line of `script` is a command executed in order against a virtual
        /// terminal. After the last command the application ends.
        ///
        /// Combine this method with [`size`](Self::size) to set the simulated
        /// terminal dimensions (`width` and `height`).
        ///
        /// # Parameters
        /// * `script` - Commands to execute, one per line.
        ///
        /// # Debug commands
        ///
        /// **Mouse related commands**
        /// * `Mouse.Hold(x,y,button)` simulates an event where the mouse button is being pressed while the mouse is located at a specific position on screen. The parameters `x` and `y` are a screen position, while the parameter `button` is one of `left`, `right` or `center`
        /// * `Mouse.Release(x,y)` simulates the release of all mouse buttons while the mouse is located at a specific screen position.
        /// * `Mouse.Click(x,y,button)` simulates a click (hold an release)
        /// * `Mouse.Move(x,y)` simulates the movement of a mouse to coordonates (x,y). No mouse button are being pressed.
        /// * `Mouse.Drag(x1,y1,x2,y2)` simulates the movement of a mouse from (x1,y1) to (x2,y2) while the left button is being pressed
        /// * `Mouse.Wheel(x,y,direction,times)` simulates the wheel mouse being rotated into a direction (one of `top`, `bottom`, `left`, `right`) for a number of times. The `times` parameter must be biggen than 0.
        ///
        /// **Key related commands**
        /// * `Key.Pressed(key)` where key can be any combination of keys
        ///
        /// **Paint related commands**
        /// * `Paint(name)` paints the current virtual screen into the current screen using ANSI codes.
        /// * `Paint.Enable(value)` enables or disables painting. `value` is a boolean value (**true** or **false**). If set to **false** all subsequent calls to command `Paint` will be ignored.
        ///
        /// **System events**
        /// * `Resize(width,height)` simulates a resize of the virtual terminal to the size represented by `width` and `height` parameters
        ///
        /// **Validation commands**
        /// * `CheckHash(hash)` checks if the hash computer over the current virtual screen is as expected. If not it will panic. This is useful for unit testing.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// let script = "
        ///     Paint(initial)
        ///     Key.Pressed(Escape)
        /// ";
        /// App::new()
        ///     .size(Size::new(60, 10))
        ///     .debug_script(script)
        ///     .window(|| window!("'Test',a:c,w:20,h:6"));
        /// ```
        #[inline(always)]
        pub fn debug_script(mut self, script: &str) -> Self {
            self.builder.debug_script(script);
            self
        }

        #[inline(always)]
        #[cfg(test)]
        #[allow(dead_code)]
        pub(crate) fn runtime_check(mut self, check: fn()) -> Self {
            self.builder.runtime_check(check);
            self
        }
    };
}

macro_rules! impl_ui_builder_methods {
    () => {
        /// Enables the application bar at the top of the desktop. 
        ///
        /// The app bar hosts menus and other application-wide commands. It is
        /// disabled by default.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().app_bar();
        /// ```
        #[inline(always)]
        pub fn app_bar(mut self) -> Self {
            self.builder.app_bar();
            self
        }

        /// Enables the command bar at the bottom of the desktop.
        ///
        /// The command bar shows keyboard shortcuts associated with the focused
        /// control. It is disabled by default.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().command_bar();
        /// ```
        #[inline(always)]
        pub fn command_bar(mut self) -> Self {
            self.builder.command_bar();
            self
        }

        /// Sets the theme used by the desktop and all controls.
        ///
        /// If not specified, the default theme is used. To change the theme after
        /// the application is running, call [`App::set_theme`](crate::system::App::set_theme).
        ///
        /// # Parameters
        /// * `theme` - The [`crate::system::Theme`] to apply at startup.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().theme(Theme::new(Themes::DarkGray));
        /// ```
        #[inline(always)]
        pub fn theme(mut self, theme: crate::system::Theme) -> Self {
            self.builder.theme(theme);
            self
        }

        /// Sets the maximum number of timers the application can use.
        ///
        /// The value is clamped to at least `1`. The default is `4`.
        ///
        /// # Parameters
        /// * `count` - Maximum number of simultaneous timers.
        ///
        /// # Examples
        ///
        /// ```rust, no_run
        /// use appcui::prelude::*;
        ///
        /// App::new().timers_count(8);
        /// ```
        #[inline(always)]
        pub fn timers_count(mut self, count: u8) -> Self {
            self.builder.timers_count(count);
            self
        }
    };
}

pub(crate) use impl_terminal_builder_methods;
pub(crate) use impl_ui_builder_methods;