From 5d879616e8ad2b991295c71d824fe08849b5af00 Mon Sep 17 00:00:00 2001 From: kake26 Date: Tue, 15 Apr 2025 13:51:22 -0500 Subject: [PATCH] Add custom command support with new CustomCommands module --- lib/CustomCommands.pm | 22 ++++++++++++++++++++++ pshell.pl | 6 ++++++ 2 files changed, 28 insertions(+) create mode 100644 lib/CustomCommands.pm diff --git a/lib/CustomCommands.pm b/lib/CustomCommands.pm new file mode 100644 index 0000000..19f84e7 --- /dev/null +++ b/lib/CustomCommands.pm @@ -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; diff --git a/pshell.pl b/pshell.pl index 30c1d0e..d65a829 100755 --- a/pshell.pl +++ b/pshell.pl @@ -6,6 +6,7 @@ use Environment::SQLite; use Term::ReadLine; use Cwd; use File::HomeDir; +use CustomCommands; $ENV{TERM} = 'xterm-256color' unless exists $ENV{TERM}; @@ -59,6 +60,11 @@ while (1) { # Skip empty commands next unless length $command; + # Try custom commands first + if (CustomCommands::handle($command)) { + next; + } + # Handle cd command specially if ($command =~ /^cd\s*(.*)/) { my $dir = $1 || $ENV{HOME};