Add custom command support with new CustomCommands module

This commit is contained in:
kake26 2025-04-15 13:51:22 -05:00
parent 7807ee5ee3
commit 5d879616e8
Signed by: kake26
GPG key ID: E0A989B571D1F99F
2 changed files with 28 additions and 0 deletions

22
lib/CustomCommands.pm Normal file
View file

@ -0,0 +1,22 @@
package CustomCommands;
our %COMMANDS = (
# Example command:
# 'hello' => sub { print "Hello, World!\n"; return 1; }
'quit' => sub { exit; return 1; }
);
sub handle {
my ($command) = @_;
# Check if this is a custom command
foreach my $cmd (keys %COMMANDS) {
if ($command =~ /^$cmd\b/) {
return $COMMANDS{$cmd}->($command);
}
}
return 0; # Not a custom command
}
1;

View file

@ -6,6 +6,7 @@ use Environment::SQLite;
use Term::ReadLine; use Term::ReadLine;
use Cwd; use Cwd;
use File::HomeDir; use File::HomeDir;
use CustomCommands;
$ENV{TERM} = 'xterm-256color' unless exists $ENV{TERM}; $ENV{TERM} = 'xterm-256color' unless exists $ENV{TERM};
@ -59,6 +60,11 @@ while (1) {
# Skip empty commands # Skip empty commands
next unless length $command; next unless length $command;
# Try custom commands first
if (CustomCommands::handle($command)) {
next;
}
# Handle cd command specially # Handle cd command specially
if ($command =~ /^cd\s*(.*)/) { if ($command =~ /^cd\s*(.*)/) {
my $dir = $1 || $ENV{HOME}; my $dir = $1 || $ENV{HOME};