Last active
July 18, 2019 08:54
-
-
Save Canop/6a267e586e5f912754305a91d53e2f45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code with semicolons: | |
use std::fmt; | |
use minimad::Text; | |
use crate::skin::MadSkin; | |
use crate::code; | |
use crate::line::FmtLine; | |
use crate::tbl; | |
use crate::wrap; | |
/// a formatted text, implementing Display | |
pub struct FmtText<'k, 's> { | |
pub skin: &'k MadSkin, | |
pub lines: Vec<FmtLine<'s>>, | |
pub width: Option<usize>, // available width | |
} | |
impl<'k, 's> FmtText<'k, 's> { | |
pub fn from(skin: &'k MadSkin, src: &'s str, width: Option<usize>) -> FmtText<'k, 's> { | |
let mut mt = Text::from(src); | |
let mut lines = mt.lines.drain(..).map( | |
|mline| FmtLine::from(mline, skin) | |
).collect(); | |
tbl::fix_all_tables(&mut lines, width.unwrap_or(std::usize::MAX)); | |
code::justify_blocks(&mut lines); | |
if let Some(width) = width { | |
lines = wrap::hard_wrap_lines(lines, width); | |
} | |
FmtText { | |
skin, | |
lines, | |
width, | |
} | |
} | |
} | |
impl fmt::Display for FmtText<'_, '_> { | |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
for line in &self.lines { | |
self.skin.write_fmt_line(f, line, self.width, false)?; | |
writeln!(f)?; | |
} | |
Ok(()) | |
} | |
} | |
# Same code without semicolon: | |
use std::fmt | |
use minimad::Text | |
use crate::skin::MadSkin | |
use crate::code | |
use crate::line::FmtLine | |
use crate::tbl | |
use crate::wrap | |
/// a formatted text, implementing Display | |
pub struct FmtText<'k, 's> { | |
pub skin: &'k MadSkin, | |
pub lines: Vec<FmtLine<'s>>, | |
pub width: Option<usize>, // available width | |
} | |
impl<'k, 's> FmtText<'k, 's> { | |
pub fn from(skin: &'k MadSkin, src: &'s str, width: Option<usize>) -> FmtText<'k, 's> { | |
let mut mt = Text::from(src) | |
let mut lines = mt.lines.drain(..).map( | |
|mline| FmtLine::from(mline, skin) | |
).collect() | |
tbl::fix_all_tables(&mut lines, width.unwrap_or(std::usize::MAX)) | |
code::justify_blocks(&mut lines) | |
if let Some(width) = width { | |
lines = wrap::hard_wrap_lines(lines, width) | |
} | |
FmtText { | |
skin, | |
lines, | |
width, | |
} | |
} | |
} | |
impl fmt::Display for FmtText<'_, '_> { | |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
for line in &self.lines { | |
self.skin.write_fmt_line(f, line, self.width, false)? | |
writeln!(f)? | |
} | |
Ok(()) | |
} | |
} | |
Is there something ambiguous here ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment