Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1fde4ff77 | ||
|
|
3e12aea424 | ||
|
|
0e04b9326f | ||
|
|
a3b2e633d0 |
2 changed files with 85 additions and 226 deletions
153
README.md
153
README.md
|
|
@ -2,161 +2,84 @@
|
|||
|
||||
A fast, simple command-line tool to search man pages by keyword, written in Zig.
|
||||
|
||||
## Features
|
||||
|
||||
- **Fast keyword search** using `man -k` (apropos)
|
||||
- **Section filtering** to search specific man page sections (1-9)
|
||||
- **Path resolution** to show actual file locations
|
||||
- **Verbose mode** with timing information
|
||||
- **POSIX compatible** - works on Linux and macOS
|
||||
- **Simple and reliable** - no complex dependencies
|
||||
|
||||
## Project Structure
|
||||
|
||||
The project is organized into logical modules for maintainability:
|
||||
|
||||
```
|
||||
src/
|
||||
├── main.zig # Main program entry point
|
||||
├── types.zig # Data structures and types
|
||||
├── parser.zig # Command line argument parsing
|
||||
├── search.zig # Man page search functionality
|
||||
├── display.zig # Output formatting and display
|
||||
└── performance.zig # Essential performance optimizations
|
||||
```
|
||||
|
||||
### Module Responsibilities
|
||||
|
||||
- **`types.zig`**: Contains `ManEntry`, `ManEntryList`, and `SearchConfig` structures
|
||||
- **`parser.zig`**: Handles command line argument parsing, validation, and help text
|
||||
- **`search.zig`**: Manages man page searching, parsing, and path resolution
|
||||
- **`display.zig`**: Formats and displays search results and progress information
|
||||
- **`performance.zig`**: Essential optimizations for fast string operations and I/O
|
||||
- **`main.zig`**: Orchestrates the program flow and coordinates between modules
|
||||
|
||||
## Performance Features
|
||||
|
||||
The tool is optimized for speed while maintaining simplicity:
|
||||
|
||||
- **Early filtering**: Section filtering happens during parsing for better performance
|
||||
- **Efficient I/O**: Optimized buffer sizes and reading strategies
|
||||
- **Fast string operations**: Optimized string comparisons for common cases
|
||||
- **Memory efficiency**: Exponential growth strategy for collections
|
||||
- **POSIX optimization**: Common man page directories checked first
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Search
|
||||
|
||||
```bash
|
||||
manwhere sleep # Find all man pages mentioning "sleep"
|
||||
# Output:
|
||||
# sleep
|
||||
# appsleepd
|
||||
```
|
||||
|
||||
### Interactive Selection with fzf
|
||||
|
||||
```bash
|
||||
manwhere sleep | fzf | xargs man # Select and open a man page
|
||||
```
|
||||
|
||||
### Section Filtering
|
||||
|
||||
```bash
|
||||
manwhere -s 1 sleep # Find only commands (section 1)
|
||||
manwhere --section 3 sleep # Find only library functions (section 3)
|
||||
```
|
||||
|
||||
### Verbose Mode
|
||||
```bash
|
||||
manwhere -v sleep # Show timing and processing details
|
||||
manwhere --verbose sleep # Same as above
|
||||
```
|
||||
|
||||
### Path Resolution
|
||||
```bash
|
||||
manwhere --paths sleep # Show file paths (slower but more detailed)
|
||||
```
|
||||
|
||||
### Combined Options
|
||||
```bash
|
||||
manwhere -v --paths -s 1 sleep # Verbose, with paths, section 1 only
|
||||
manwhere -s 1 -s 3 sleep # Search sections 1 and 3
|
||||
```
|
||||
|
||||
### Help
|
||||
|
||||
```bash
|
||||
manwhere -h # Show help message
|
||||
manwhere --help # Same as above
|
||||
```
|
||||
|
||||
## Man Page Sections
|
||||
|
||||
- **1**: Commands (user commands)
|
||||
- **2**: System calls
|
||||
- **3**: Library functions
|
||||
- **4**: Device files
|
||||
- **5**: File formats
|
||||
- **6**: Games
|
||||
- **7**: Miscellaneous
|
||||
- **8**: System administration
|
||||
- **9**: Kernel routines
|
||||
|
||||
## Building
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Zig 0.15.1 or later
|
||||
|
||||
### Build Commands
|
||||
|
||||
```bash
|
||||
# Debug build (good for development)
|
||||
# Debug build (for development)
|
||||
zig build
|
||||
|
||||
# Release build (maximum performance)
|
||||
zig build -Doptimize=ReleaseFast
|
||||
|
||||
# Clean build
|
||||
zig build clean
|
||||
```
|
||||
|
||||
### Install to ~/.local/bin
|
||||
```bash
|
||||
# Release build (installs to ~/.local/bin)
|
||||
zig build release
|
||||
```
|
||||
|
||||
## Performance
|
||||
## Example Workflows
|
||||
|
||||
The tool is designed to be fast and efficient:
|
||||
### Find and open a man page
|
||||
|
||||
- **Typical search time**: 700-800ms for common keywords
|
||||
- **Section filtering**: 20-30% faster than searching all sections
|
||||
- **Path resolution**: Adds ~200-300ms for detailed results
|
||||
- **Memory usage**: ~8-12MB for typical searches
|
||||
- **Scalability**: Handles large result sets efficiently
|
||||
```bash
|
||||
manwhere printf | fzf | xargs man
|
||||
```
|
||||
|
||||
### Quick lookup with preview
|
||||
|
||||
```bash
|
||||
manwhere sleep | fzf --preview 'man {}'
|
||||
```
|
||||
|
||||
### Scripting
|
||||
|
||||
```bash
|
||||
# Find all SSL-related functions
|
||||
manwhere ssl | while read name; do
|
||||
man 3 "$name" 2>/dev/null && break
|
||||
done
|
||||
```
|
||||
|
||||
## POSIX Compatibility
|
||||
|
||||
Works reliably on:
|
||||
|
||||
- **Linux**: Standard `/usr/share/man` and `/usr/local/share/man`
|
||||
- **macOS**: Standard paths plus Homebrew (`/opt/homebrew/share/man`) and MacPorts (`/opt/local/share/man`)
|
||||
- **Other Unix-like systems**: Standard man page directory structures
|
||||
|
||||
## Error Handling
|
||||
|
||||
The program provides clear error messages for:
|
||||
- Invalid command line options
|
||||
- Missing required arguments
|
||||
- Invalid section numbers
|
||||
- Multiple keywords (not supported)
|
||||
- Search failures
|
||||
|
||||
## Why Simple and Fast?
|
||||
|
||||
This tool prioritizes:
|
||||
1. **Reliability** - Works consistently across POSIX systems
|
||||
2. **Speed** - Essential optimizations without complexity
|
||||
3. **Simplicity** - Easy to understand, maintain, and debug
|
||||
4. **POSIX compatibility** - No platform-specific dependencies
|
||||
5. **Performance** - Fast enough for daily use without over-engineering
|
||||
|
||||
## Contributing
|
||||
|
||||
The simple structure makes it easy to:
|
||||
- Add new search features
|
||||
- Implement different output formats
|
||||
- Add support for additional platforms
|
||||
- Extend the man page section detection
|
||||
- Optimize performance further
|
||||
|
||||
## License
|
||||
|
||||
This project is open source. See the source code for license details.
|
||||
|
|
|
|||
158
build.zig
158
build.zig
|
|
@ -4,138 +4,43 @@ pub fn build(b: *std.Build) void {
|
|||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
// ============================================================================
|
||||
// MODULES
|
||||
// ============================================================================
|
||||
|
||||
// Main module (uses standard optimize option)
|
||||
const root_module = b.createModule(.{
|
||||
// Module
|
||||
const exe_mod = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
// Debug module (always Debug optimization)
|
||||
const debug_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = .Debug,
|
||||
});
|
||||
|
||||
// Release module (always ReleaseFast optimization)
|
||||
const release_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = .ReleaseFast,
|
||||
});
|
||||
|
||||
// Test module (uses standard optimize option)
|
||||
const test_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// EXECUTABLES
|
||||
// ============================================================================
|
||||
|
||||
// Main executable
|
||||
// Executable
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "manwhere",
|
||||
.root_module = root_module,
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
// Debug executable
|
||||
const debug_exe = b.addExecutable(.{
|
||||
.name = "manwhere-debug",
|
||||
.root_module = debug_module,
|
||||
});
|
||||
|
||||
// Release executable
|
||||
const release_exe = b.addExecutable(.{
|
||||
.name = "manwhere",
|
||||
.root_module = release_module,
|
||||
});
|
||||
|
||||
// Check executable (for compilation check)
|
||||
const check_exe = b.addExecutable(.{
|
||||
.name = "check-compile",
|
||||
.root_module = root_module,
|
||||
});
|
||||
|
||||
// Test executable
|
||||
const unit_tests = b.addTest(.{
|
||||
.root_module = test_module,
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// BUILD STEPS
|
||||
// ============================================================================
|
||||
|
||||
// Default run step
|
||||
// Run step
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
if (b.args) |args| run_cmd.addArgs(args);
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
// Debug build step
|
||||
const debug_step = b.step("debug", "Build with Debug optimization");
|
||||
b.installArtifact(debug_exe);
|
||||
debug_step.dependOn(b.getInstallStep());
|
||||
|
||||
// Debug run step
|
||||
const debug_run_cmd = b.addRunArtifact(debug_exe);
|
||||
if (b.args) |args| debug_run_cmd.addArgs(args);
|
||||
debug_run_cmd.step.dependOn(&debug_exe.step);
|
||||
const debug_run_step = b.step("debug-run", "Run the app with Debug optimization");
|
||||
debug_run_step.dependOn(&debug_run_cmd.step);
|
||||
|
||||
// Release step - build optimized binary and install to ~/.local/bin
|
||||
const release_step = b.step("release", "Build optimized binary and install to ~/.local/bin/");
|
||||
|
||||
// Get home directory from environment
|
||||
const home = std.process.getEnvVarOwned(b.allocator, "HOME") catch {
|
||||
std.debug.print("Error: HOME environment variable not set\n", .{});
|
||||
return;
|
||||
};
|
||||
defer b.allocator.free(home);
|
||||
|
||||
const install_path = std.fs.path.join(b.allocator, &.{ home, ".local", "bin" }) catch {
|
||||
std.debug.print("Error: Failed to construct install path\n", .{});
|
||||
return;
|
||||
};
|
||||
// Note: NOT freed - used by build system
|
||||
|
||||
// Create install directory if it doesn't exist
|
||||
const mkdir_cmd = b.addSystemCommand(&.{
|
||||
"mkdir", "-p", install_path,
|
||||
// Test step
|
||||
const unit_tests = b.addTest(.{
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
release_step.dependOn(&mkdir_cmd.step);
|
||||
|
||||
// Build the release executable
|
||||
const release_build = b.addInstallArtifact(release_exe, .{});
|
||||
release_build.step.dependOn(&mkdir_cmd.step);
|
||||
release_step.dependOn(&release_build.step);
|
||||
|
||||
// Copy to final destination
|
||||
const copy_cmd = b.addSystemCommand(&.{
|
||||
"cp", "zig-out/bin/manwhere", install_path,
|
||||
});
|
||||
copy_cmd.step.dependOn(&release_build.step);
|
||||
release_step.dependOn(©_cmd.step);
|
||||
|
||||
// Test step - run all unit tests
|
||||
const run_unit_tests = b.addRunArtifact(unit_tests);
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_unit_tests.step);
|
||||
|
||||
// Check step - compilation check without output
|
||||
const check_step = b.step("check", "Check code formatting and compilation");
|
||||
// Check step
|
||||
const check_exe = b.addExecutable(.{
|
||||
.name = "check-compile",
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
const check_step = b.step("check", "Check compilation");
|
||||
check_step.dependOn(&check_exe.step);
|
||||
|
||||
// Format step - format source code
|
||||
// Format step
|
||||
const fmt_step = b.step("fmt", "Format source code");
|
||||
const fmt_cmd = b.addFmt(.{
|
||||
.paths = &.{ "src", "build.zig" },
|
||||
|
|
@ -143,10 +48,41 @@ pub fn build(b: *std.Build) void {
|
|||
});
|
||||
fmt_step.dependOn(&fmt_cmd.step);
|
||||
|
||||
// Clean step - remove build artifacts
|
||||
// Clean step
|
||||
const clean_step = b.step("clean", "Remove build artifacts");
|
||||
const clean_cmd = b.addSystemCommand(&.{
|
||||
"rm", "-rf", ".zig-cache", "zig-out",
|
||||
});
|
||||
clean_step.dependOn(&clean_cmd.step);
|
||||
|
||||
// Release step
|
||||
const release_step = b.step("release", "Build and install to ~/.local/bin");
|
||||
|
||||
const release_mod = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = .ReleaseFast,
|
||||
});
|
||||
const release_exe = b.addExecutable(.{
|
||||
.name = "manwhere",
|
||||
.root_module = release_mod,
|
||||
});
|
||||
|
||||
// Get install path
|
||||
const home = std.process.getEnvVarOwned(b.allocator, "HOME") catch {
|
||||
std.debug.print("Error: HOME not set\n", .{});
|
||||
return;
|
||||
};
|
||||
defer b.allocator.free(home);
|
||||
|
||||
const bin_path = b.pathJoin(&.{ home, ".local", "bin" });
|
||||
|
||||
// Create directory
|
||||
const mkdir_cmd = b.addSystemCommand(&.{ "mkdir", "-p", bin_path });
|
||||
|
||||
// Install the binary
|
||||
const install_bin = b.addInstallBinFile(release_exe.getEmittedBin(), "manwhere");
|
||||
install_bin.step.dependOn(&mkdir_cmd.step);
|
||||
|
||||
release_step.dependOn(&install_bin.step);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue