#!/usr/bin/env perl # # chronosquirrel-bitbar.1s.pl # This is a BitBar plugin for displaying chronosquirrel status in # the MacOS menubar. It also provides in a drop-down menu a set of # commands for changing the active task. # # The current task status (taskname, task time) is displayed on the OSX # menubar. If a task isn't active, then "Off Clock" and the time off # clock will be displayed. # # Clicking on that display brings up a menu of the active tasks. # Selecting one of those tasks makes that the current task. The menu # menu is divided up based on billable and nonbillable tasks. Within # each of those lists, the tasks are sorted by the tasks' ordering # numbers. # # Revision History # 1.0 Initial revision. 180831 # # # Copyright 2018 Wayne Morrison # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # # # chronosquirrel Status # v1.0 # Wayne Morrison # squirrel-king # Show chronosquirrel status. # use strict; # # Version information. # my $NAME = "chronosquirrel-bitbar.1s.pl"; my $VERS = "$NAME version: 1.0"; ############################################################################ my @tasks; # Task info. my $csout; # Chronosquirrel output. my %billables = (); # Billable tasks/ordernums. my %nonbills = (); # Nonbillable tasks/ordernums. my @billables = (); # Billable task names. my @nonbills = (); # Nonbillable task names. my $chronosquirrel = glob("~/bin/chronosquirrel"); ############################################################################## # # Give an error message if we couldn't find chronosquirrel. # if(! -x $chronosquirrel) { print "task-timing status unknown\n"; exit(1); } # egrep #>bitbar .chronosquirrelrc ############################################################################## # # Gather the task information and build a pair of lists of task names, # sorted by the task order number. # # # Get the list of tasks and configuration status, and put them in a list. # $csout = `$chronosquirrel -tasks -rare`; @tasks = split /\n/, $csout; # # Put each active task in a hash of billable or nonbillable tasks. # The hash key is the task name and the value is the task order number. # foreach my $ln (@tasks) { my @atoms; # Pieces of output line. my $taskname; # Name of task from output line. my $billstr; # Billing string of task. my $ordernum; # Order number of task. # # Collapse multiple spaces into a single space and break the line # into its pieces. # $ln =~ s/\s+/ /g; @atoms = split / /, $ln; # # Skip inactive tasks. # next if($atoms[-2] eq 'inactive'); # # Get the order number and the billing status. # $ordernum = $atoms[-1]; $billstr = $atoms[-3]; # # Get rid of all the line's pieces except the task name. # pop @atoms; pop @atoms; pop @atoms; # # Reconstruct the remaining line pieces into the task name. # $taskname = join(' ', @atoms); # # Add the taskname/order number into the appropriate hash. # if($billstr eq 'billable') { $billables{$taskname} = $ordernum; } else { $nonbills{$taskname} = $ordernum; } } # # Build a set of lists from the hashes, sorting the list by order number. # @billables = sort { $billables{$a} <=> $billables{$b} } keys(%billables); @nonbills = sort { $nonbills{$a} <=> $nonbills{$b} } keys(%nonbills); ############################################################################## # # Start emitting stuff that will be added to the BitBar menubar item for # chronosquirrel. The first piece of output is what's shown in the menubar. # Subsequent output becomes items in a dropdown menu. # # The dropdown menu has this format: # # billable-task-1 # ... # billable-task-N # --------------- # nonbillable-task-1 # ... # nonbillable-task-M # --------------- # # # # Give the status. This is what's displayed in the menubar. # system("$chronosquirrel"); # # Start giving data for the dropdown menu. The format is determined by BitBar. # print "---\n"; print "Off Clock | color=red bash=$chronosquirrel param1=-end terminal=false\n"; # # Give an entry for each active, billable task. # foreach my $tn (@billables) { print "$tn | color=green bash=$chronosquirrel param1=\"$tn\" terminal=false\n"; } print "---\n"; # # Give an entry for each active, nonbillable task. # foreach my $tn (@nonbills) { print "$tn | bash=$chronosquirrel param1=\"$tn\" terminal=false\n"; } exit(0); 1; ############################################################################## =pod =head1 NAME B - BitBar plugin to display chronosquirrel status =head1 SYNOPSIS chronosquirrel-bitbar.1s.pl =head1 DESCRIPTION B is a B plugin for displaying B status in the MacOS menubar. It also provides a set of commands for changing the active task in a drop-down menu. The current task status (taskname and task time) is displayed on the OSX menubar. If a task isn't active, then "Off Clock" and the time off-clock will be displayed. Clicking on that display brings up a menu of the active tasks. Selecting one of those tasks makes that the current task. The menu menu is divided up based on billable and nonbillable tasks. Within each of those lists, the tasks are sorted by the tasks' ordering numbers. The I<1s> in the B name tells B to run the script once a second. This causes the displayed time to be updated every second. The I<1s> can be changed to another time interval -- such as I<5s> for five seconds, I<21s> for 21 seconds, B<8m> for eight minutes, or I<1d> for once per day. If once every second is too fast, then changing the script name will alter the frequency as desired. B B is only intended for use with B. It may be used from the command line, but there isn't really any benefit to doing so. =head1 OPTIONS B takes no options. =head1 COPYRIGHT Copyright 2018 Wayne Morrison. All rights reserved. =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 SEE ALSO B: https://getbitbar.com =head1 LICENSE Copyright 2018 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =cut