From 3814d5f1b8ad82e54a9042ef6c6acc4f8c60084b Mon Sep 17 00:00:00 2001 From: Nemo Date: Wed, 27 May 2020 14:36:09 +0530 Subject: [PATCH] remove git stats, add i3-pomodor and djvu2pdf --- djvu2pdf | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++ git_stats.sh | 50 ------------ i3-pomodoro | 202 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 416 insertions(+), 50 deletions(-) create mode 100755 djvu2pdf delete mode 100755 git_stats.sh create mode 100755 i3-pomodoro diff --git a/djvu2pdf b/djvu2pdf new file mode 100755 index 0000000..4e97e08 --- /dev/null +++ b/djvu2pdf @@ -0,0 +1,214 @@ +#!/bin/sh +# (c) Christoph Sieghart 2007 - 2011 +# Source: https://0x2a.at/site/projects/djvu2pdf/ + +# djvu2pdf + +# Copyright: 2007-2009 Christoph Sieghart + +# The home page of djvu2pdf is at: +# http://www.0x2a.at/s/projects/djvu2pdf + +# The entire code base may be distributed under the terms of the GNU General +# Public License (GPL), which appears immediately below. Alternatively, all +# of the source code as any code derived from that code may instead be +# distributed under the GNU Lesser General Public License (LGPL), at the +# choice of the distributor. The complete text of the LGPL appears at the +# bottom of this file. + +# See /usr/share/common-licenses/(GPL|LGPL) + +VERSION=0.9.2 + +# Flags for commandline options +S_FLAG=0 +C_FLAG=0 + +trap 'if [ $S_FLAG -eq 1 ]; then clean_temp_dir; clean_cursor; fi' 0 1 2 + +#***************************************************************************** +# - Functions +#***************************************************************************** + +check_print_error() { + if [ $? -ne 0 ] + then + echo "Error: $0: $1" + exit 1 + fi +} + +print_version() { + echo "djvu2pdf $VERSION - Christoph Sieghart " +} + +print_help() { + print_version + echo -e "\n Usage: ./djvu2pdf filename.djvu [filename.djvu] ...\n" + echo " -h Prints this help" + echo " -v Prints the version number" + echo " -s Show status messages (A little bit slower - every page" + echo " gets dumped on its own and later recombined" + echo -e " -c Don't use terminal escape sequences to move cursor \n" + exit 0 +} + +print_quiet() { + if [ $S_FLAG -eq 1 ]; then + echo -en $1 + fi +} + +move_cursor() { + if [ $S_FLAG -eq 1 ]; then + if [ $C_FLAG -eq 1 ]; then + echo -ne "\n" + return + else + # cursor magic + echo -en "\033[35D" + fi + fi +} + +clean_cursor() { + # if the cursor 'magic' messes anything up + if [ $S_FLAG -eq 1 ]; then + if [ $C_FLAG -eq 0 ]; then + tput sgr0 + fi + fi +} + +clean_temp_files() { + rm -rf "$TEMP"/* >/dev/null 2>&1 +} + +clean_temp_dir() { + rm -rf "$TEMP" >/dev/null 2>&1 +} + +#***************************************************************************** +# - Programm checks +#***************************************************************************** + +# MacOS and BSD compatibility +which seq >/dev/null 2>&1 +if [ $? -eq 0 ]; then + SEQ=seq +else + SEQ=jot +fi + +for i in "ddjvu djvulibre" "gs ghostscript"; do + BINARY=$(echo $i | awk '{print $1'}) + PACKAGE=$(echo $i | awk '{print $2'}) + which $BINARY >/dev/null 2>&1 + check_print_error "$BINARY not found. Install $PACKAGE." +done + + +#***************************************************************************** +# - Commandline options +#***************************************************************************** + +while getopts hvcs opt +do + case "$opt" in + v) print_version + exit 0;; + h) print_help;; + c) C_FLAG=1;; + s) S_FLAG=1;; + \?) exit 1;; + esac +done + +shift `expr $OPTIND - 1` + +if [ $C_FLAG -eq 1 ]; then + if [ $S_FLAG -eq 0 ]; then + echo -e "Error: $0: The option -c only makes sense with -s" + exit 1 + fi +fi + +if [ -z "$1" ]; then + echo -e "Error: $0: No file specified for conversion" + exit 1 +fi + +#***************************************************************************** +# - Main loop +#***************************************************************************** + +while [ $# -gt 0 ]; do + + if [ ! -f "$1" ]; then + echo -e "Error: $0: File '$1' not found" + exit 1 + fi + + FILENAME=$1 + FILEBASE=`basename "$FILENAME" .djvu` + OUTPUTFILE="${FILEBASE}.pdf" + + if [ $S_FLAG -eq 0 ]; then + ddjvu -format=pdf "${FILENAME}" "${OUTPUTFILE}" 2> /dev/null + else + + #***************************************************************************** + # - Temporary Files + #***************************************************************************** + + TEMP="${TMPDIR:=/tmp}/djvu2pdf.$$" + mkdir $TEMP + check_print_error "Creating temporary directory failed!" + + # Child processes should use our temporary directory + TMPDIR=$TEMP + + PAGES=`djvudump "$FILENAME" | grep pages | awk '{print $8;}'` + ZEROCOUNT=$(expr `echo $PAGES | wc -m` - 1) + COUNT=1 + TENS=1 + + print_quiet "Converting $FILENAME to $OUTPUTFILE\n" + + # + # We dump every page and print the status message + # + + for COUNT in `$SEQ 1 $PAGES`; + do + if [ `expr $COUNT / $TENS` -gt 0 ] + then + TENS=`expr $TENS \* 10` + ZEROCOUNT=`expr $ZEROCOUNT - 1` + ZEROS=$(for i in `$SEQ 1 $ZEROCOUNT`; do echo -n 0; done) + fi + + ddjvu -format=pdf -page $COUNT "$FILENAME" "$TEMP/$FILEBASE.${ZEROS}$COUNT.pdf" 2> /dev/null + + print_quiet "Page $COUNT/$PAGES dumped" + move_cursor + done + + clean_cursor + + # + # The pages get combined into one big happy .pdf + # + + print_quiet "\nDumping finished - writing $OUTPUTFILE\n" + + gs -dSAFER -dQUIET -dBATCH -sDEVICE=pdfwrite -sOutputFile="$OUTPUTFILE" -DNOPAUSE "$TEMP/$FILEBASE".*.pdf + check_print_error "Error in creating pdf file" + + clean_temp_files + fi + + shift +done + +exit 0 diff --git a/git_stats.sh b/git_stats.sh deleted file mode 100755 index 01348a4..0000000 --- a/git_stats.sh +++ /dev/null @@ -1,50 +0,0 @@ -function git_history { - git log $filename | grep "Date: " -} - -function first_commit { - git_history | tail -1 -} - -function last_commit { - git_history | head -1 -} - -function number_of_commits { - git_history | wc -l -} - -function legible_output { - # $2: filename - # $1: lines of code - # $3: number of commits - # $6: month (first) - # $7: date (first) - # $9: year (first) - # $13: month (last) - # $14: date (last) - # $16: year (last) - awk '{print $2 "," $1 "," $3 "," $6 " " $7 " " $9 "," $13 " " $14 " " $16}' -} - -function csv_lines_for { - for filename in $(find . -iname "*.$1"); do - echo "`wc -l $filename` `number_of_commits` `first_commit` `last_commit`" | - legible_output | - xargs echo - done -} - -function create_csv { - echo "filename,lines of code,number of commits,date of first commit,date of last commit" - - for argument in "$@" - do - csv_lines_for $argument - done -} - -cd $1 -create_csv $@ -cd - - diff --git a/i3-pomodoro b/i3-pomodoro new file mode 100755 index 0000000..22ebf98 --- /dev/null +++ b/i3-pomodoro @@ -0,0 +1,202 @@ +#!/usr/bin/env python3 +# Source: https://github.com/rkashapov/i3blocks-pomodoro +# +# MIT License +# +# Copyright (c) 2019 Rustam Kashapov +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +""" +Simple Pomodoro app inspired by spt project. +The app designed to be used with i3blocks. +""" + +import errno +import os +import socket +from itertools import cycle +from subprocess import call +from time import sleep +from threading import Thread + + +def notify(text): + call(["notify-send", "Pomodoro", text]) + + +def daemonize(): + if os.fork() > 0: + exit() + os.setsid() + if os.fork() > 0: + exit() + + +class AlredyStartedError(Exception): + pass + + +class Cycle: + def __init__(self, items): + self._items = items + self._iter = None + self.reset() + + def reset(self): + self._iter = cycle(self._items) + + def __iter__(self): + return self + + def __next__(self): + return next(self._iter) + + +class Pomodoro: + def __init__(self, cycle): + self._cycle = cycle + self._state = "Paused" + self._remaining = 0 + self._do_reset = False + self._running = False + + @property + def state(self): + state = self._state + if not self._running: + state = "Paused" + minutes, seconds = divmod(self._remaining, 60) + return "{0} {1:02d}:{2:02d}".format(state, minutes, seconds) + + def toggle(self): + self._running = not self._running + + def reset(self): + self._do_reset = True + self._cycle.reset() + + def start(self): + for timeout, state, message in self._cycle: + if self._do_reset: + self._do_reset = False + + if self._running: + self._state = state + notify(message) + + self._remaining = timeout * 60 + while self._remaining: + if self._do_reset: + break + sleep(1) + if self._running: + self._remaining -= 1 + self._state = state + + +class App: + ADDRESS = '/tmp/pomodoro.sock' + PAUSE = b"pause" + RESET = b"reset" + DISPLAY = b"display" + + def __init__(self, pomodoro): + self._thread = Thread(target=self._handle_connections) + self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self._pomodoro = pomodoro + + def run(self): + try: + self._sock.bind(self.ADDRESS) + except socket.error as e: + if e.errno != errno.EADDRINUSE: + raise e + + try: + self._sock.connect(self.ADDRESS) + except socket.error as err: + if err.errno != errno.ECONNREFUSED: + raise e + os.unlink(self.ADDRESS) + self._sock.bind(self.ADDRESS) + else: + raise AlredyStartedError() + + daemonize() + + Thread(target=self._pomodoro.start).start() + self._handle_connections() + + def display(self): + self._send(self.DISPLAY) + + def toggle(self): + self._send(self.PAUSE) + + def reset(self): + self._send(self.RESET) + + def _send(self, command): + self._sock.sendall(command) + print(self._sock.recv(32).decode("utf-8")) + + def _handle_connections(self): + self._sock.listen() + while True: + conn, _ = self._sock.accept() + command = conn.recv(16) + if command == self.PAUSE: + self._pomodoro.toggle() + elif command == self.RESET: + self._pomodoro.reset() + + conn.sendall(self._pomodoro.state.encode("utf-8")) + conn.close() + + +app = App( + Pomodoro( + Cycle( + [ + (25, "Working", "Time to start working!"), + (5, "Resting", "Time to start resting!"), + (25, "Working", "Time to start working!"), + (5, "Resting", "Time to start resting!"), + (25, "Working", "Time to start working!"), + (5, "Resting", "Time to start resting!"), + (25, "Working", "Time to start working!"), + (15, "Long Break", "Time to take some nap!"), + ] + ) + ) +) + + +if __name__ == "__main__": + try: + app.run() + except AlredyStartedError: + button = os.getenv("BLOCK_BUTTON", "").lower() + if button == "1": + app.toggle() + elif button == "3": + app.reset() + else: + app.display()