A C++ command-line auto-completion tool using trie data structures and the GNU Readline library.
This project implements an interactive command-line interface with intelligent auto-completion capabilities. It uses a trie (prefix tree) data structure to efficiently store and retrieve completions, providing fast prefix-based suggestions similar to shell completion.
- Trie-based Auto-completion: Fast prefix matching using an optimized trie data structure
- System Command Loading: Automatically loads all available system commands from PATH
- Custom Word Lists: Support for loading custom completion dictionaries from files
- Interactive Interface: Real-time completion using GNU Readline
- Tree Visualization: Generate Graphviz DOT format output to visualize the trie structure
- Memory Safe: Proper memory management with RAII and smart destruction
- Node Class: Represents individual trie nodes with character data and child pointers
- Completer Class: Main trie implementation with insertion, search, and completion methods
- Utility Functions: File loading, command discovery, and tree visualization
completer.hpp- Header definitions for Node and Completer classescompleter.cpp- Core trie implementation and readline integrationutils.cpp- Utility functions for loading data and tree visualizationMakefile- Build configuration
- C++14 compatible compiler (g++/clang++)
- GNU Readline library development headers
- Make
makeThis creates an executable named completer with the following compilation flags:
-Wall -Wextrafor comprehensive warnings-std=c++14for C++14 standard-fsanitize=addressfor memory debugging-lreadlinefor readline library linking
make cleanRun without arguments to load system commands:
./completerLoad completions from custom files:
./completer wordlist1.txt wordlist2.txtOnce running, the tool provides an interactive prompt:
- Tab completion: Press Tab to see available completions
- Type
tree: Generate DOT format visualization of the trie structure - Ctrl+C or Ctrl+D: Exit the program
complete : git<TAB>
git gitk git-upload-pack
complete : git
prefix -> git
complete : tree
digraph {
Node0[label="~"]
Node1[label="g"]
Node0 -> Node1[weight=9]
...
}
The trie is implemented with:
- Root node containing sentinel value (-1)
- Character-based branching for efficient prefix matching
- Null terminator ('\0') marking word boundaries
- Dynamic memory allocation with proper cleanup
- Prefix Search: Traverse trie following input characters
- Subtree Collection: Gather all complete words from matching subtree
- Result Formatting: Convert to readline-compatible string array
- PATH Scanning: Automatically discovers executable commands
- File Loading: Reads custom dictionaries line by line
- Readline Integration: Seamless tab completion interface
Custom word list files should contain one word per line:
word1
word2
word3
The implementation uses RAII principles:
- Automatic cleanup via destructors
- Exception-safe resource management
- Address sanitizer integration for debugging
The tree command generates Graphviz DOT format output for visualizing the trie structure. Save the output to a .dot file and render with:
./completer > output.dot
# Type 'tree' when prompted
dot -Tpng output.dot -o trie.pngWhen modifying the code:
- Maintain C++14 compatibility
- Follow existing code style and naming conventions
- Ensure proper memory management
- Test with address sanitizer enabled
This project is provided as-is for educational and development purposes.