use crate::config::Config;
use crate::file_entry::{FileEntry, FileType};
use crate::sort::{SortOptions, sort_entries};
use colored::{ColoredString, Colorize};
use unicode_width::UnicodeWidthStr;
pub fn format_short(entries: Vec<FileEntry>, config: &Config, sort: SortOptions) {
format_short_with_max_rows(entries, config, config.display.max_rows, sort);
}
pub fn format_short_compact(entries: Vec<FileEntry>, config: &Config, sort: SortOptions) {
format_short_with_max_rows(entries, config, config.display.compact_max_rows, sort);
}
fn format_short_with_max_rows(
mut entries: Vec<FileEntry>,
config: &Config,
max_rows: usize,
sort: SortOptions,
) {
sort_entries(&mut entries, sort);
if sort.is_custom() {
let rows = if max_rows == 0 {
entries.len().div_ceil(3).max(1)
} else {
max_rows
};
format_sorted_entries_with_max_rows(entries, rows, config.display.column_spacing, config);
return;
}
let mut directories: Vec<FileEntry> = Vec::new();
let mut executables: Vec<FileEntry> = Vec::new();
let mut regular_files: Vec<FileEntry> = Vec::new();
for entry in entries {
match entry.get_file_type() {
FileType::Directory => directories.push(entry),
FileType::Executable => executables.push(entry),
FileType::RegularFile => regular_files.push(entry),
}
}
let column_spacing = config.display.column_spacing;
if max_rows > 0 {
format_with_max_rows(
directories,
executables,
regular_files,
max_rows,
column_spacing,
config,
);
} else {
format_single_column_per_type(
directories,
executables,
regular_files,
column_spacing,
config,
);
}
}
fn format_sorted_entries_with_max_rows(
entries: Vec<FileEntry>,
max_rows: usize,
column_spacing: usize,
config: &Config,
) {
let widths = compact_column_widths(&entries, max_rows, config);
for row in 0..max_rows {
let mut line = String::new();
let mut has_content = false;
for (column, width) in widths.iter().enumerate() {
if column > 0 {
line.push_str(&" ".repeat(column_spacing));
}
if let Some(entry) = entries.get(column * max_rows + row) {
line.push_str(&format_short_entry(entry, config));
let actual_width = entry_width(entry, config);
line.push_str(&" ".repeat(width - actual_width));
has_content = true;
} else {
line.push_str(&" ".repeat(*width));
}
}
if has_content {
println!("{}", line.trim_end());
}
}
}
fn format_entry(
icon: &str,
icon_colored: ColoredString,
filename_colored: ColoredString,
) -> String {
if icon.is_empty() {
format!("{}", filename_colored)
} else {
format!("{} {}", icon_colored, filename_colored)
}
}
fn display_width(icon: &str, filename: &str) -> usize {
UnicodeWidthStr::width(icon)
+ if icon.is_empty() { 0 } else { 1 }
+ UnicodeWidthStr::width(filename)
}
fn format_with_max_rows(
directories: Vec<FileEntry>,
executables: Vec<FileEntry>,
regular_files: Vec<FileEntry>,
max_rows: usize,
column_spacing: usize,
config: &Config,
) {
let directory_widths = compact_column_widths(&directories, max_rows, config);
let executable_widths = compact_column_widths(&executables, max_rows, config);
let file_widths = compact_column_widths(®ular_files, max_rows, config);
let sections = [
(&directories, directory_widths),
(&executables, executable_widths),
(®ular_files, file_widths),
];
for row in 0..max_rows {
let mut line = String::new();
let mut has_any_content = false;
let mut has_previous_section = false;
for (entries, widths) in §ions {
if widths.is_empty() {
continue;
}
if has_previous_section {
line.push_str(&" ".repeat(column_spacing));
}
has_previous_section = true;
has_any_content |= append_compact_section(
&mut line,
entries,
widths,
row,
max_rows,
column_spacing,
config,
);
}
if has_any_content {
println!("{}", line.trim_end());
}
}
}
fn compact_column_widths(entries: &[FileEntry], max_rows: usize, config: &Config) -> Vec<usize> {
entries
.chunks(max_rows)
.map(|column| {
column
.iter()
.map(|entry| entry_width(entry, config))
.max()
.unwrap_or(0)
})
.collect()
}
fn append_compact_section(
line: &mut String,
entries: &[FileEntry],
widths: &[usize],
row: usize,
max_rows: usize,
column_spacing: usize,
config: &Config,
) -> bool {
let mut has_content = false;
for (column, width) in widths.iter().enumerate() {
if column > 0 {
line.push_str(&" ".repeat(column_spacing));
}
if let Some(entry) = entries.get(column * max_rows + row) {
line.push_str(&format_short_entry(entry, config));
has_content = true;
}
let actual_width = entries
.get(column * max_rows + row)
.map(|entry| entry_width(entry, config))
.unwrap_or(0);
line.push_str(&" ".repeat(width - actual_width));
}
has_content
}
fn entry_width(entry: &FileEntry, config: &Config) -> usize {
let filename = entry.path.to_string_lossy();
let icon = entry.get_icon_custom(&config.icons);
display_width(icon.as_str(), filename.as_ref())
}
fn format_short_entry(entry: &FileEntry, config: &Config) -> String {
let filename = entry.path.to_string_lossy();
let icon = entry.get_icon_custom(&config.icons);
let filename_colored = match entry.get_file_type() {
FileType::Directory | FileType::Executable => {
filename.color(entry.get_color(&config.colors)).bold()
}
FileType::RegularFile => filename.color(entry.get_color(&config.colors)),
};
format_entry(
icon.as_str(),
icon.color(entry.get_icon_color(&config.icons.colors)),
filename_colored,
)
}
fn format_single_column_per_type(
directories: Vec<FileEntry>,
executables: Vec<FileEntry>,
regular_files: Vec<FileEntry>,
column_spacing: usize,
config: &Config,
) {
let dir_width = directories
.iter()
.map(|e| {
let filename = e.path.to_string_lossy();
let icon = e.get_icon_custom(&config.icons);
display_width(icon.as_str(), filename.as_ref())
})
.max()
.unwrap_or(0);
let exec_width = executables
.iter()
.map(|e| {
let filename = e.path.to_string_lossy();
let icon = e.get_icon_custom(&config.icons);
display_width(icon.as_str(), filename.as_ref())
})
.max()
.unwrap_or(0);
let file_width = regular_files
.iter()
.map(|e| {
let filename = e.path.to_string_lossy();
let icon = e.get_icon_custom(&config.icons);
display_width(icon.as_str(), filename.as_ref())
})
.max()
.unwrap_or(0);
let max_rows = *[directories.len(), executables.len(), regular_files.len()]
.iter()
.max()
.unwrap_or(&0);
for i in 0..max_rows {
let mut line = String::new();
if dir_width > 0 {
if i < directories.len() {
let entry = &directories[i];
let filename = entry.path.to_string_lossy();
let icon = entry.get_icon_custom(&config.icons);
let actual_width = display_width(icon.as_str(), filename.as_ref());
line.push_str(&format_entry(
icon.as_str(),
icon.color(entry.get_icon_color(&config.icons.colors)),
filename.color(entry.get_color(&config.colors)).bold(),
));
if actual_width < dir_width {
line.push_str(&" ".repeat(dir_width - actual_width));
}
} else {
line.push_str(&" ".repeat(dir_width));
}
if exec_width > 0 || file_width > 0 {
line.push_str(&" ".repeat(column_spacing));
}
}
if exec_width > 0 {
if i < executables.len() {
let entry = &executables[i];
let filename = entry.path.to_string_lossy();
let icon = entry.get_icon_custom(&config.icons);
let actual_width = display_width(icon.as_str(), filename.as_ref());
line.push_str(&format_entry(
icon.as_str(),
icon.color(entry.get_icon_color(&config.icons.colors)),
filename.color(entry.get_color(&config.colors)).bold(),
));
if actual_width < exec_width {
line.push_str(&" ".repeat(exec_width - actual_width));
}
} else {
line.push_str(&" ".repeat(exec_width));
}
if file_width > 0 {
line.push_str(&" ".repeat(column_spacing));
}
}
if i < regular_files.len() {
let entry = ®ular_files[i];
let filename = entry.path.to_string_lossy();
let icon = entry.get_icon_custom(&config.icons);
let actual_width = display_width(icon.as_str(), filename.as_ref());
line.push_str(&format_entry(
icon.as_str(),
icon.color(entry.get_icon_color(&config.icons.colors)),
filename.color(entry.get_color(&config.colors)),
));
if actual_width < file_width {
line.push_str(&" ".repeat(file_width - actual_width));
}
}
println!("{}", line.trim_end());
}
}