Script: beat.pl

Add a bar item with the Beat-Internet-Time.
Author: nils_2 — Version: 0.2 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: item, time
Added: 2010-02-25 — Updated: 2010-05-09

Download GitHub Repository

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
#
# Copyright (c) 2010 by Nils Görs <weechatter@arcor.de>
#
# just prints the beat time in Bar-Item.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Config:
# Add [beat] to your weechat.bar.status.items
# 
# refresh rate in seconds:
# /set plugins.var.perl.beat.refresh <sec>
# history:
# 0.2 wrong string was set to config

use strict;
my $prgname	= "beat";
my $version	= "0.2";
my $description	= "Shows you the Beat-Internet-Time in Bar-Item";
# default values
my $refresh	= "60";	#seconds
my %Hooks	= ();

# first function called by a WeeChat-script.
weechat::register($prgname, "Nils Görs <weechatter\@arcor.de>", $version,
                  "GPL3", $description, "", "");

if (!weechat::config_is_set_plugin("refresh")){
  weechat::config_set_plugin("refresh", $refresh);
}else{
  $refresh = weechat::config_get_plugin("refresh");
}

weechat::hook_config( "plugins.var.perl.$prgname.refresh", 'toggle_refresh', "" );
hook_timer() if ($refresh ne "0");

sub item_update{
      weechat::bar_item_update('beat');
        return weechat::WEECHAT_RC_OK
}

sub show_beat {
    my $time = shift || time();

    unless ( $time =~ /^\d+$/ ) {
        weechat::print("","$prgname: time() format is wrong.");
    }

    return sprintf "@%d", ( ( $time+3600 ) % 86400 ) / 86.4;
}

# check out config settings
sub toggle_refresh{
	$refresh = $_[2];

	if (!weechat::config_is_set_plugin("refresh")){
	  $refresh = 60 ;
	  weechat::config_set_plugin("refresh", $refresh);
	}

	if ($refresh ne "0"){
		if (defined $Hooks{timer}) {
			unhook_timer();
			hook_timer();
			return weechat::WEECHAT_RC_OK;
		}
	}
	if ($refresh eq "0"){
		if (defined $Hooks{timer}) {
			unhook_timer();
		}
	}else{
		if (not defined $Hooks{timer}){
			weechat::config_set_plugin("refresh", "0") unless hook_timer();		# fall back to '0', if hook fails
		}
	}
return weechat::WEECHAT_RC_OK;
}
my $bar_item = "";
sub hook_timer{
	$Hooks{timer} = weechat::hook_timer($refresh * 1000, 60, 0, "item_update", "");
		if ($Hooks{timer} eq '')
		{
			weechat::print("","ERROR: can't enable $prgname, hook failed");
			return 0;
		}
	$bar_item = weechat::bar_item_new($prgname, "show_beat","");
	weechat::bar_item_update('beat');
	return 1;
}
sub unhook_timer{
	weechat::bar_item_remove($bar_item);
	weechat::unhook($Hooks{timer}) if %Hooks;
	%Hooks = ();
}