A highly optimized, minimal, and scalable command-line syntax highlighting tool written in Go. This program is designed as a more powerful alternative to the cat command with syntax highlighting capabilities, specifically for Linux operating systems.
Complete documentation is available in multiple languages:
The documentation includes:
- Installation and usage instructions
- Configuration guide for adding new languages
- Architecture and design overview
- Contribution guidelines
- Highly efficient: Uses a two-goroutine pipeline architecture for maximum performance
- Extensible: Supports custom syntax highlighting rules via external JSON configuration
- Minimal dependencies: Only uses Go standard library packages
- Automatic language detection: Based on file extensions
- Line ending support: Handles both LF and CRLF line endings
- Support for stdin: Can be used in command pipelines
- Standard
catcompatibility: Supports common cat flags like-n,-b,-s, and-E - Multi-language support: Includes built-in support for Go, Python, JavaScript, JSON, Markdown, XML/HTML, and SQL
- Security-focused: Uses low-level syscall operations for file I/O
- Integrated paging: Use
--lessflag to view large files with thelesspager
- Operating System: Linux (not compatible with macOS or Windows)
- Dependencies:
- Go 1.18+ (for building from source)
lesscommand (for the--lessflag functionality)
# Install from source
git clone https://github.com/AmirMahdyJebreily/hili-cat.git
cd hili-cat
./build.sh
# Or use the pre-built binary from releases
wget https://github.com/AmirMahdyJebreily/hili-cat/releases/download/pre-release0.0.1/hili-cat-linux-amd64
chmod +x hili-cat-linux-amd64
sudo mv hili-cat-linux-amd64 /usr/local/bin/hili-cat# Highlight a file with automatic language detection
hili-cat main.go
# Highlight multiple files
hili-cat file1.go file2.go
# Highlight stdin with a specific language
cat main.go | hili-cat --lang go
# Use a custom configuration file
hili-cat --config /path/to/config.json main.go
# Specify line ending format
hili-cat --line-ending crlf file.go
# Show line numbers (like cat -n)
hili-cat -n file.go
# Show only non-blank line numbers (like cat -b)
hili-cat -b file.go
# Squeeze repeated blank lines (like cat -s)
hili-cat -s file.go
# Show line endings with $ marker (like cat -E)
hili-cat -E file.go
# View highlighted file using less for pagination
hili-cat --less file.goThe default configuration is stored at /etc/highlight/config.json. A sample configuration is provided in the config directory.
You can create your own configuration file with custom syntax highlighting rules for different languages. The configuration file uses JSON format with the following structure:
{
"languages": {
"language-name": {
"extensions": ["ext1", "ext2"],
"rules": [
{
"name": "rule-name",
"pattern": "regex-pattern",
"style": "style-name"
}
],
"styles": {
"style-name": "color-name"
}
}
}
}Available styles include:
- Text styles:
bold,italic,underline - Colors:
black,red,green,yellow,blue,magenta,cyan,white - Bright colors:
brightblack,brightred,brightgreen,brightyellow,brightblue,brightmagenta,brightcyan,brightwhite
The highlight tool uses a highly efficient two-goroutine pipeline design:
- Reader Goroutine: Handles file I/O or stdin using low-level syscalls for maximum performance
- Highlighter Goroutine: Processes the data and applies syntax highlighting using regex-based rules
This pipeline approach allows for efficient streaming of data, even with large files, minimizing memory usage while maintaining high performance.
┌─────────┐ ┌─────────────┐ ┌───────────┐
│ Input │────▶│ Buffered │────▶│ Output │
│ Source │ │ Channel │ │ (Stdout) │
└─────────┘ └─────────────┘ └───────────┘
│ ▲
│ │
▼ │
┌──────────┐ ┌─────────────┐
│ Reader │ │ Highlighter │
│ Goroutine│ │ Goroutine │
└──────────┘ └─────────────┘
--config: Path to the configuration file (default:/etc/highlight/config.json)--lang: Language for syntax highlighting (required when reading from stdin)--line-ending: Line ending to use (auto,lf,crlf, default:auto)-n, --number: Number all output lines-b, --number-nonblank: Number non-blank output lines-s, --squeeze-blank: Suppress repeated empty output lines-E, --show-ends: Display $ at end of each line--less, --pager: Pipe output toless -Rcommand for paged viewing--help: Show help message
highlight is designed with performance as a primary goal:
- Buffered I/O: Uses efficient buffer sizes for optimal read/write performance
- Regexp Optimization: Precompiles regex patterns to minimize CPU usage
- Memory Management: Minimizes allocations to reduce GC overhead
- Syscall Usage: Direct syscall usage for file operations instead of higher-level abstractions
- Channel Buffering: Properly sized channels to prevent blocking in the pipeline
# Highlight a Go file with automatic language detection
highlight main.go# Pipe the output of git diff into highlight for colored diff output
git diff | highlight --lang diff# Use highlight in a script to show syntax-highlighted output
#!/bin/bash
echo "Displaying highlighted code:"
highlight --lang go main.goThe project includes a demo script that showcases the key features of the highlight tool:
# Make the demo script executable
chmod +x demo.sh
# Run the demo
./demo.shThe demo will:
- Build the highlight tool if needed
- Show syntax highlighting for Go and JSON files
- Demonstrate line numbering
- Show how to use highlight with piped input
- Display line endings
- Demonstrate highlighting multiple files
Contributions are welcome! Here are some areas where help is needed:
- Adding support for more programming languages
- Performance optimizations
- Additional formatting options
- Bug fixes and test improvements
For detailed information on how to contribute, please read the Contributing Guide.
hili-cat is designed to be easily extended with support for additional programming languages.
To learn how to add support for your favorite language, see the Language Configuration Guide.
The project uses GitHub Actions to automate builds:
- Tag
pre-release0.0.1and any version withv*prefix triggers automatic builds - Linux builds are automatically created and attached to GitHub releases
- See the workflow file in
.github/workflows/linux-build.ymlfor details
See the LICENSE file for details.