| 1 | use std::cmp::max; |
| 2 | use std::io::Write; |
| 3 | |
| 4 | use crate::ansi; |
| 5 | use crate::cli::Width; |
| 6 | use crate::style::{DecorationStyle, Style}; |
| 7 | |
| 8 | fn paint_text(text_style: Style, text: &str, addendum: &str) -> String { |
| 9 | if addendum.is_empty() { |
| 10 | text_style.paint(text).to_string() |
| 11 | } else { |
| 12 | text_style |
| 13 | .paint(text.to_string() + " (" + addendum + ")") |
| 14 | .to_string() |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | pub type DrawFunction = dyn FnMut( |
| 19 | &mut dyn Write, |
| 20 | &str, |
| 21 | &str, |
| 22 | &str, |
| 23 | &Width, |
| 24 | Style, |
| 25 | ansi_term::Style, |
| 26 | ) -> std::io::Result<()>; |
| 27 | |
| 28 | pub fn get_draw_function( |
| 29 | decoration_style: DecorationStyle, |
| 30 | ) -> (Box<DrawFunction>, bool, ansi_term::Style) { |
| 31 | match decoration_style { |
| 32 | DecorationStyle::Box(style) => (Box::new(write_boxed), true, style), |
| 33 | DecorationStyle::BoxWithUnderline(style) => { |
| 34 | (Box::new(write_boxed_with_underline), true, style) |
| 35 | } |
| 36 | DecorationStyle::BoxWithOverline(style) => { |
| 37 | // TODO: not implemented |
| 38 | (Box::new(write_boxed), true, style) |
| 39 | } |
| 40 | DecorationStyle::BoxWithUnderOverline(style) => { |
| 41 | // TODO: not implemented |
| 42 | (Box::new(write_boxed), true, style) |
| 43 | } |
| 44 | DecorationStyle::Underline(style) => (Box::new(write_underlined), false, style), |
| 45 | DecorationStyle::Overline(style) => (Box::new(write_overlined), false, style), |
| 46 | DecorationStyle::UnderOverline(style) => (Box::new(write_underoverlined), false, style), |
| 47 | DecorationStyle::NoDecoration => ( |
| 48 | Box::new(write_no_decoration), |
| 49 | false, |
| 50 | ansi_term::Style::new(), |
| 51 | ), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn write_no_decoration( |
| 56 | writer: &mut dyn Write, |
| 57 | text: &str, |
| 58 | raw_text: &str, |
| 59 | addendum: &str, |
| 60 | _line_width: &Width, // ignored |
| 61 | text_style: Style, |
| 62 | _decoration_style: ansi_term::Style, |
| 63 | ) -> std::io::Result<()> { |
| 64 | if text_style.is_raw { |
| 65 | writeln!(writer, "{raw_text}")?; |
| 66 | } else { |
| 67 | writeln!(writer, "{}", paint_text(text_style, text, addendum))?; |
| 68 | } |
| 69 | Ok(()) |
| 70 | } |
| 71 | |
| 72 | /// Write text to stream, surrounded by a box, leaving the cursor just |
| 73 | /// beyond the bottom right corner. |
| 74 | pub fn write_boxed( |
| 75 | writer: &mut dyn Write, |
| 76 | text: &str, |
| 77 | raw_text: &str, |
| 78 | addendum: &str, |
| 79 | _line_width: &Width, // ignored |
| 80 | text_style: Style, |
| 81 | decoration_style: ansi_term::Style, |
| 82 | ) -> std::io::Result<()> { |
| 83 | let up_left = if decoration_style.is_bold { |
| 84 | box_drawing::heavy::UP_LEFT |
| 85 | } else { |
| 86 | box_drawing::light::UP_LEFT |
| 87 | }; |
| 88 | let box_width = ansi::measure_text_width(text); |
| 89 | write_boxed_partial( |
| 90 | writer, |
| 91 | text, |
| 92 | raw_text, |
| 93 | addendum, |
| 94 | box_width, |
| 95 | text_style, |
| 96 | decoration_style, |
| 97 | )?; |
| 98 | writeln!(writer, "{}", decoration_style.paint(up_left))?; |
| 99 | Ok(()) |
| 100 | } |
| 101 | |
| 102 | /// Write text to stream, surrounded by a box, and extend a line from |
| 103 | /// the bottom right corner. |
| 104 | fn write_boxed_with_underline( |
| 105 | writer: &mut dyn Write, |
| 106 | text: &str, |
| 107 | raw_text: &str, |
| 108 | addendum: &str, |
| 109 | line_width: &Width, |
| 110 | text_style: Style, |
| 111 | decoration_style: ansi_term::Style, |
| 112 | ) -> std::io::Result<()> { |
| 113 | let box_width = ansi::measure_text_width(text); |
| 114 | write_boxed_with_horizontal_whisker( |
| 115 | writer, |
| 116 | text, |
| 117 | raw_text, |
| 118 | addendum, |
| 119 | box_width, |
| 120 | text_style, |
| 121 | decoration_style, |
| 122 | )?; |
| 123 | let line_width = match *line_width { |
| 124 | Width::Fixed(n) => n, |
| 125 | Width::Variable => box_width, |
| 126 | }; |
| 127 | write_horizontal_line( |
| 128 | writer, |
| 129 | if line_width > box_width { |
| 130 | line_width - box_width - 1 |
| 131 | } else { |
| 132 | 0 |
| 133 | }, |
| 134 | text_style, |
| 135 | decoration_style, |
| 136 | )?; |
| 137 | writeln!(writer)?; |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | enum UnderOverline { |
| 142 | Under, |
| 143 | Over, |
| 144 | Underover, |
| 145 | } |
| 146 | |
| 147 | fn write_underlined( |
| 148 | writer: &mut dyn Write, |
| 149 | text: &str, |
| 150 | raw_text: &str, |
| 151 | addendum: &str, |
| 152 | line_width: &Width, |
| 153 | text_style: Style, |
| 154 | decoration_style: ansi_term::Style, |
| 155 | ) -> std::io::Result<()> { |
| 156 | _write_under_or_over_lined( |
| 157 | UnderOverline::Under, |
| 158 | writer, |
| 159 | text, |
| 160 | raw_text, |
| 161 | addendum, |
| 162 | line_width, |
| 163 | text_style, |
| 164 | decoration_style, |
| 165 | ) |
| 166 | } |
| 167 | |
| 168 | fn write_overlined( |
| 169 | writer: &mut dyn Write, |
| 170 | text: &str, |
| 171 | raw_text: &str, |
| 172 | addendum: &str, |
| 173 | line_width: &Width, |
| 174 | text_style: Style, |
| 175 | decoration_style: ansi_term::Style, |
| 176 | ) -> std::io::Result<()> { |
| 177 | _write_under_or_over_lined( |
| 178 | UnderOverline::Over, |
| 179 | writer, |
| 180 | text, |
| 181 | raw_text, |
| 182 | addendum, |
| 183 | line_width, |
| 184 | text_style, |
| 185 | decoration_style, |
| 186 | ) |
| 187 | } |
| 188 | |
| 189 | fn write_underoverlined( |
| 190 | writer: &mut dyn Write, |
| 191 | text: &str, |
| 192 | raw_text: &str, |
| 193 | addendum: &str, |
| 194 | line_width: &Width, |
| 195 | text_style: Style, |
| 196 | decoration_style: ansi_term::Style, |
| 197 | ) -> std::io::Result<()> { |
| 198 | _write_under_or_over_lined( |
| 199 | UnderOverline::Underover, |
| 200 | writer, |
| 201 | text, |
| 202 | raw_text, |
| 203 | addendum, |
| 204 | line_width, |
| 205 | text_style, |
| 206 | decoration_style, |
| 207 | ) |
| 208 | } |
| 209 | |
| 210 | #[allow(clippy::too_many_arguments)] |
| 211 | fn _write_under_or_over_lined( |
| 212 | underoverline: UnderOverline, |
| 213 | writer: &mut dyn Write, |
| 214 | text: &str, |
| 215 | raw_text: &str, |
| 216 | addendum: &str, |
| 217 | line_width: &Width, |
| 218 | text_style: Style, |
| 219 | decoration_style: ansi_term::Style, |
| 220 | ) -> std::io::Result<()> { |
| 221 | let text_width = ansi::measure_text_width(text); |
| 222 | let line_width = match *line_width { |
| 223 | Width::Fixed(n) => max(n, text_width), |
| 224 | Width::Variable => text_width, |
| 225 | }; |
| 226 | let write_line = |writer: &mut dyn Write| -> std::io::Result<()> { |
| 227 | write_horizontal_line(writer, line_width, text_style, decoration_style)?; |
| 228 | writeln!(writer)?; |
| 229 | Ok(()) |
| 230 | }; |
| 231 | match underoverline { |
| 232 | UnderOverline::Under => {} |
| 233 | _ => write_line(writer)?, |
| 234 | } |
| 235 | if text_style.is_raw { |
| 236 | writeln!(writer, "{raw_text}")?; |
| 237 | } else { |
| 238 | writeln!(writer, "{}", paint_text(text_style, text, addendum))?; |
| 239 | } |
| 240 | match underoverline { |
| 241 | UnderOverline::Over => {} |
| 242 | _ => write_line(writer)?, |
| 243 | } |
| 244 | Ok(()) |
| 245 | } |
| 246 | |
| 247 | fn write_horizontal_line( |
| 248 | writer: &mut dyn Write, |
| 249 | width: usize, |
| 250 | _text_style: Style, |
| 251 | decoration_style: ansi_term::Style, |
| 252 | ) -> std::io::Result<()> { |
| 253 | let horizontal = if decoration_style.is_bold { |
| 254 | box_drawing::heavy::HORIZONTAL |
| 255 | } else { |
| 256 | box_drawing::light::HORIZONTAL |
| 257 | }; |
| 258 | write!( |
| 259 | writer, |
| 260 | "{}", |
| 261 | decoration_style.paint(horizontal.repeat(width)) |
| 262 | ) |
| 263 | } |
| 264 | |
| 265 | fn write_boxed_with_horizontal_whisker( |
| 266 | writer: &mut dyn Write, |
| 267 | text: &str, |
| 268 | raw_text: &str, |
| 269 | addendum: &str, |
| 270 | box_width: usize, |
| 271 | text_style: Style, |
| 272 | decoration_style: ansi_term::Style, |
| 273 | ) -> std::io::Result<()> { |
| 274 | let up_horizontal = if decoration_style.is_bold { |
| 275 | box_drawing::heavy::UP_HORIZONTAL |
| 276 | } else { |
| 277 | box_drawing::light::UP_HORIZONTAL |
| 278 | }; |
| 279 | write_boxed_partial( |
| 280 | writer, |
| 281 | text, |
| 282 | raw_text, |
| 283 | addendum, |
| 284 | box_width, |
| 285 | text_style, |
| 286 | decoration_style, |
| 287 | )?; |
| 288 | write!(writer, "{}", decoration_style.paint(up_horizontal))?; |
| 289 | Ok(()) |
| 290 | } |
| 291 | |
| 292 | fn write_boxed_partial( |
| 293 | writer: &mut dyn Write, |
| 294 | text: &str, |
| 295 | raw_text: &str, |
| 296 | addendum: &str, |
| 297 | box_width: usize, |
| 298 | text_style: Style, |
| 299 | decoration_style: ansi_term::Style, |
| 300 | ) -> std::io::Result<()> { |
| 301 | let (horizontal, down_left, vertical) = if decoration_style.is_bold { |
| 302 | ( |
| 303 | box_drawing::heavy::HORIZONTAL, |
| 304 | box_drawing::heavy::DOWN_LEFT, |
| 305 | box_drawing::heavy::VERTICAL, |
| 306 | ) |
| 307 | } else { |
| 308 | ( |
| 309 | box_drawing::light::HORIZONTAL, |
| 310 | box_drawing::light::DOWN_LEFT, |
| 311 | box_drawing::light::VERTICAL, |
| 312 | ) |
| 313 | }; |
| 314 | let horizontal_edge = horizontal.repeat(box_width); |
| 315 | writeln!( |
| 316 | writer, |
| 317 | "{}{}", |
| 318 | decoration_style.paint(&horizontal_edge), |
| 319 | decoration_style.paint(down_left), |
| 320 | )?; |
| 321 | if text_style.is_raw { |
| 322 | write!(writer, "{raw_text}")?; |
| 323 | } else { |
| 324 | write!(writer, "{}", paint_text(text_style, text, addendum))?; |
| 325 | } |
| 326 | write!( |
| 327 | writer, |
| 328 | "{}\n{}", |
| 329 | decoration_style.paint(vertical), |
| 330 | decoration_style.paint(&horizontal_edge), |
| 331 | ) |
| 332 | } |