fn render()

in eden/mononoke/scs/client/src/commands/blame.rs [70:279]


    fn render(&self, matches: &ArgMatches, w: &mut dyn Write) -> Result<(), Error> {
        let schemes = get_schemes(matches);
        let path = get_path(matches).expect("path is required");
        let title_width = matches
            .value_of(ARG_TITLE_WIDTH)
            .map(usize::from_str)
            .transpose()
            .context("Invalid title width")?
            .unwrap_or(DEFAULT_TITLE_WIDTH);
        let number_width = |n| ((n + 1) as f32).log10().ceil() as usize;

        match self.blame {
            thrift::Blame::blame_compact(ref blame) => {
                let max_commit_number_width = number_width(
                    blame
                        .commit_numbers
                        .iter()
                        .flatten()
                        .copied()
                        .max()
                        .unwrap_or(0),
                );
                let max_author_width = blame.authors.iter().map(|a| a.width()).max().unwrap_or(0);
                let max_line_width =
                    number_width(blame.lines.iter().map(|l| l.line).max().unwrap_or(0) + 1);

                let max_origin_line_width =
                    number_width(blame.lines.iter().map(|l| l.origin_line).max().unwrap_or(0));
                let max_origin_path_width = blame
                    .lines
                    .iter()
                    .map(|l| {
                        let origin_path = blame.paths[l.path_index as usize].as_str();
                        if origin_path != path {
                            origin_path.width()
                        } else {
                            0
                        }
                    })
                    .max()
                    .unwrap_or(0);
                let max_parent_line_range_width = blame
                    .lines
                    .iter()
                    .map(|l| {
                        let parent_index_width = l
                            .parent_index
                            .map_or(0, |i| if i > 0 { number_width(i) + 2 } else { 0 });
                        let parent_path_width = l
                            .parent_path_index
                            .map_or(0, |p| blame.paths[p as usize].width() + 2);
                        let parent_range_width = match (l.parent_start_line, l.parent_range_length)
                        {
                            (Some(start), Some(0)) => number_width(start) + 1,
                            (Some(start), Some(len)) => {
                                number_width(start) + number_width(start + len - 1) + 1
                            }
                            _ => 0,
                        };
                        parent_index_width + parent_path_width + parent_range_width
                    })
                    .max()
                    .unwrap_or(0);

                for line in blame.lines.iter() {
                    let mut separator = "";
                    if matches.is_present(ARG_USER) {
                        let author = blame.authors[line.author_index as usize].as_str();
                        write!(
                            w,
                            "{}",
                            author.unicode_pad(max_author_width, Alignment::Right, false),
                        )?;
                        separator = " ";
                    }
                    if matches.is_present(ARG_COMMIT_NUMBER) {
                        if let Some(commit_numbers) = &blame.commit_numbers {
                            let commit_number =
                                format!("#{}", commit_numbers[line.commit_id_index as usize]);
                            write!(
                                w,
                                "{}{:>width$}",
                                separator,
                                commit_number,
                                width = max_commit_number_width + 1
                            )?
                        }
                        separator = " ";
                    }
                    if !matches.is_present(ARG_NO_COMMIT_ID) {
                        write!(w, "{}", separator)?;
                        render_commit_id(
                            None,
                            " ",
                            "blamed changeset",
                            &map_commit_ids(
                                blame.commit_ids[line.commit_id_index as usize].values(),
                            ),
                            &schemes,
                            w,
                        )?;
                        separator = " ";
                    }
                    if matches.is_present(ARG_DATE) || matches.is_present(ARG_DATE_SHORT) {
                        let blame_date = datetime(&blame.dates[line.date_index as usize]);
                        let blame_date_formatted = if matches.is_present(ARG_DATE_SHORT) {
                            blame_date.format("%F")
                        } else {
                            blame_date.format("%+")
                        };
                        write!(w, "{}{}", separator, blame_date_formatted)?;
                        separator = " ";
                    }
                    if separator != "" {
                        separator = ":";
                    }
                    if matches.is_present(ARG_TITLE) {
                        let title = match line.title_index {
                            Some(title_index) => match blame.titles.as_ref() {
                                Some(titles) => titles[title_index as usize].as_str(),
                                None => "",
                            },
                            None => "",
                        };
                        write!(
                            w,
                            "{}{}",
                            separator,
                            title.unicode_pad(title_width, Alignment::Left, true)
                        )?;
                        separator = ":";
                    }
                    if matches.is_present(ARG_PARENT_LINE_RANGE) {
                        let mut plr = String::with_capacity(max_parent_line_range_width);
                        if let Some(parent_index) = line.parent_index {
                            if parent_index != 0 {
                                write!(plr, "({})", parent_index)?;
                            }
                        }
                        if let Some(path_index) = line.parent_path_index {
                            write!(plr, "[{}]", blame.paths[path_index as usize])?;
                        }
                        if let (Some(start), Some(length)) =
                            (line.parent_start_line, line.parent_range_length)
                        {
                            if length == 0 {
                                write!(plr, "+{}", start)?;
                            } else {
                                write!(plr, "{}-{}", start, start + length - 1)?;
                            }
                        }
                        write!(
                            w,
                            "{}{}",
                            separator,
                            plr.unicode_pad(max_parent_line_range_width, Alignment::Right, false)
                        )?;
                        separator = ":";
                    }
                    if matches.is_present(ARG_ORIGIN_PATH) {
                        let origin_path = blame.paths[line.path_index as usize].as_str();
                        write!(
                            w,
                            "{}{}",
                            separator,
                            if origin_path != path { origin_path } else { "" }.unicode_pad(
                                max_origin_path_width,
                                Alignment::Right,
                                false
                            )
                        )?;
                        separator = ":";
                    }
                    if matches.is_present(ARG_ORIGIN_LINE_NUMBER) {
                        write!(
                            w,
                            "{}{:>width$}",
                            separator,
                            line.origin_line,
                            width = max_origin_line_width
                        )?;
                        separator = ":";
                    }
                    if matches.is_present(ARG_LINE_NUMBER) {
                        write!(
                            w,
                            "{}{:>width$}",
                            separator,
                            line.line,
                            width = max_line_width
                        )?;
                        separator = ":";
                    }
                    if separator != "" {
                        separator = ": ";
                    }
                    write!(
                        w,
                        "{}{}\n",
                        separator,
                        line.contents.as_deref().unwrap_or_default()
                    )?;
                }
                Ok(())
            }
            thrift::Blame::UnknownField(id) => {
                Err(format_err!("Unknown thrift::Blame field id: {}", id))
            }
        }
    }