Tty.pm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Tty.pm,v 1.6 2016/06/28 15:28:20 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 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. use DPB::MiniCurses;
  20. package DPB::Reporter::Tty;
  21. our @ISA = qw(DPB::MiniCurses DPB::Reporter DPB::Limiter);
  22. my $extra = '';
  23. sub handle_window
  24. {
  25. my $self = shift;
  26. $self->set_cursor;
  27. $self->SUPER::handle_window;
  28. }
  29. sub set_sig_handlers
  30. {
  31. my $self = shift;
  32. $self->SUPER::set_sig_handlers;
  33. OpenBSD::Handler->register(sub {
  34. $self->reset_cursor; });
  35. }
  36. sub filter
  37. {
  38. 'report';
  39. }
  40. sub new
  41. {
  42. my $class = shift;
  43. my $state = shift;
  44. my $self = $class->make_singleton($state, @_);
  45. $self->create_terminal;
  46. $self->set_sig_handlers;
  47. # no cursor, to avoid flickering
  48. $self->set_cursor;
  49. return $self;
  50. }
  51. sub report
  52. {
  53. my ($self, $force) = @_;
  54. if ($self->{force}) {
  55. $force = 1;
  56. undef $self->{force};
  57. }
  58. $self->limit($force, 150, "REP", 1,
  59. sub {
  60. my $msg = "";
  61. for my $prod (@{$self->{producers}}) {
  62. $msg.= $prod->report;
  63. }
  64. $msg .= $extra;
  65. if ($msg ne $self->{msg} || $self->{continued}) {
  66. if (defined $self->{record}) {
  67. print {$self->{record}} "@@@", time(), "\n";
  68. print {$self->{record}} $msg;
  69. }
  70. $self->{continued} = 0;
  71. my $method = $self->{write};
  72. $self->$method($msg);
  73. $self->{msg} = $msg;
  74. }
  75. });
  76. }
  77. sub myprint
  78. {
  79. my $self = shift;
  80. for my $string (@_) {
  81. $string =~ s/^\t/ /gm; # XXX dirty hack for warn
  82. $extra .= $string;
  83. }
  84. }
  85. 1;