Move built-in commands to CustomCommands module and improve command handling
This commit is contained in:
parent
5d879616e8
commit
3b6dffa176
2 changed files with 37 additions and 26 deletions
|
@ -1,22 +1,50 @@
|
||||||
package CustomCommands;
|
package CustomCommands;
|
||||||
|
|
||||||
|
use Cwd;
|
||||||
|
use File::HomeDir;
|
||||||
|
|
||||||
our %COMMANDS = (
|
our %COMMANDS = (
|
||||||
# Example command:
|
# Built-in commands
|
||||||
# 'hello' => sub { print "Hello, World!\n"; return 1; }
|
'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; }
|
'quit' => sub { exit; return 1; }
|
||||||
);
|
);
|
||||||
|
|
||||||
sub handle {
|
sub handle {
|
||||||
my ($command) = @_;
|
my ($command, $env) = @_;
|
||||||
|
|
||||||
# Check if this is a custom command
|
# Check if this is a custom command
|
||||||
foreach my $cmd (keys %COMMANDS) {
|
foreach my $cmd (keys %COMMANDS) {
|
||||||
if ($command =~ /^$cmd\b/) {
|
if ($command =~ /^$cmd\b/) {
|
||||||
return $COMMANDS{$cmd}->($command);
|
return $COMMANDS{$cmd}->($command, $env) ? 1 : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0; # Not a custom command
|
return -1; # Command not found
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
25
pshell.pl
25
pshell.pl
|
@ -61,32 +61,15 @@ while (1) {
|
||||||
next unless length $command;
|
next unless length $command;
|
||||||
|
|
||||||
# Try custom commands first
|
# Try custom commands first
|
||||||
if (CustomCommands::handle($command)) {
|
my $cmd_status = CustomCommands::handle($command, $env);
|
||||||
|
if ($cmd_status == 1) {
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
elsif ($cmd_status == -1) {
|
||||||
# Handle cd command specially
|
print "Command not found: $command\n";
|
||||||
if ($command =~ /^cd\s*(.*)/) {
|
|
||||||
my $dir = $1 || $ENV{HOME};
|
|
||||||
chdir $dir or warn "Could not change to directory $dir: $!";
|
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Handle environment variable assignment
|
|
||||||
if ($command =~ /^export\s+(\w+)=(.*)/) {
|
|
||||||
$ENV{$1} = $2;
|
|
||||||
$env->set($1, $2);
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Handle environment variable display
|
|
||||||
if ($command eq 'env') {
|
|
||||||
print "$_=$ENV{$_}\n" for sort keys %ENV;
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
# I guess we can insert more custom commands here
|
|
||||||
|
|
||||||
# Save command to history
|
# Save command to history
|
||||||
$history->add($command);
|
$history->add($command);
|
||||||
$term->addhistory($command);
|
$term->addhistory($command);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue