#[derive(Clone, Debug)]
pub struct LineComposer<I> {
    words: I,
    width: usize,
    current: Option<String>,
}

impl<I> LineComposer<I> {
    pub(crate) fn new<S>(words: I, width: usize) -> Self
    where
        I: Iterator<Item = S>,
        S: AsRef<str>,
    {
        LineComposer {
            words,
            width,
            current: None,
        }
    }
}

impl<I, S> Iterator for LineComposer<I>
where
    I: Iterator<Item = S>,
    S: AsRef<str>,
{
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        let mut next = match self.words.next() {
            None => return self.current.take(),
            Some(value) => value,
        };

        let mut current = self.current.take().unwrap_or_else(String::new);

        loop {
            let word = next.as_ref();
            if self.width <= current.len() + word.len() {
                self.current = Some(String::from(word));