Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ regex = "1.10.6"
serde_json = "1.0.125"
etcetera = "0.8.0"
table_formatter = "0.6.1"
tiktoken-rs = "0.7.0"

[dependencies.env_logger]
features = []
Expand Down
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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,
LINES_COLUMN_WIDTH, PATH_COLUMN_WIDTH, TOKENS_COLUMN_WIDTH,
},
input::Format,
};
Expand Down Expand Up @@ -446,13 +446,14 @@ impl Cli {
}),
Some(Streaming::Simple) => Some(|l: LanguageType, e| {
println!(
"{:>LANGUAGE_COLUMN_WIDTH$} {:<PATH_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
"{:>LANGUAGE_COLUMN_WIDTH$} {:<PATH_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
l.name(),
e.name.to_string_lossy().to_string(),
e.stats.lines(),
e.stats.code,
e.stats.comments,
e.stats.blanks
e.stats.blanks,
e.stats.tokens,
);
}),
_ => None,
Expand Down
26 changes: 16 additions & 10 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use tokei::{find_char_boundary, CodeStats, Language, LanguageType, Report};

use crate::consts::{
BLANKS_COLUMN_WIDTH, CODE_COLUMN_WIDTH, COMMENTS_COLUMN_WIDTH, FILES_COLUMN_WIDTH,
LINES_COLUMN_WIDTH,
LINES_COLUMN_WIDTH, TOKENS_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 NO_LANG_HEADER_ROW_LEN: usize = 69 + 13;
const NO_LANG_ROW_LEN: usize = 63 + 13;
const NO_LANG_ROW_LEN_NO_SPACES: usize = 56 + 13;
const IDENT_INACCURATE: &str = "(!)";

pub fn crate_version() -> String {
Expand Down Expand Up @@ -155,13 +155,14 @@ impl<W: Write> Printer<W> {
let files_column_width: usize = FILES_COLUMN_WIDTH + 6;
writeln!(
self.writer,
" {:<6$} {:>files_column_width$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
" {:<7$} {:>files_column_width$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
"Language".bold().blue(),
"Files".bold().blue(),
"Lines".bold().blue(),
"Code".bold().blue(),
"Comments".bold().blue(),
"Blanks".bold().blue(),
"Tokens".bold().blue(),
self.columns - NO_LANG_HEADER_ROW_LEN
)?;
self.print_row()
Expand All @@ -183,7 +184,7 @@ impl<W: Write> Printer<W> {
write!(self.writer, " ")?;
writeln!(
self.writer,
"{:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
"{:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
language
.reports
.len()
Expand All @@ -192,6 +193,7 @@ impl<W: Write> Printer<W> {
language.code.to_formatted_string(&self.number_format),
language.comments.to_formatted_string(&self.number_format),
language.blanks.to_formatted_string(&self.number_format),
language.tokens.to_formatted_string(&self.number_format),
)
}

Expand All @@ -203,7 +205,7 @@ impl<W: Write> Printer<W> {
write!(self.writer, " ")?;
writeln!(
self.writer,
"{:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
"{:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
language
.children
.values()
Expand All @@ -227,6 +229,7 @@ impl<W: Write> Printer<W> {
.blanks
.to_formatted_string(&self.number_format)
.blue(),
language.tokens.to_formatted_string(&self.number_format).blue(),
)
}

Expand Down Expand Up @@ -284,12 +287,13 @@ impl<W: Write> Printer<W> {
} else {
writeln!(
self.writer,
" {:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
" {:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_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),
comments.to_formatted_string(&self.number_format),
blanks.to_formatted_string(&self.number_format),
stats.iter().map(|s| s.tokens).sum::<usize>().to_formatted_string(&self.number_format),
)
}
}
Expand Down Expand Up @@ -414,12 +418,13 @@ impl<W: Write> Printer<W> {

writeln!(
self.writer,
" {:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
" {:>FILES_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
" ",
stats.lines().to_formatted_string(&self.number_format),
stats.code.to_formatted_string(&self.number_format),
stats.comments.to_formatted_string(&self.number_format),
stats.blanks.to_formatted_string(&self.number_format),
stats.tokens.to_formatted_string(&self.number_format),
)
}

Expand Down Expand Up @@ -467,7 +472,7 @@ impl<W: Write> Printer<W> {
let lines_column_width: usize = FILES_COLUMN_WIDTH + 6;
writeln!(
self.writer,
" {: <max$} {:>lines_column_width$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
" {: <max$} {:>lines_column_width$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
name,
report
.stats
Expand All @@ -479,6 +484,7 @@ impl<W: Write> Printer<W> {
.comments
.to_formatted_string(&self.number_format),
report.stats.blanks.to_formatted_string(&self.number_format),
report.stats.tokens.to_formatted_string(&self.number_format),
max = max_len
)
}
Expand Down
5 changes: 4 additions & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Set of common pub consts.

/// Fallback row length
pub const FALLBACK_ROW_LEN: usize = 81;
pub const FALLBACK_ROW_LEN: usize = 81 + 13;

// Column widths used for console printing.

Expand All @@ -25,3 +25,6 @@ pub const COMMENTS_COLUMN_WIDTH: usize = 12;

/// Blanks column width
pub const BLANKS_COLUMN_WIDTH: usize = 12;

/// Tokens column width
pub const TOKENS_COLUMN_WIDTH: usize = 12;
15 changes: 10 additions & 5 deletions src/language/language_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,26 @@ impl LanguageType {
// first character in the column, so removing starting whitespace
// could cause a miscount.
let line = if is_fortran { line } else { line.trim() };
let tokens = crate::tokens::count_tokens_from_bytes(line);
if line.trim().is_empty() {
(1, 0, 0)
(1, 0, 0, tokens)
} else if is_literate
|| comments.iter().any(|c| line.starts_with(c.as_bytes()))
{
(0, 0, 1)
(0, 0, 1, tokens)
} else {
(0, 1, 0)
(0, 1, 0, tokens)
}
})
.reduce(|| (0, 0, 0), |a, b| (a.0 + b.0, a.1 + b.1, a.2 + b.2))
.reduce(|| (0, 0, 0, 0), |a, b| (a.0 + b.0, a.1 + b.1, a.2 + b.2, a.3 + b.3))
};

let (mut stats, (blanks, code, comments)) = rayon::join(parse_lines, simple_parse);
let (mut stats, (blanks, code, comments, tokens)) = rayon::join(parse_lines, simple_parse);

stats.blanks += blanks;
stats.code += code;
stats.comments += comments;
stats.tokens += tokens;
stats
} else {
self.parse_lines(config, text, CodeStats::new(), syntax)
Expand Down Expand Up @@ -211,6 +213,9 @@ impl LanguageType {
}
}

let tokens = crate::tokens::count_tokens_from_bytes(lines);
stats.tokens += tokens;

stats
}

Expand Down
1 change: 1 addition & 0 deletions src/language/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl Languages {
total.comments += language.comments;
total.blanks += language.blanks;
total.code += language.code;
total.tokens += language.tokens;
total.inaccurate |= language.inaccurate;
total.children.insert(*ty, language.reports.clone());
}
Expand Down
6 changes: 6 additions & 0 deletions src/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct Language {
pub code: usize,
/// The total number of comments(both single, and multi-line)
pub comments: usize,
/// The total number of tokens.
pub tokens: usize,
/// A collection of statistics of individual files.
pub reports: Vec<Report>,
/// A map of any languages found in the reports.
Expand Down Expand Up @@ -77,6 +79,7 @@ impl Language {
summary.comments += stats.comments;
summary.code += stats.code;
summary.blanks += stats.blanks;
summary.tokens += stats.tokens;
}
}

Expand Down Expand Up @@ -104,16 +107,19 @@ impl Language {
let mut blanks = 0;
let mut code = 0;
let mut comments = 0;
let mut tokens = 0;

for report in &self.reports {
blanks += report.stats.blanks;
code += report.stats.code;
comments += report.stats.comments;
tokens += report.stats.tokens;
}

self.blanks = blanks;
self.code = code;
self.comments = comments;
self.tokens = tokens;
}

/// Checks if the language is empty. Empty meaning it doesn't have any
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod consts;
mod language;
mod sort;
mod stats;
mod tokens;

pub use self::{
config::Config,
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
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,
LANGUAGE_COLUMN_WIDTH, LINES_COLUMN_WIDTH, PATH_COLUMN_WIDTH, TOKENS_COLUMN_WIDTH,
},
input::add_input,
};
Expand Down Expand Up @@ -61,17 +61,18 @@ fn main() -> Result<(), Box<dyn Error>> {

if cli.streaming == Some(crate::cli::Streaming::Simple) {
println!(
"#{:^LANGUAGE_COLUMN_WIDTH$} {:^PATH_COLUMN_WIDTH$} {:^LINES_COLUMN_WIDTH$} {:^CODE_COLUMN_WIDTH$} {:^COMMENTS_COLUMN_WIDTH$} {:^BLANKS_COLUMN_WIDTH$}",
"language", "path", "lines", "code", "comments", "blanks"
"#{:^LANGUAGE_COLUMN_WIDTH$} {:^PATH_COLUMN_WIDTH$} {:^LINES_COLUMN_WIDTH$} {:^CODE_COLUMN_WIDTH$} {:^COMMENTS_COLUMN_WIDTH$} {:^BLANKS_COLUMN_WIDTH$} {:^TOKENS_COLUMN_WIDTH$}",
"language", "path", "lines", "code", "comments", "blanks", "tokens"
);
println!(
"{:>LANGUAGE_COLUMN_WIDTH$} {:<PATH_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$}",
"{:>LANGUAGE_COLUMN_WIDTH$} {:<PATH_COLUMN_WIDTH$} {:>LINES_COLUMN_WIDTH$} {:>CODE_COLUMN_WIDTH$} {:>COMMENTS_COLUMN_WIDTH$} {:>BLANKS_COLUMN_WIDTH$} {:>TOKENS_COLUMN_WIDTH$}",
(0..10).map(|_| "#").collect::<String>(),
(0..80).map(|_| "#").collect::<String>(),
(0..12).map(|_| "#").collect::<String>(),
(0..12).map(|_| "#").collect::<String>(),
(0..12).map(|_| "#").collect::<String>(),
(0..12).map(|_| "#").collect::<String>()
(0..12).map(|_| "#").collect::<String>(),
(0..12).map(|_| "#").collect::<String>(),
);
}

Expand Down
Loading
Loading