|
| 1 | +//! Console Services Usage Examples |
| 2 | +//! |
| 3 | +//! This module demonstrates how to use ConsoleServices for text output and console management. |
| 4 | +//! |
| 5 | +//! ## Examples Included |
| 6 | +//! |
| 7 | +//! - **BasicConsoleOutput**: Simple text output to console |
| 8 | +//! - **FormattedConsoleOutput**: Formatted output with cursor positioning |
| 9 | +//! - **InteractiveConsole**: Console manipulation (clear, cursor control) |
| 10 | +//! |
| 11 | +//! Copyright (c) Microsoft Corporation. |
| 12 | +//! |
| 13 | +//! SPDX-License-Identifier: Apache-2.0 |
| 14 | +
|
| 15 | +extern crate alloc; |
| 16 | + |
| 17 | +use patina::{ |
| 18 | + component::{IntoComponent, service::Service}, |
| 19 | + error::Result, |
| 20 | +}; |
| 21 | +use patina_uefi_services::service::console::ConsoleServices; |
| 22 | + |
| 23 | +/// Example component demonstrating basic console output. |
| 24 | +/// |
| 25 | +/// This component shows the simplest use case: writing text to the console. |
| 26 | +/// The ConsoleServices dependency is requested via the component's entry point. |
| 27 | +#[derive(IntoComponent)] |
| 28 | +pub struct BasicConsoleOutput; |
| 29 | + |
| 30 | +impl BasicConsoleOutput { |
| 31 | + /// Component entry point that outputs a simple message to the console. |
| 32 | + /// |
| 33 | + /// # Arguments |
| 34 | + /// |
| 35 | + /// * `console` - The console services for text output |
| 36 | + /// |
| 37 | + /// # Returns |
| 38 | + /// |
| 39 | + /// * `Result<()>` - Success or error status |
| 40 | + pub fn entry_point(self, console: Service<dyn ConsoleServices>) -> Result<()> { |
| 41 | + // Simple console output |
| 42 | + console.output_string("Hello from Patina Console Services!\r\n")?; |
| 43 | + console.output_string("This is a basic console output example.\r\n")?; |
| 44 | + |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Example component demonstrating formatted console output with cursor positioning. |
| 50 | +/// |
| 51 | +/// This component shows how to use cursor positioning and formatted output |
| 52 | +/// to create structured console displays. |
| 53 | +#[derive(IntoComponent)] |
| 54 | +pub struct FormattedConsoleOutput; |
| 55 | + |
| 56 | +impl FormattedConsoleOutput { |
| 57 | + /// Component entry point that demonstrates formatted console output. |
| 58 | + /// |
| 59 | + /// # Arguments |
| 60 | + /// |
| 61 | + /// * `console` - The console services for text output and cursor control |
| 62 | + /// |
| 63 | + /// # Returns |
| 64 | + /// |
| 65 | + /// * `Result<()>` - Success or error status |
| 66 | + pub fn entry_point(self, console: Service<dyn ConsoleServices>) -> Result<()> { |
| 67 | + // Output a header |
| 68 | + console.output_string("\r\n=== Formatted Console Example ===\r\n\r\n")?; |
| 69 | + |
| 70 | + // Create a simple table with cursor positioning |
| 71 | + console.set_cursor_position(0, 5)?; |
| 72 | + console.output_string("Column 1")?; |
| 73 | + |
| 74 | + console.set_cursor_position(20, 5)?; |
| 75 | + console.output_string("Column 2")?; |
| 76 | + |
| 77 | + console.set_cursor_position(40, 5)?; |
| 78 | + console.output_string("Column 3")?; |
| 79 | + |
| 80 | + // Add data rows |
| 81 | + console.set_cursor_position(0, 6)?; |
| 82 | + console.output_string("Data A")?; |
| 83 | + |
| 84 | + console.set_cursor_position(20, 6)?; |
| 85 | + console.output_string("Data B")?; |
| 86 | + |
| 87 | + console.set_cursor_position(40, 6)?; |
| 88 | + console.output_string("Data C")?; |
| 89 | + |
| 90 | + // Get final cursor position to show where we ended up |
| 91 | + // Query cursor position for demonstration |
| 92 | + let (_col, row) = console.get_cursor_position()?; |
| 93 | + console.set_cursor_position(0, row + 2)?; |
| 94 | + console.output_string("Table complete!\r\n")?; |
| 95 | + |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Example component demonstrating interactive console manipulation. |
| 101 | +/// |
| 102 | +/// This component shows how to use console management features like |
| 103 | +/// clearing the screen, cursor visibility, and positioning. |
| 104 | +#[derive(IntoComponent)] |
| 105 | +pub struct InteractiveConsole; |
| 106 | + |
| 107 | +impl InteractiveConsole { |
| 108 | + /// Component entry point that demonstrates console manipulation. |
| 109 | + /// |
| 110 | + /// # Arguments |
| 111 | + /// |
| 112 | + /// * `console` - The console services for full console control |
| 113 | + /// |
| 114 | + /// # Returns |
| 115 | + /// |
| 116 | + /// * `Result<()>` - Success or error status |
| 117 | + pub fn entry_point(self, console: Service<dyn ConsoleServices>) -> Result<()> { |
| 118 | + // Clear the screen first |
| 119 | + console.clear_screen()?; |
| 120 | + |
| 121 | + // Show cursor manipulation |
| 122 | + console.output_string("Demonstrating cursor control...\r\n")?; |
| 123 | + |
| 124 | + // Hide cursor |
| 125 | + console.enable_cursor(false)?; |
| 126 | + console.output_string("Cursor is now hidden\r\n")?; |
| 127 | + |
| 128 | + // Show cursor again |
| 129 | + console.enable_cursor(true)?; |
| 130 | + console.output_string("Cursor is now visible\r\n")?; |
| 131 | + |
| 132 | + // Draw a simple pattern using cursor positioning |
| 133 | + console.output_string("\r\nDrawing a pattern:\r\n\r\n")?; |
| 134 | + for i in 0..5 { |
| 135 | + console.set_cursor_position(i * 4, 10 + i)?; |
| 136 | + console.output_string("*")?; |
| 137 | + } |
| 138 | + |
| 139 | + // Return cursor to bottom |
| 140 | + console.set_cursor_position(0, 16)?; |
| 141 | + console.output_string("\r\nConsole demonstration complete!\r\n")?; |
| 142 | + |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | +} |
0 commit comments