AlignOptions

Struct AlignOptions 

Source
pub struct AlignOptions {
    pub align: Alignment,
    pub split: String,
    pub pad: char,
}
Expand description

Configuration options for text alignment operations.

This struct allows you to customize how text alignment is performed, including the alignment direction, line separator, and padding character.

§Examples

use ansi_align::{AlignOptions, Alignment, ansi_align_with_options};

// Basic usage with default settings
let opts = AlignOptions::new(Alignment::Center);

// Customized options
let opts = AlignOptions::new(Alignment::Right)
    .with_split("|")
    .with_pad('.');

let text = "short|longer text";
let result = ansi_align_with_options(text, &opts);

Fields§

§align: Alignment

The alignment type (left, center, right)

§split: String

The string to split lines on (default: “\n”)

§pad: char

The padding character to use (default: “ “)

Implementations§

Source§

impl AlignOptions

Source

pub fn new(align: Alignment) -> Self

Creates new alignment options with the specified alignment direction.

Uses default values for split string ("\n") and padding character (' ').

§Arguments
  • align - The alignment direction to use
§Examples
use ansi_align::{AlignOptions, Alignment};

let opts = AlignOptions::new(Alignment::Center);
Examples found in repository?
examples/cli_tool.rs (line 86)
66fn main() -> Result<(), Box<dyn std::error::Error>> {
67    let cli = Cli::parse();
68
69    if cli.demo {
70        show_demo();
71        return Ok(());
72    }
73
74    let input_text = get_input_text(&cli)?;
75
76    if input_text.trim().is_empty() {
77        if !cli.quiet {
78            eprintln!("{}", "⚠️  No input text provided".yellow());
79        }
80        return Ok(());
81    }
82
83    let split_str = cli.split.replace("\\n", "\n").replace("\\t", "\t");
84
85    let alignment = cli.align.into();
86    let options = AlignOptions::new(alignment)
87        .with_split(split_str)
88        .with_pad(cli.pad);
89
90    let aligned_text = ansi_align_with_options(&input_text, &options);
91
92    if cli.border {
93        print_with_border(&aligned_text, &cli);
94    } else {
95        println!("{}", aligned_text);
96    }
97
98    Ok(())
99}
100
101fn get_input_text(cli: &Cli) -> Result<String, Box<dyn std::error::Error>> {
102    if let Some(file_path) = &cli.file {
103        // Read from file
104        Ok(fs::read_to_string(file_path)?)
105    } else if let Some(text) = &cli.text {
106        if text == "-" {
107            // Read from stdin
108            let mut buffer = String::new();
109            io::stdin().read_to_string(&mut buffer)?;
110            Ok(buffer)
111        } else {
112            // Process escape sequences in command line text
113            Ok(text.replace("\\n", "\n").replace("\\t", "\t"))
114        }
115    } else {
116        // Read from stdin if no text provided
117        let mut buffer = String::new();
118        io::stdin().read_to_string(&mut buffer)?;
119        Ok(buffer)
120    }
121}
122
123fn print_with_border(text: &str, cli: &Cli) {
124    let lines: Vec<&str> = text.split('\n').collect();
125    let max_width = lines
126        .iter()
127        .map(|line| string_width::string_width(line))
128        .max()
129        .unwrap_or(0);
130
131    let border_width = max_width + 2; // 1 space padding on each side
132
133    if !cli.quiet {
134        // Top border
135        println!(
136            "{}{}{}",
137            "┌".cyan().bold(),
138            "─".repeat(border_width).cyan(),
139            "┐".cyan().bold()
140        );
141    }
142
143    // Content with side borders - text is already aligned
144    for line in lines {
145        if !cli.quiet {
146            print!("{}", "│ ".cyan().bold());
147        }
148        print!("{}", line);
149        if !cli.quiet {
150            print!("{}", " │".cyan().bold());
151        }
152        println!();
153    }
154
155    if !cli.quiet {
156        // Bottom border
157        println!(
158            "{}{}{}",
159            "└".cyan().bold(),
160            "─".repeat(border_width).cyan(),
161            "┘".cyan().bold()
162        );
163    }
164}
165
166fn show_demo() {
167    println!(
168        "{}",
169        "🎨 ansi-align Demo - Beautiful Text Alignment"
170            .bold()
171            .magenta()
172    );
173    println!();
174
175    // Demo 1: Basic alignment
176    println!("{}", "📝 Basic Alignment:".bold().blue());
177    let basic_text = "Hello\nWorld\nRust";
178
179    println!("\n{}", "Left:".green());
180    println!(
181        "{}",
182        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Left))
183    );
184
185    println!("\n{}", "Center:".green());
186    println!(
187        "{}",
188        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Center))
189    );
190
191    println!("\n{}", "Right:".green());
192    println!(
193        "{}",
194        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Right))
195    );
196
197    println!("\n{}", "─".repeat(50).dimmed());
198
199    // Demo 2: ANSI colors
200    println!("\n{}", "🌈 ANSI Color Support:".bold().blue());
201    let colored_text = format!(
202        "{}\n{}\n{}",
203        "Red Text".red().bold(),
204        "Green Text".green().italic(),
205        "Blue Text".blue().underline()
206    );
207
208    println!("\n{}", "Center aligned with colors:".green());
209    println!(
210        "{}",
211        ansi_align_with_options(&colored_text, &AlignOptions::new(Alignment::Center))
212    );
213
214    println!("\n{}", "─".repeat(50).dimmed());
215
216    // Demo 3: Unicode support
217    println!("\n{}", "🌏 Unicode Support:".bold().blue());
218    let unicode_text = "古\n古古古\nHello 世界";
219
220    println!("\n{}", "Right aligned Unicode:".green());
221    println!(
222        "{}",
223        ansi_align_with_options(unicode_text, &AlignOptions::new(Alignment::Right))
224    );
225
226    println!("\n{}", "─".repeat(50).dimmed());
227
228    // Demo 4: Custom options
229    println!("\n{}", "⚙️  Custom Options:".bold().blue());
230    let custom_text = "Name|Age|Location";
231    let options = AlignOptions::new(Alignment::Center)
232        .with_split("|")
233        .with_pad('.');
234
235    println!("\n{}", "Custom separator '|' and padding '.':".green());
236    println!("{}", ansi_align_with_options(custom_text, &options));
237
238    println!("\n{}", "─".repeat(50).dimmed());
239
240    // Demo 5: Menu example
241    println!("\n{}", "📋 Menu Example:".bold().blue());
242    let menu = format!(
243        "{}\n{}\n{}\n{}",
244        "🏠 Home".cyan(),
245        "📋 About Us".yellow(),
246        "📞 Contact".green(),
247        "⚙️ Settings".magenta()
248    );
249
250    println!("\n{}", "Center aligned menu:".green());
251    println!(
252        "{}",
253        ansi_align_with_options(&menu, &AlignOptions::new(Alignment::Center))
254    );
255
256    println!(
257        "\n{}",
258        "✨ Try it yourself with different options!".bold().green()
259    );
260    println!("{}", "Examples:".dimmed());
261    println!(
262        "{}",
263        "  cargo run --example cli_tool -- \"Hello\\nWorld\" --align center".dimmed()
264    );
265    println!(
266        "{}",
267        "  echo \"Line 1\\nLonger Line 2\" | cargo run --example cli_tool -- - --border".dimmed()
268    );
269    println!(
270        "{}",
271        "  cargo run --example cli_tool -- --file README.md --align right --pad '.'".dimmed()
272    );
273}
More examples
Hide additional examples
examples/basic_usage.rs (line 41)
3fn main() {
4    println!("=== Basic Alignment Examples ===\n");
5
6    // Simple text alignment
7    let text = "Hello\nWorld\nRust";
8
9    println!("Original text:");
10    println!("{}\n", text);
11
12    println!("Left aligned:");
13    println!("{}\n", left(text));
14
15    println!("Center aligned:");
16    println!("{}\n", center(text));
17
18    println!("Right aligned:");
19    println!("{}\n", right(text));
20
21    println!("=== ANSI Color Examples ===\n");
22
23    // Text with ANSI colors
24    let colored_text = "\x1b[31mRed\x1b[0m\n\x1b[32mGreen Text\x1b[0m\n\x1b[34mBlue\x1b[0m";
25
26    println!("Colored text (center aligned):");
27    println!("{}\n", center(colored_text));
28
29    println!("=== Unicode Examples ===\n");
30
31    // Unicode text with wide characters
32    let unicode_text = "古\n古古古\nHello 世界";
33
34    println!("Unicode text (right aligned):");
35    println!("{}\n", right(unicode_text));
36
37    println!("=== Custom Options Examples ===\n");
38
39    // Custom separator and padding
40    let pipe_separated = "Name|Age|Location";
41    let options = AlignOptions::new(Alignment::Center)
42        .with_split("|")
43        .with_pad('.');
44
45    println!("Custom separator and padding:");
46    println!("{}\n", ansi_align_with_options(pipe_separated, &options));
47
48    // Menu-like alignment
49    let menu = "🏠 Home\n📋 About Us\n📞 Contact\n⚙️ Settings";
50
51    println!("Menu (center aligned):");
52    println!("{}\n", center(menu));
53}
Source

pub fn with_split<S: Into<String>>(self, split: S) -> Self

Sets the string used to split lines using the builder pattern.

By default, lines are split on "\n", but you can specify any string as a line separator.

§Arguments
  • split - The string to use as a line separator
§Examples
use ansi_align::{AlignOptions, Alignment};

let opts = AlignOptions::new(Alignment::Center)
    .with_split("|")
    .with_split("<->"); // Multi-character separators work too
Examples found in repository?
examples/cli_tool.rs (line 87)
66fn main() -> Result<(), Box<dyn std::error::Error>> {
67    let cli = Cli::parse();
68
69    if cli.demo {
70        show_demo();
71        return Ok(());
72    }
73
74    let input_text = get_input_text(&cli)?;
75
76    if input_text.trim().is_empty() {
77        if !cli.quiet {
78            eprintln!("{}", "⚠️  No input text provided".yellow());
79        }
80        return Ok(());
81    }
82
83    let split_str = cli.split.replace("\\n", "\n").replace("\\t", "\t");
84
85    let alignment = cli.align.into();
86    let options = AlignOptions::new(alignment)
87        .with_split(split_str)
88        .with_pad(cli.pad);
89
90    let aligned_text = ansi_align_with_options(&input_text, &options);
91
92    if cli.border {
93        print_with_border(&aligned_text, &cli);
94    } else {
95        println!("{}", aligned_text);
96    }
97
98    Ok(())
99}
100
101fn get_input_text(cli: &Cli) -> Result<String, Box<dyn std::error::Error>> {
102    if let Some(file_path) = &cli.file {
103        // Read from file
104        Ok(fs::read_to_string(file_path)?)
105    } else if let Some(text) = &cli.text {
106        if text == "-" {
107            // Read from stdin
108            let mut buffer = String::new();
109            io::stdin().read_to_string(&mut buffer)?;
110            Ok(buffer)
111        } else {
112            // Process escape sequences in command line text
113            Ok(text.replace("\\n", "\n").replace("\\t", "\t"))
114        }
115    } else {
116        // Read from stdin if no text provided
117        let mut buffer = String::new();
118        io::stdin().read_to_string(&mut buffer)?;
119        Ok(buffer)
120    }
121}
122
123fn print_with_border(text: &str, cli: &Cli) {
124    let lines: Vec<&str> = text.split('\n').collect();
125    let max_width = lines
126        .iter()
127        .map(|line| string_width::string_width(line))
128        .max()
129        .unwrap_or(0);
130
131    let border_width = max_width + 2; // 1 space padding on each side
132
133    if !cli.quiet {
134        // Top border
135        println!(
136            "{}{}{}",
137            "┌".cyan().bold(),
138            "─".repeat(border_width).cyan(),
139            "┐".cyan().bold()
140        );
141    }
142
143    // Content with side borders - text is already aligned
144    for line in lines {
145        if !cli.quiet {
146            print!("{}", "│ ".cyan().bold());
147        }
148        print!("{}", line);
149        if !cli.quiet {
150            print!("{}", " │".cyan().bold());
151        }
152        println!();
153    }
154
155    if !cli.quiet {
156        // Bottom border
157        println!(
158            "{}{}{}",
159            "└".cyan().bold(),
160            "─".repeat(border_width).cyan(),
161            "┘".cyan().bold()
162        );
163    }
164}
165
166fn show_demo() {
167    println!(
168        "{}",
169        "🎨 ansi-align Demo - Beautiful Text Alignment"
170            .bold()
171            .magenta()
172    );
173    println!();
174
175    // Demo 1: Basic alignment
176    println!("{}", "📝 Basic Alignment:".bold().blue());
177    let basic_text = "Hello\nWorld\nRust";
178
179    println!("\n{}", "Left:".green());
180    println!(
181        "{}",
182        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Left))
183    );
184
185    println!("\n{}", "Center:".green());
186    println!(
187        "{}",
188        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Center))
189    );
190
191    println!("\n{}", "Right:".green());
192    println!(
193        "{}",
194        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Right))
195    );
196
197    println!("\n{}", "─".repeat(50).dimmed());
198
199    // Demo 2: ANSI colors
200    println!("\n{}", "🌈 ANSI Color Support:".bold().blue());
201    let colored_text = format!(
202        "{}\n{}\n{}",
203        "Red Text".red().bold(),
204        "Green Text".green().italic(),
205        "Blue Text".blue().underline()
206    );
207
208    println!("\n{}", "Center aligned with colors:".green());
209    println!(
210        "{}",
211        ansi_align_with_options(&colored_text, &AlignOptions::new(Alignment::Center))
212    );
213
214    println!("\n{}", "─".repeat(50).dimmed());
215
216    // Demo 3: Unicode support
217    println!("\n{}", "🌏 Unicode Support:".bold().blue());
218    let unicode_text = "古\n古古古\nHello 世界";
219
220    println!("\n{}", "Right aligned Unicode:".green());
221    println!(
222        "{}",
223        ansi_align_with_options(unicode_text, &AlignOptions::new(Alignment::Right))
224    );
225
226    println!("\n{}", "─".repeat(50).dimmed());
227
228    // Demo 4: Custom options
229    println!("\n{}", "⚙️  Custom Options:".bold().blue());
230    let custom_text = "Name|Age|Location";
231    let options = AlignOptions::new(Alignment::Center)
232        .with_split("|")
233        .with_pad('.');
234
235    println!("\n{}", "Custom separator '|' and padding '.':".green());
236    println!("{}", ansi_align_with_options(custom_text, &options));
237
238    println!("\n{}", "─".repeat(50).dimmed());
239
240    // Demo 5: Menu example
241    println!("\n{}", "📋 Menu Example:".bold().blue());
242    let menu = format!(
243        "{}\n{}\n{}\n{}",
244        "🏠 Home".cyan(),
245        "📋 About Us".yellow(),
246        "📞 Contact".green(),
247        "⚙️ Settings".magenta()
248    );
249
250    println!("\n{}", "Center aligned menu:".green());
251    println!(
252        "{}",
253        ansi_align_with_options(&menu, &AlignOptions::new(Alignment::Center))
254    );
255
256    println!(
257        "\n{}",
258        "✨ Try it yourself with different options!".bold().green()
259    );
260    println!("{}", "Examples:".dimmed());
261    println!(
262        "{}",
263        "  cargo run --example cli_tool -- \"Hello\\nWorld\" --align center".dimmed()
264    );
265    println!(
266        "{}",
267        "  echo \"Line 1\\nLonger Line 2\" | cargo run --example cli_tool -- - --border".dimmed()
268    );
269    println!(
270        "{}",
271        "  cargo run --example cli_tool -- --file README.md --align right --pad '.'".dimmed()
272    );
273}
More examples
Hide additional examples
examples/basic_usage.rs (line 42)
3fn main() {
4    println!("=== Basic Alignment Examples ===\n");
5
6    // Simple text alignment
7    let text = "Hello\nWorld\nRust";
8
9    println!("Original text:");
10    println!("{}\n", text);
11
12    println!("Left aligned:");
13    println!("{}\n", left(text));
14
15    println!("Center aligned:");
16    println!("{}\n", center(text));
17
18    println!("Right aligned:");
19    println!("{}\n", right(text));
20
21    println!("=== ANSI Color Examples ===\n");
22
23    // Text with ANSI colors
24    let colored_text = "\x1b[31mRed\x1b[0m\n\x1b[32mGreen Text\x1b[0m\n\x1b[34mBlue\x1b[0m";
25
26    println!("Colored text (center aligned):");
27    println!("{}\n", center(colored_text));
28
29    println!("=== Unicode Examples ===\n");
30
31    // Unicode text with wide characters
32    let unicode_text = "古\n古古古\nHello 世界";
33
34    println!("Unicode text (right aligned):");
35    println!("{}\n", right(unicode_text));
36
37    println!("=== Custom Options Examples ===\n");
38
39    // Custom separator and padding
40    let pipe_separated = "Name|Age|Location";
41    let options = AlignOptions::new(Alignment::Center)
42        .with_split("|")
43        .with_pad('.');
44
45    println!("Custom separator and padding:");
46    println!("{}\n", ansi_align_with_options(pipe_separated, &options));
47
48    // Menu-like alignment
49    let menu = "🏠 Home\n📋 About Us\n📞 Contact\n⚙️ Settings";
50
51    println!("Menu (center aligned):");
52    println!("{}\n", center(menu));
53}
Source

pub const fn with_pad(self, pad: char) -> Self

Sets the character used for padding using the builder pattern.

By default, spaces (' ') are used for padding, but you can specify any character.

§Arguments
  • pad - The character to use for padding
§Examples
use ansi_align::{AlignOptions, Alignment};

let opts = AlignOptions::new(Alignment::Right)
    .with_pad('.');
Examples found in repository?
examples/cli_tool.rs (line 88)
66fn main() -> Result<(), Box<dyn std::error::Error>> {
67    let cli = Cli::parse();
68
69    if cli.demo {
70        show_demo();
71        return Ok(());
72    }
73
74    let input_text = get_input_text(&cli)?;
75
76    if input_text.trim().is_empty() {
77        if !cli.quiet {
78            eprintln!("{}", "⚠️  No input text provided".yellow());
79        }
80        return Ok(());
81    }
82
83    let split_str = cli.split.replace("\\n", "\n").replace("\\t", "\t");
84
85    let alignment = cli.align.into();
86    let options = AlignOptions::new(alignment)
87        .with_split(split_str)
88        .with_pad(cli.pad);
89
90    let aligned_text = ansi_align_with_options(&input_text, &options);
91
92    if cli.border {
93        print_with_border(&aligned_text, &cli);
94    } else {
95        println!("{}", aligned_text);
96    }
97
98    Ok(())
99}
100
101fn get_input_text(cli: &Cli) -> Result<String, Box<dyn std::error::Error>> {
102    if let Some(file_path) = &cli.file {
103        // Read from file
104        Ok(fs::read_to_string(file_path)?)
105    } else if let Some(text) = &cli.text {
106        if text == "-" {
107            // Read from stdin
108            let mut buffer = String::new();
109            io::stdin().read_to_string(&mut buffer)?;
110            Ok(buffer)
111        } else {
112            // Process escape sequences in command line text
113            Ok(text.replace("\\n", "\n").replace("\\t", "\t"))
114        }
115    } else {
116        // Read from stdin if no text provided
117        let mut buffer = String::new();
118        io::stdin().read_to_string(&mut buffer)?;
119        Ok(buffer)
120    }
121}
122
123fn print_with_border(text: &str, cli: &Cli) {
124    let lines: Vec<&str> = text.split('\n').collect();
125    let max_width = lines
126        .iter()
127        .map(|line| string_width::string_width(line))
128        .max()
129        .unwrap_or(0);
130
131    let border_width = max_width + 2; // 1 space padding on each side
132
133    if !cli.quiet {
134        // Top border
135        println!(
136            "{}{}{}",
137            "┌".cyan().bold(),
138            "─".repeat(border_width).cyan(),
139            "┐".cyan().bold()
140        );
141    }
142
143    // Content with side borders - text is already aligned
144    for line in lines {
145        if !cli.quiet {
146            print!("{}", "│ ".cyan().bold());
147        }
148        print!("{}", line);
149        if !cli.quiet {
150            print!("{}", " │".cyan().bold());
151        }
152        println!();
153    }
154
155    if !cli.quiet {
156        // Bottom border
157        println!(
158            "{}{}{}",
159            "└".cyan().bold(),
160            "─".repeat(border_width).cyan(),
161            "┘".cyan().bold()
162        );
163    }
164}
165
166fn show_demo() {
167    println!(
168        "{}",
169        "🎨 ansi-align Demo - Beautiful Text Alignment"
170            .bold()
171            .magenta()
172    );
173    println!();
174
175    // Demo 1: Basic alignment
176    println!("{}", "📝 Basic Alignment:".bold().blue());
177    let basic_text = "Hello\nWorld\nRust";
178
179    println!("\n{}", "Left:".green());
180    println!(
181        "{}",
182        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Left))
183    );
184
185    println!("\n{}", "Center:".green());
186    println!(
187        "{}",
188        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Center))
189    );
190
191    println!("\n{}", "Right:".green());
192    println!(
193        "{}",
194        ansi_align_with_options(basic_text, &AlignOptions::new(Alignment::Right))
195    );
196
197    println!("\n{}", "─".repeat(50).dimmed());
198
199    // Demo 2: ANSI colors
200    println!("\n{}", "🌈 ANSI Color Support:".bold().blue());
201    let colored_text = format!(
202        "{}\n{}\n{}",
203        "Red Text".red().bold(),
204        "Green Text".green().italic(),
205        "Blue Text".blue().underline()
206    );
207
208    println!("\n{}", "Center aligned with colors:".green());
209    println!(
210        "{}",
211        ansi_align_with_options(&colored_text, &AlignOptions::new(Alignment::Center))
212    );
213
214    println!("\n{}", "─".repeat(50).dimmed());
215
216    // Demo 3: Unicode support
217    println!("\n{}", "🌏 Unicode Support:".bold().blue());
218    let unicode_text = "古\n古古古\nHello 世界";
219
220    println!("\n{}", "Right aligned Unicode:".green());
221    println!(
222        "{}",
223        ansi_align_with_options(unicode_text, &AlignOptions::new(Alignment::Right))
224    );
225
226    println!("\n{}", "─".repeat(50).dimmed());
227
228    // Demo 4: Custom options
229    println!("\n{}", "⚙️  Custom Options:".bold().blue());
230    let custom_text = "Name|Age|Location";
231    let options = AlignOptions::new(Alignment::Center)
232        .with_split("|")
233        .with_pad('.');
234
235    println!("\n{}", "Custom separator '|' and padding '.':".green());
236    println!("{}", ansi_align_with_options(custom_text, &options));
237
238    println!("\n{}", "─".repeat(50).dimmed());
239
240    // Demo 5: Menu example
241    println!("\n{}", "📋 Menu Example:".bold().blue());
242    let menu = format!(
243        "{}\n{}\n{}\n{}",
244        "🏠 Home".cyan(),
245        "📋 About Us".yellow(),
246        "📞 Contact".green(),
247        "⚙️ Settings".magenta()
248    );
249
250    println!("\n{}", "Center aligned menu:".green());
251    println!(
252        "{}",
253        ansi_align_with_options(&menu, &AlignOptions::new(Alignment::Center))
254    );
255
256    println!(
257        "\n{}",
258        "✨ Try it yourself with different options!".bold().green()
259    );
260    println!("{}", "Examples:".dimmed());
261    println!(
262        "{}",
263        "  cargo run --example cli_tool -- \"Hello\\nWorld\" --align center".dimmed()
264    );
265    println!(
266        "{}",
267        "  echo \"Line 1\\nLonger Line 2\" | cargo run --example cli_tool -- - --border".dimmed()
268    );
269    println!(
270        "{}",
271        "  cargo run --example cli_tool -- --file README.md --align right --pad '.'".dimmed()
272    );
273}
More examples
Hide additional examples
examples/basic_usage.rs (line 43)
3fn main() {
4    println!("=== Basic Alignment Examples ===\n");
5
6    // Simple text alignment
7    let text = "Hello\nWorld\nRust";
8
9    println!("Original text:");
10    println!("{}\n", text);
11
12    println!("Left aligned:");
13    println!("{}\n", left(text));
14
15    println!("Center aligned:");
16    println!("{}\n", center(text));
17
18    println!("Right aligned:");
19    println!("{}\n", right(text));
20
21    println!("=== ANSI Color Examples ===\n");
22
23    // Text with ANSI colors
24    let colored_text = "\x1b[31mRed\x1b[0m\n\x1b[32mGreen Text\x1b[0m\n\x1b[34mBlue\x1b[0m";
25
26    println!("Colored text (center aligned):");
27    println!("{}\n", center(colored_text));
28
29    println!("=== Unicode Examples ===\n");
30
31    // Unicode text with wide characters
32    let unicode_text = "古\n古古古\nHello 世界";
33
34    println!("Unicode text (right aligned):");
35    println!("{}\n", right(unicode_text));
36
37    println!("=== Custom Options Examples ===\n");
38
39    // Custom separator and padding
40    let pipe_separated = "Name|Age|Location";
41    let options = AlignOptions::new(Alignment::Center)
42        .with_split("|")
43        .with_pad('.');
44
45    println!("Custom separator and padding:");
46    println!("{}\n", ansi_align_with_options(pipe_separated, &options));
47
48    // Menu-like alignment
49    let menu = "🏠 Home\n📋 About Us\n📞 Contact\n⚙️ Settings";
50
51    println!("Menu (center aligned):");
52    println!("{}\n", center(menu));
53}

Trait Implementations§

Source§

impl Clone for AlignOptions

Source§

fn clone(&self) -> AlignOptions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AlignOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AlignOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.