lipgloss 0.0.6

Style definitions for nice terminal layouts. The core of the lipgloss-rs library.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Size constraint and dimension methods for Style.
//!
//! This module provides methods for controlling the dimensions of styled content,
//! including explicit sizing and maximum size constraints. These methods are
//! essential for creating consistent layouts and ensuring content fits within
//! desired boundaries in terminal applications.
//!
//! # Key Concepts
//!
//! - **Explicit Sizing**: Set exact width and height dimensions
//! - **Maximum Constraints**: Set upper limits for content expansion
//! - **Content Interaction**: How sizing affects text wrapping and truncation
//! - **Layout Impact**: How dimensions affect alignment and positioning
//!
//! # Dimension Types
//!
//! - **Width**: Controls horizontal space (character columns)
//! - **Height**: Controls vertical space (text lines)
//! - **Max Width**: Upper limit for automatic width calculation
//! - **Max Height**: Upper limit for automatic height calculation
//!
//! # Examples
//!
//! ```rust
//! use lipgloss::Style;
//!
//! // Fixed dimensions
//! let fixed_box = Style::new()
//!     .width(20)
//!     .height(10)
//!     .render("Content");
//!
//! // Maximum constraints
//! let constrained = Style::new()
//!     .max_width(50)
//!     .max_height(5)
//!     .render("This text will wrap or truncate if it exceeds the limits");
//!
//! // Combined with other styling
//! let styled_box = Style::new()
//!     .width(30)
//!     .border(lipgloss::normal_border())
//!     .padding(1, 2, 1, 2)
//!     .render("Styled content in a fixed-width box");
//! ```

use crate::security::validate_dimension;
use crate::style::{properties::*, Style};

impl Style {
    /// Set the explicit width for the styled content.
    ///
    /// This method sets a fixed width for the styled content, measured in character
    /// columns. When an explicit width is set, the content will be formatted to fit
    /// within this width, with text wrapping or truncation as necessary. The width
    /// includes padding but excludes borders and margins.
    ///
    /// # Behavior
    ///
    /// - **Text Wrapping**: Long lines will wrap to fit within the specified width
    /// - **Padding Inclusion**: The width includes any horizontal padding
    /// - **Border Exclusion**: Borders add to the total rendered width
    /// - **Alignment**: Content alignment works within the specified width
    /// - **Minimum Size**: Width of 0 or negative values may cause unexpected behavior
    ///
    /// # Arguments
    ///
    /// * `w` - The width in character columns (should be positive)
    ///
    /// # Returns
    ///
    /// The modified `Style` with the width constraint set.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Basic width setting
    /// let narrow = Style::new()
    ///     .width(10)
    ///     .render("This text will wrap");
    ///
    /// // Width with padding
    /// let padded = Style::new()
    ///     .width(20)
    ///     .padding(0, 2, 0, 2)  // 2 chars padding on each side
    ///     .render("Content");    // Actual content area: 16 chars
    ///
    /// // Width with borders
    /// let bordered = Style::new()
    ///     .width(15)
    ///     .border(lipgloss::normal_border())  // Adds 2 chars to total width
    ///     .render("Text content");
    /// ```
    ///
    /// ## Layout Interaction
    ///
    /// ```rust
    /// use lipgloss::{Style, position::CENTER};
    ///
    /// // Width enables horizontal alignment
    /// let centered = Style::new()
    ///     .width(30)
    ///     .align_horizontal(CENTER)
    ///     .render("Centered text");
    ///
    /// // Width with text wrapping
    /// let wrapped = Style::new()
    ///     .width(12)
    ///     .render("This is a long line that will wrap to multiple lines");
    /// ```
    ///
    /// ## Common Patterns
    ///
    /// ```rust
    /// use lipgloss::Style;
    /// use lipgloss::color::Color;
    ///
    /// // Card-like component with fixed width
    /// let card = Style::new()
    ///     .width(40)
    ///     .border(lipgloss::rounded_border())
    ///     .padding(1, 2, 1, 2)
    ///     .background(Color("#f0f0f0".to_string()))
    ///     .render("Card content with consistent width");
    ///
    /// // Column layout
    /// let column = Style::new()
    ///     .width(25)
    ///     .render("Column 1 content");
    /// ```
    pub fn width(mut self, w: i32) -> Self {
        self.width = validate_dimension(w, "width");
        self.set_prop(WIDTH_KEY);
        self
    }

    /// Set the explicit height for the styled content.
    ///
    /// This method sets a fixed height for the styled content, measured in text lines.
    /// When an explicit height is set, the content will be formatted to fit within
    /// this height, with vertical alignment and truncation as necessary. The height
    /// includes padding but excludes borders and margins.
    ///
    /// # Behavior
    ///
    /// - **Content Truncation**: Content exceeding the height will be truncated
    /// - **Vertical Alignment**: Content can be aligned within the specified height
    /// - **Padding Inclusion**: The height includes any vertical padding
    /// - **Border Exclusion**: Borders add to the total rendered height
    /// - **Empty Space**: Heights larger than content create whitespace
    ///
    /// # Arguments
    ///
    /// * `h` - The height in text lines (should be positive)
    ///
    /// # Returns
    ///
    /// The modified `Style` with the height constraint set.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Basic height setting
    /// let tall_box = Style::new()
    ///     .height(5)
    ///     .render("Content\nLine 2\nLine 3");
    ///
    /// // Height with padding
    /// let padded = Style::new()
    ///     .height(8)
    ///     .padding(1, 0, 1, 0)  // 1 line padding top/bottom
    ///     .render("Content");    // Actual content area: 6 lines
    ///
    /// // Height with borders
    /// let bordered = Style::new()
    ///     .height(6)
    ///     .border(lipgloss::normal_border())  // Adds 2 lines to total height
    ///     .render("Text\ncontent");
    /// ```
    ///
    /// ## Vertical Alignment
    ///
    /// ```rust
    /// use lipgloss::{Style, position::{TOP, CENTER, BOTTOM}};
    ///
    /// // Top-aligned content in tall box
    /// let top_aligned = Style::new()
    ///     .height(10)
    ///     .align_vertical(TOP)
    ///     .render("Top content");
    ///
    /// // Centered content
    /// let centered = Style::new()
    ///     .height(8)
    ///     .align_vertical(CENTER)
    ///     .render("Centered\ncontent");
    ///
    /// // Bottom-aligned content
    /// let bottom_aligned = Style::new()
    ///     .height(6)
    ///     .align_vertical(BOTTOM)
    ///     .render("Bottom content");
    /// ```
    ///
    /// ## Content Management
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Truncation behavior
    /// let truncated = Style::new()
    ///     .height(3)
    ///     .render("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");  // Lines 4-5 truncated
    ///
    /// // Creating consistent height panels
    /// let panel = Style::new()
    ///     .width(30)
    ///     .height(12)
    ///     .border(lipgloss::rounded_border())
    ///     .padding(1, 2, 1, 2)
    ///     .render("Panel content with fixed dimensions");
    /// ```
    pub fn height(mut self, h: i32) -> Self {
        self.height = validate_dimension(h, "height");
        self.set_prop(HEIGHT_KEY);
        self
    }

    /// Set the maximum width constraint for the styled content.
    ///
    /// This method sets an upper limit for the width of styled content, measured in
    /// character columns. Unlike `width()`, this doesn't force a specific width but
    /// instead prevents the content from exceeding the specified maximum. Content
    /// narrower than the maximum will retain its natural width.
    ///
    /// # Behavior
    ///
    /// - **Constraint Only**: Content narrower than max_width keeps its natural width
    /// - **Text Wrapping**: Long lines wrap when they would exceed the maximum
    /// - **Dynamic Sizing**: Final width depends on content and constraints
    /// - **Padding Interaction**: Maximum width includes horizontal padding
    /// - **Border Independence**: Borders are added outside the maximum width
    ///
    /// # Arguments
    ///
    /// * `w` - The maximum width in character columns (should be positive)
    ///
    /// # Returns
    ///
    /// The modified `Style` with the maximum width constraint set.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Basic maximum width
    /// let constrained = Style::new()
    ///     .max_width(25)
    ///     .render("This long text will wrap when it exceeds 25 characters per line");
    ///
    /// // Short content keeps natural width
    /// let short = Style::new()
    ///     .max_width(50)
    ///     .render("Short");  // Will be narrower than 50 chars
    ///
    /// // Maximum width with padding
    /// let padded = Style::new()
    ///     .max_width(30)
    ///     .padding(0, 3, 0, 3)  // 6 chars total horizontal padding
    ///     .render("Content");   // Effective content area: up to 24 chars
    /// ```
    ///
    /// ## Responsive Design
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Responsive text blocks
    /// let responsive = Style::new()
    ///     .max_width(60)
    ///     .border(lipgloss::normal_border())
    ///     .padding(1, 2, 1, 2)
    ///     .render("This content will wrap nicely within 60 characters, but shorter content won't be forced to that width");
    ///
    /// // Flexible containers
    /// let flexible = Style::new()
    ///     .max_width(40)
    ///     .render("Adapts to content size up to 40 chars");
    /// ```
    ///
    /// ## Comparison with Fixed Width
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Fixed width - always 20 characters
    /// let fixed = Style::new()
    ///     .width(20)
    ///     .render("Hi");  // Padded to 20 chars
    ///
    /// // Maximum width - only as wide as needed
    /// let flexible = Style::new()
    ///     .max_width(20)
    ///     .render("Hi");  // Only 2 chars wide
    ///
    /// // Maximum width with long content
    /// let wrapped = Style::new()
    ///     .max_width(20)
    ///     .render("This is a very long line that will wrap");
    /// ```
    ///
    /// ## Layout Applications
    ///
    /// ```rust
    /// use lipgloss::Style;
    /// use lipgloss::color::Color;
    ///
    /// // Flexible card components
    /// let card = Style::new()
    ///     .max_width(50)
    ///     .border(lipgloss::rounded_border())
    ///     .padding(1, 2, 1, 2)
    ///     .background(Color("#f8f9fa".to_string()))
    ///     .render("Dynamic card content that adapts to content length");
    ///
    /// // Constrained text areas
    /// let text_area = Style::new()
    ///     .max_width(80)
    ///     .render("Long form text content that should wrap at reasonable line lengths for readability");
    /// ```
    pub fn max_width(mut self, w: i32) -> Self {
        self.max_width = validate_dimension(w, "max_width");
        self.set_prop(MAX_WIDTH_KEY);
        self
    }

    /// Set the maximum height constraint for the styled content.
    ///
    /// This method sets an upper limit for the height of styled content, measured in
    /// text lines. Unlike `height()`, this doesn't force a specific height but instead
    /// prevents the content from exceeding the specified maximum. Content shorter than
    /// the maximum will retain its natural height.
    ///
    /// # Behavior
    ///
    /// - **Constraint Only**: Content shorter than max_height keeps its natural height
    /// - **Content Truncation**: Excess lines are truncated when maximum is exceeded
    /// - **Dynamic Sizing**: Final height depends on content and constraints
    /// - **Padding Interaction**: Maximum height includes vertical padding
    /// - **Border Independence**: Borders are added outside the maximum height
    ///
    /// # Arguments
    ///
    /// * `h` - The maximum height in text lines (should be positive)
    ///
    /// # Returns
    ///
    /// The modified `Style` with the maximum height constraint set.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Basic maximum height
    /// let constrained = Style::new()
    ///     .max_height(5)
    ///     .render("Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7");
    /// // Only first 5 lines will be shown
    ///
    /// // Short content keeps natural height
    /// let short = Style::new()
    ///     .max_height(10)
    ///     .render("Short\ncontent");  // Will be only 2 lines tall
    ///
    /// // Maximum height with padding
    /// let padded = Style::new()
    ///     .max_height(8)
    ///     .padding(1, 0, 1, 0)  // 2 lines total vertical padding
    ///     .render("Content\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6");
    /// // Effective content area: up to 6 lines
    /// ```
    ///
    /// ## Content Management
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Scrollable content areas
    /// let scrollable = Style::new()
    ///     .max_height(6)
    ///     .border(lipgloss::normal_border())
    ///     .render("Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8");
    /// // Shows first 6 lines, indicates more content available
    ///
    /// // Flexible containers
    /// let flexible = Style::new()
    ///     .max_height(12)
    ///     .render("Adapts to content height up to 12 lines");
    /// ```
    ///
    /// ## Comparison with Fixed Height
    ///
    /// ```rust
    /// use lipgloss::Style;
    ///
    /// // Fixed height - always 5 lines
    /// let fixed = Style::new()
    ///     .height(5)
    ///     .render("Line 1\nLine 2");  // Padded to 5 lines with whitespace
    ///
    /// // Maximum height - only as tall as needed
    /// let flexible = Style::new()
    ///     .max_height(5)
    ///     .render("Line 1\nLine 2");  // Only 2 lines tall
    ///
    /// // Maximum height with long content
    /// let truncated = Style::new()
    ///     .max_height(3)
    ///     .render("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");  // Truncated to 3 lines
    /// ```
    ///
    /// ## Layout Applications
    ///
    /// ```rust
    /// use lipgloss::Style;
    /// use lipgloss::color::Color;
    ///
    /// // Preview panels with content limits
    /// let preview = Style::new()
    ///     .max_width(40)
    ///     .max_height(8)
    ///     .border(lipgloss::rounded_border())
    ///     .padding(1, 2, 1, 2)
    ///     .background(Color("#f0f0f0".to_string()))
    ///     .render("Preview content that may be truncated if too long...");
    ///
    /// // Constrained output areas
    /// let output = Style::new()
    ///     .max_height(15)
    ///     .render("Command output or log content with height limits");
    /// ```
    pub fn max_height(mut self, h: i32) -> Self {
        self.max_height = validate_dimension(h, "max_height");
        self.set_prop(MAX_HEIGHT_KEY);
        self
    }
}