diff --git a/.gitignore b/.gitignore index 97caff5..9edb8e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,41 @@ +# Created by https://www.toptal.com/developers/gitignore/api/rust +# Edit at https://www.toptal.com/developers/gitignore?templates=rust + +### Rust ### +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# End of https://www.toptal.com/developers/gitignore/api/rust + + +### IDE ### .vscode -target -src/test -.settings -.commit_message -*.bk -*.rustfmt +.idea/ +*.iml + +### Other ### + +# macOS .DS_Store + +# settings +.settings .tokeirc + +# benchmark results.csv + node_modules *.code-workspace diff --git a/src/cli.rs b/src/cli.rs index c21f9d8..7de2f24 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,6 +8,10 @@ use tokei::{Config, LanguageType, Sort}; use crate::{ cli_utils::{crate_version, parse_or_exit, NumberFormatStyle}, + consts::{ + BLANKS_COLUMN_WIDTH, CODE_COLUMN_WIDTH, COMMENTS_COLUMN_WIDTH, LANGUAGE_COLUMN_WIDTH, + LINES_COLUMN_WIDTH, PATH_COLUMN_WIDTH, + }, input::Format, }; @@ -413,7 +417,7 @@ impl Cli { }), Some(Streaming::Simple) => Some(|l: LanguageType, e| { println!( - "{:>10} {:<80} {:>12} {:>12} {:>12} {:>12}", + "{:>LANGUAGE_COLUMN_WIDTH$} {:LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", l.name(), e.name.to_string_lossy().to_string(), e.stats.lines(), diff --git a/src/cli_utils.rs b/src/cli_utils.rs index 4112e33..04a42de 100644 --- a/src/cli_utils.rs +++ b/src/cli_utils.rs @@ -13,10 +13,14 @@ use num_format::ToFormattedString; use crate::input::Format; use tokei::{find_char_boundary, CodeStats, Language, LanguageType, Report}; -pub const FALLBACK_ROW_LEN: usize = 79; -const NO_LANG_HEADER_ROW_LEN: usize = 67; -const NO_LANG_ROW_LEN: usize = 61; -const NO_LANG_ROW_LEN_NO_SPACES: usize = 54; +use crate::consts::{ + BLANKS_COLUMN_WIDTH, CODE_COLUMN_WIDTH, COMMENTS_COLUMN_WIDTH, FILES_COLUMN_WIDTH, + LINES_COLUMN_WIDTH, +}; + +const NO_LANG_HEADER_ROW_LEN: usize = 69; +const NO_LANG_ROW_LEN: usize = 63; +const NO_LANG_ROW_LEN_NO_SPACES: usize = 56; const IDENT_INACCURATE: &str = "(!)"; pub fn crate_version() -> String { @@ -151,9 +155,11 @@ impl Printer { impl Printer { pub fn print_header(&mut self) -> io::Result<()> { self.print_row()?; + + let files_column_width: usize = FILES_COLUMN_WIDTH + 6; writeln!( self.writer, - " {:<6$} {:>12} {:>12} {:>12} {:>12} {:>12}", + " {:<6$} {:>files_column_width$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", "Language".bold().blue(), "Files".bold().blue(), "Lines".bold().blue(), @@ -181,7 +187,7 @@ impl Printer { write!(self.writer, " ")?; writeln!( self.writer, - "{:>6} {:>12} {:>12} {:>12} {:>12}", + "{:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", language .reports .len() @@ -201,7 +207,7 @@ impl Printer { write!(self.writer, " ")?; writeln!( self.writer, - "{:>6} {:>12} {:>12} {:>12} {:>12}", + "{:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", language .children .values() @@ -282,7 +288,7 @@ impl Printer { } else { writeln!( self.writer, - " {:>6} {:>12} {:>12} {:>12} {:>12}", + " {:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", stats.len().to_formatted_string(&self.number_format), (code + comments + blanks).to_formatted_string(&self.number_format), code.to_formatted_string(&self.number_format), @@ -413,7 +419,7 @@ impl Printer { writeln!( self.writer, - " {:>6} {:>12} {:>12} {:>12} {:>12}", + " {:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", " ", stats.lines().to_formatted_string(&self.number_format), stats.code.to_formatted_string(&self.number_format), @@ -463,9 +469,10 @@ impl Printer { max_len: usize, report: &Report, ) -> io::Result<()> { + let lines_column_width: usize = FILES_COLUMN_WIDTH + 6; writeln!( self.writer, - " {: 12} {:>12} {:>12} {:>12}", + " {: lines_column_width$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", name, report .stats diff --git a/src/consts.rs b/src/consts.rs new file mode 100644 index 0000000..3795d5d --- /dev/null +++ b/src/consts.rs @@ -0,0 +1,12 @@ +// Set of common pub consts. + +pub const FALLBACK_ROW_LEN: usize = 81; + +// Column widths used for console printing. +pub const LANGUAGE_COLUMN_WIDTH: usize = 10; +pub const PATH_COLUMN_WIDTH: usize = 80; +pub const FILES_COLUMN_WIDTH: usize = 8; +pub const LINES_COLUMN_WIDTH: usize = 12; +pub const CODE_COLUMN_WIDTH: usize = 12; +pub const COMMENTS_COLUMN_WIDTH: usize = 12; +pub const BLANKS_COLUMN_WIDTH: usize = 12; diff --git a/src/lib.rs b/src/lib.rs index 667dd6d..7b89e4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ extern crate serde; #[macro_use] mod utils; mod config; +mod consts; mod language; mod sort; mod stats; diff --git a/src/main.rs b/src/main.rs index dd7b3a2..62619df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate log; mod cli; mod cli_utils; +mod consts; mod input; use std::{error::Error, io, process}; @@ -11,7 +12,11 @@ use tokei::{Config, Languages, Sort}; use crate::{ cli::Cli, - cli_utils::{Printer, FALLBACK_ROW_LEN}, + cli_utils::Printer, + consts::{ + BLANKS_COLUMN_WIDTH, CODE_COLUMN_WIDTH, COMMENTS_COLUMN_WIDTH, FALLBACK_ROW_LEN, + LANGUAGE_COLUMN_WIDTH, LINES_COLUMN_WIDTH, PATH_COLUMN_WIDTH, + }, input::add_input, }; @@ -56,11 +61,11 @@ fn main() -> Result<(), Box> { if cli.streaming == Some(crate::cli::Streaming::Simple) { println!( - "#{:^10} {:^80} {:^12} {:^12} {:^12} {:^12}", + "#{:^LANGUAGE_COLUMN_WIDTH$} {:^PATH_COLUMN_WIDTH$} {:^LINES_COLUMN_WIDTH$} {:^CODE_COLUMN_WIDTH$} {:^COMMENTS_COLUMN_WIDTH$} {:^BLANKS_COLUMN_WIDTH$}", "language", "path", "lines", "code", "comments", "blanks" ); println!( - "{:>10} {:<80} {:>12} {:>12} {:>12} {:>12}", + "{:>LANGUAGE_COLUMN_WIDTH$} {:LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", (0..10).map(|_| "#").collect::(), (0..80).map(|_| "#").collect::(), (0..12).map(|_| "#").collect::(), diff --git a/src/stats.rs b/src/stats.rs index 1f1634c..5562e65 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -1,6 +1,8 @@ -use std::{collections::BTreeMap, fmt, ops, path::PathBuf}; - +use crate::consts::{ + BLANKS_COLUMN_WIDTH, CODE_COLUMN_WIDTH, COMMENTS_COLUMN_WIDTH, LINES_COLUMN_WIDTH, +}; use crate::LanguageType; +use std::{collections::BTreeMap, fmt, ops, path::PathBuf}; /// A struct representing stats about a single blob of code. #[derive(Clone, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)] @@ -109,7 +111,7 @@ macro_rules! display_stats { ($f:expr, $this:expr, $name:expr, $max:expr) => { write!( $f, - " {: 12} {:>12} {:>12} {:>12}", + " {: LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}", $name, $this.stats.lines(), $this.stats.code, @@ -125,7 +127,8 @@ impl fmt::Display for Report { let name = self.name.to_string_lossy(); let name_length = name.len(); - let max_len = f.width().unwrap_or(25); + // Added 2 to max length to cover wider Files column (see https://github.com/XAMPPRocky/tokei/issues/891). + let max_len = f.width().unwrap_or(27) + 2; if name_length <= max_len { display_stats!(f, self, name, max_len)