Interactive.pm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Interactive.pm,v 1.1 2015/08/24 10:16:18 espie Exp $
  3. #
  4. # Copyright (c) 2015 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. package DPB::Interactive;
  20. sub new
  21. {
  22. my $class = shift;
  23. require Term::ReadLine;
  24. bless {
  25. rl => Term::ReadLine->new('dpb'),
  26. prompt => '$ ',
  27. want_report => 1}, $class;
  28. }
  29. sub is_interactive
  30. {
  31. return 1;
  32. }
  33. sub want_report
  34. {
  35. my $self = shift;
  36. return $self->{want_report};
  37. }
  38. sub may_ask_for_commands
  39. {
  40. my $self = shift;
  41. return 0 if $self->{quitting};
  42. my $cmd = $self->{rl}->readline($self->{prompt});
  43. $self->{want_report} = 0;
  44. if ($cmd =~ m/^(?:port|pkgpath)\s+(\S+)/) {
  45. $self->{current_port} = $1;
  46. $self->{prompt} = $self->{current_port}.'$ ';
  47. } elsif ($cmd =~ m/^quit\b/i) {
  48. $self->{quitting} = 1;
  49. } else {
  50. print STDERR "Unknown command\n";
  51. }
  52. return 1;
  53. }
  54. 1;