pshell/lib/CustomCommands.pm

50 lines
1 KiB
Perl

package CustomCommands;
use Cwd;
use File::HomeDir;
our %COMMANDS = (
# Built-in commands
'cd' => sub {
my ($command) = @_;
if ($command =~ /^cd\s*(.*)/) {
my $dir = $1 || $ENV{HOME};
chdir $dir or warn "Could not change to directory $dir: $!";
return 1;
}
return 0;
},
'export' => sub {
my ($command, $env) = @_;
if ($command =~ /^export\s+(\w+)=(.*)/) {
$ENV{$1} = $2;
$env->set($1, $2);
return 1;
}
return 0;
},
'env' => sub {
print "$_=$ENV{$_}\n" for sort keys %ENV;
return 1;
},
# Example custom commands
'quit' => sub { exit; return 1; }
);
sub handle {
my ($command, $env) = @_;
# Check if this is a custom command
foreach my $cmd (keys %COMMANDS) {
if ($command =~ /^$cmd\b/) {
return $COMMANDS{$cmd}->($command, $env) ? 1 : 0;
}
}
return -1; # Command not found
}
1;