App-Pimpd

 view release on metacpan or  search on metacpan

lib/App/Pimpd/Info.pm  view on Meta::CPAN

  use vars qw(@ISA @EXPORT);
  @ISA = qw(Exporter);
  @EXPORT = qw(
    current
    info
    status
    stats
  );
}

use App::Pimpd;
use App::Pimpd::Validate;
use Term::ExtendedColor qw(fg clear);
use Time::Duration;

# FIXME
#get_color_support();


my(%current, %status, ,%stats);

sub _current_update {
  if($mpd->status->state ne 'stop') {
    %current = (
      'artist'     =>  $mpd->current->artist     // 'N/A',
      'album'      =>  $mpd->current->album      // 'N/A',
      'title'      =>  $mpd->current->title      // 'N/A',
      'genre'      =>  $mpd->current->genre      // 'N/A',
      'file'       =>  $mpd->current->file       // 'N/A',
      'date'       =>  $mpd->current->date       // 'N/A',
      'time'       =>  $mpd->status->time->sofar.'/'.
                       $mpd->status->time->total,
      'bitrate'    =>  $mpd->status->bitrate     // 'N/A',
      'audio'      =>  $mpd->status->audio       // 'N/A',
    );
    %status  = (
      'repeat'     =>  _on_off( $mpd->status->repeat ),
      'shuffle'    =>  _on_off( $mpd->status->random ),
      'xfade'      =>  _on_off( $mpd->status->xfade ),
      'volume'     =>  $mpd->status->volume,
      'state'      =>  $mpd->status->state,
      'list'       =>  $mpd->status->playlist,
    );
    %stats   = (
      'song'       =>  $mpd->status->song,
      'length'     =>  $mpd->status->playlistlength,
      'songs'      =>  $mpd->stats->songs,
      'albums'     =>  $mpd->stats->albums,
      'artists'    =>  $mpd->stats->artists,
    );
  }
  return;
}

sub current {
  _current_update();

  my $output;
  if(to_terminal()) {
    $output = sprintf("%s - %s on %s from %s [%s]",
        fg($c[3], fg('bold',  $current{artist})),
        fg($c[11], $current{title}),
        fg($c[0], $current{album}),
        fg($c[4], fg('bold', $current{date})),
        $current{genre},
      );
  }
  else {
    $output = sprintf("%s - %s on %s from %s [%s]",
      $current{artist},
      $current{title},
      $current{album},
      $current{date},
      $current{genre},
    );
  }

  return $output;
}

sub info {
  (undef,undef,undef,undef,undef, my $crnt_year) = localtime(time);
  $crnt_year += 1900;

  _current_update();

  for(keys(%current)) {
    $current{$_} = fg($c[14], 'N/A') if(!defined($current{$_}));
  }

  $status{'state'} = 'Playing' if($status{'state'} eq 'play');
  $status{'state'} = 'Paused'  if($status{'state'} eq 'pause');
  $status{'state'} = 'Stopped' if($status{'state'} eq 'stop');

  if($status{volume} < 0) {
    $status{volume} = 'N/A (Software Mixer)';
  }

  printf("%s %8s: %.66s\n", fg('bold', fg('251', 'S')),
    'Artist', fg($c[3], fg('bold', $current{artist}))
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('250', 'O')),
    'Album', fg($c[0], $current{album})
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('249', 'N')),
    'Song', fg($c[11], fg('bold', $current{title}))
  );

  printf("%s %8s: %.66s\n", fg('bold', fg(248, 'G')),
    'Genre', fg($c[13], $current{genre})
  );
  printf("%s %9s: %s\n", fg('bold', undef),
    'File', fg($c[7], $current{file})
  );

  printf("%s %8s: %.66s\n", fg('bold', fg('247', 'I')),
    'Date', $current{date}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('246', 'N')),
    'Time', $current{time}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('245', 'F')),
    'Bitrate', $current{bitrate}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('244', 'O')),
    'Audio', $current{audio}
  );

  print fg($c[15]);
  print '-' x 25, clear(), "\n";

  printf("%s %8s: %.66s\n", fg('bold', fg('243', 'S')),
    'Repeat', $status{repeat}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('242', 'T')),
    'Shuffle', $status{shuffle}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('242', 'A')),
    'Xfade', $status{xfade}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('241', 'T')),
    'Volume', $status{volume}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('240', 'U')),
   'State', $status{state}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('239', 'S')),
   'List V', $status{list}
  );

  print fg($c[15]);
  print '-' x 25, clear(), "\n";

  printf("%s %8s: %.66s\n", fg('bold', fg('238', 'S')),
    'Song', $stats{song}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('237', 'T')),
    'List', $stats{length} . ' songs'
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('236', 'A')),
    'Songs', $stats{songs}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('235', 'T')),
    'Albums', $stats{albums}
  );
  printf("%s %8s: %.66s\n", fg('bold', fg('234', 'S')),
   'Artists', $stats{artists}
  );
  return;
}

sub _on_off {
  my $state = shift;

  if($state > 1) {
    return "ON ($state)";
  }
  elsif($state == 1) {
    return 'ON';
  }
  return 'OFF';
}

sub stats {
  my $friendly_names = {
    artists     => 'Artists',
    albums      => 'Albums',
    songs       => 'Songs',

    uptime      => 'Uptime',
    playtime    => 'Play time',
    db_playtime => 'DB Play Time',
  };

  my $status = $mpd->stats;

  for(qw(artists albums songs)) {
    printf("%15s: %s\n", $friendly_names->{$_}, $status->$_);
  }

  print "\n";

  for my $time(qw(uptime playtime db_playtime)) {
    printf("%15s: %s\n", $friendly_names->{$time}, duration( $status->$time ));
  }

  printf("%15s: %s\n", 'DB Updated', scalar localtime( $status->db_update ));
  return;
}

sub status {
  my $friendly_names = {
    play  => 'playing',
    pause => 'paused',
    stop  => 'stopped',
  };

  my $status = $mpd->status;

  return $friendly_names->{ $status->state }
    ? $friendly_names->{ $status->state }
    : ''
}





( run in 0.912 second using v1.01-cache-2.11-cpan-39bf76dae61 )