Skip to content

Commit

Permalink
[issue_891] give more space for Files column (#933)
Browse files Browse the repository at this point in the history
* [issue_891] give more space for Files column

+ put columns width magic numbers into some consts

* [issue_891] adjust .gitignore

* [issue_891] adjust column widths for all places

New consts for that are moved into dedicated consts file
  • Loading branch information
adam-tokarski authored Jun 18, 2024
1 parent 1770db0 commit 1428a7e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
41 changes: 35 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -413,7 +417,7 @@ impl Cli {
}),
Some(Streaming::Simple) => Some(|l: LanguageType, e| {
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$}",
l.name(),
e.name.to_string_lossy().to_string(),
e.stats.lines(),
Expand Down
27 changes: 17 additions & 10 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -151,9 +155,11 @@ impl<W> Printer<W> {
impl<W: Write> Printer<W> {
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(),
Expand Down Expand Up @@ -181,7 +187,7 @@ impl<W: Write> Printer<W> {
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()
Expand All @@ -201,7 +207,7 @@ impl<W: Write> Printer<W> {
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()
Expand Down Expand Up @@ -282,7 +288,7 @@ impl<W: Write> Printer<W> {
} 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),
Expand Down Expand Up @@ -413,7 +419,7 @@ impl<W: Write> Printer<W> {

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),
Expand Down Expand Up @@ -463,9 +469,10 @@ impl<W: Write> Printer<W> {
max_len: usize,
report: &Report,
) -> io::Result<()> {
let lines_column_width: usize = FILES_COLUMN_WIDTH + 6;
writeln!(
self.writer,
" {: <max$} {:>12} {:>12} {:>12} {:>12}",
" {: <max$} {:>lines_column_width$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
name,
report
.stats
Expand Down
12 changes: 12 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extern crate serde;
#[macro_use]
mod utils;
mod config;
mod consts;
mod language;
mod sort;
mod stats;
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate log;

mod cli;
mod cli_utils;
mod consts;
mod input;

use std::{error::Error, io, process};
Expand All @@ -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,
};

Expand Down Expand Up @@ -56,11 +61,11 @@ fn main() -> Result<(), Box<dyn Error>> {

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$} {:<PATH_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
(0..10).map(|_| "#").collect::<String>(),
(0..80).map(|_| "#").collect::<String>(),
(0..12).map(|_| "#").collect::<String>(),
Expand Down
11 changes: 7 additions & 4 deletions src/stats.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -109,7 +111,7 @@ macro_rules! display_stats {
($f:expr, $this:expr, $name:expr, $max:expr) => {
write!(
$f,
" {: <max$} {:>12} {:>12} {:>12} {:>12}",
" {: <max$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
$name,
$this.stats.lines(),
$this.stats.code,
Expand All @@ -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)
Expand Down

0 comments on commit 1428a7e

Please sign in to comment.