Tokei

repository·master·Indexed 12 days ago

https://github.com/xampprocky/tokei

A high-performance code statistics tool that counts files, lines, code, comments, and blanks across over 150 programming languages. Available as a CLI tool and a Rust library, version 14.0.0 supports multiple output formats including JSON, YAML, and CBOR, and respects .gitignore and .ignore files by default.

Tokens
9.8K
Snippets
42
Records
52
Agent score
97%

What's inside Tokei

  1. Run Tokei using Docker

    master

    Tokei is available as an alpine-based Docker image. You can build it using earthly +docker.

    To analyze a specific path:

    docker run --rm -v /path/to/analyze:/src tokei .

    To analyze the current directory (Linux):

    docker run --rm -v $(pwd):/src tokei .
  2. Exclude files and directories from Tokei

    master

    Tokei respects .gitignore and .ignore files by default. To exclude additional files or directories, use the --exclude flag, which follows .gitignore syntax, or define exclusions in a .tokeignore file.

    tokei ./foo --exclude *.rs
  3. How to add a new supported language

    master
    Tokei's language definitions are stored in languages.json. To add support for a new language, you must define it in that file. Detailed instructions for adding and testing a new language can be found in the project's CONTRIBUTING.md file.
  4. Output Tokei statistics in different formats

    master

    While the default output is human-readable for terminals, you can use the --output flag to export data in machine-readable formats.

    Note: You must compile Tokei with the appropriate feature flags to enable serialization support.

    Enabling features during installation

    • All formats: cargo install tokei --features all
    • CBOR: cargo install tokei --features cbor
    • YAML: cargo install tokei --features yaml

    Supported formats

    • JSON: --output json
    • YAML: --output yaml
    • CBOR: --output cbor
    tokei ./foo --output json
  5. Run Tokei fuzzing targets

    master

    To execute a fuzzing job, use the cargo +nightly fuzz run command followed by the target name. The process will continue running until interrupted with Ctrl+C.

    Execution Options

    • Use multiple cores: Use the --jobs flag to specify the number of parallel jobs.
    • Limit input size: To increase fuzzing speed (at the risk of missing bugs that only appear in large files), pass -max_len via the -- separator.
    • Shared Corpus: Since parse_from_slice_panic and parse_from_slice_total use identical input formats, you can share a common corpus directory to improve efficiency.
    # Run a specific target
    cargo +nightly fuzz run <target>
    
    # Run with 6 cores
    cargo +nightly fuzz run <target> --jobs=6
    
    # Run with a maximum input length of 200
    cargo +nightly fuzz run <target> -- -max_len=200
    
    # Run both parse targets using a shared corpus
    cargo +nightly fuzz run parse_from_slice_{panic,total} fuzz/corpus/common
  6. Install Tokei via Package Managers

    master

    Tokei can be installed on various platforms using standard package managers.

    Unix

    • Alpine Linux: apk add tokei
    • Arch Linux: pacman -S tokei
    • Fedora: sudo dnf install tokei
    • OpenSUSE: sudo zypper install tokei
    • Void Linux: sudo xbps-install tokei
    • Nix/NixOS: nix-env -i tokei
    • FreeBSD: pkg install tokei
    • NetBSD: pkgin install tokei
    • Cargo: cargo install tokei
    • Conda: conda install -c conda-forge tokei

    macOS

    • Homebrew: brew install tokei
    • MacPorts: sudo port install tokei

    Windows

    • Winget: winget install XAMPPRocky.tokei
    • Scoop: scoop install tokei
    # Example for Alpine Linux
    apk add tokei
    
    # Example for Homebrew
    brew install tokei
    
    # Example for Winget
    winget install XAMPPRocky.tokei
  7. How Tokei tracks language syntax and state

    master

    Tokei uses a SyntaxCounter to manage the state of a file during analysis. The counter operates in three primary modes to ensure accurate counting of code, comments, and strings:

    • plain mode: The default state. Blanks are counted as blanks, and the parser looks for triggers to enter string or comment modes.
    • string mode: Triggered when the parser enters a string literal. While in this mode, comments are ignored and cannot trigger a transition to comment mode.
    • comment mode: Triggered when the parser enters a comment block. While in this mode, strings are ignored and cannot trigger a transition to string mode.

    This state machine allows Tokei to correctly distinguish between actual code and text that looks like code but is actually inside a comment or a string literal.

    /// - `plain` mode: This is the normal state, blanks are counted as blanks, 
    ///   string literals can trigger `string` mode, and comments can trigger `comment` mode.
    /// - `string` mode: This when the state machine is current inside a string 
    ///   literal for a given language, comments cannot trigger `comment` mode while 
    ///   in `string` mode.
    /// - `comment` mode: This when the state machine is current inside a comment 
    ///   for a given language, strings cannot trigger `string` mode while in 
    ///   `comment` mode.