Major update to the existing python scripts and added creategif and tool for 256 color terms

This commit is contained in:
lanxu 2018-05-21 21:32:35 +03:00
parent 60ee7b0866
commit 9a9e166bdd
10 changed files with 243 additions and 1560 deletions

View File

@ -3,6 +3,6 @@ Type=Application
Name=ranger-terminal
Comment=Launches the ranger file manager
Icon=utilities-terminal
Exec=urxvt -e ranger
Exec=termite -e ranger
Categories=System;FileTools;FileManager
MimeType=inode/directory;

98
creategif.py Executable file
View File

@ -0,0 +1,98 @@
#!/usr/bin/python
"""
creategif.py to create awful gif animations from videos! :D
-h help
-i [--input] input file
-o [--output] output file
-v [--verbose] verbose level. Set it as "verbose"
-s [--size] size of the video. Give the size of the longest dimension (default: 320)
-d [--delay] delay in ms for each frame for the generated gif (default: 5)
-r [--rate] the rate in which frames are extracted (default: 10 fps)
"""
import getopt
import sys
import subprocess
from subprocess import call
import lib.helpers as helpers
__author__ = 'lanxu <jukka.lankinen@gmail.com>'
def main(argv):
ffmpeg_available = helpers.is_exe("/usr/bin/ffmpeg")
convert_available = helpers.is_exe("/usr/bin/convert")
input_file = ''
output_file = ''
loglevel = 'error'
size = '320'
delay = '5'
rate = '10'
try:
opts, args = getopt.getopt(argv,"hi:o:v:s:d:r:", ["input=","output=","verbose=", "size=", "delay=", "rate="])
except getopt.GetoptError:
print('creategif.py -i <inputfile> -o <outputfile>')
sys.exit(1)
for opt, arg in opts:
if opt == '-h':
print('creategif.py -i <inputfile> -o <outputfile> [-v]')
sys.exit()
elif opt in ("-v", "--verbose"):
loglevel = 'verbose'
elif opt in ("-i", "--input"):
input_file = arg
elif opt in ("-o", "--output"):
output_file = arg
elif opt in ("-s", "--size"):
size = arg
elif opt in ("-d", "--delay"):
delay = arg
elif opt in ("-r", "--rate"):
rate = arg
if not ffmpeg_available or not convert_available:
print('ffmpeg or convert not installed')
sys.exit(1)
if output_file == "" or input_file == "":
print('provide input and output files')
sys.exit(1)
if loglevel != "error":
print('Input file is "'+input_file+'"')
print('Output file is "'+output_file+'"')
try:
command_ffmpeg = ['ffmpeg', '-i', input_file,
'-loglevel', loglevel,
'-vf', 'scale='+size+':-1',
'-r', rate,
'-f', 'image2pipe',
'-vcodec', 'ppm',
'-']
command_convert = ['convert',
'-delay', delay,
'-loop', '0',
'-layers', 'Optimize',
'-matte', '+dither',
'-alpha', 'remove',
'-depth', '8',
'-', output_file]
ps = subprocess.Popen(command_ffmpeg, stdout=subprocess.PIPE)
val = call(command_convert, stdin=ps.stdout)
# ffmpeg returns 0 if success
if val > 0:
print('Encoding failed!')
sys.exit(2)
except:
print('Encoding failed!')
sys.exit(2)
if loglevel != "error":
print('Done.')
if __name__ == "__main__":
main(sys.argv[1:])

1537
dropbox.py

File diff suppressed because one or more lines are too long

View File

@ -3,23 +3,13 @@ import os
import re
import sys
import getopt
import lib.helpers as helpers
from subprocess import call
__author__ = 'lanxu <jukka.lankinen@gmail.com>'
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def run_command(command):
try:
return_value = call(command)
except (RuntimeError, TypeError, NameError, OSError) as error:
raise
return return_value
def main(argv):
ffmpeg_available = is_exe("/usr/bin/ffmpeg")
ffmpeg_available = helpers.is_exe("/usr/bin/ffmpeg")
inputFile = ''
outputFile = ''
@ -62,7 +52,7 @@ def main(argv):
print('Running pass 1...')
try:
val = run_command(command_pass1)
val = helpers.run_command(command_pass1)
# ffmpeg returns 0 if success
if val > 0:
print('Encoding failed')
@ -71,10 +61,10 @@ def main(argv):
# TODO Remove temp files here
print('Encoding failed!')
sys.exit(2)
print('Running pass 2..')
try:
run_command(command_pass2)
val = helpers.run_command(command_pass2)
# ffmpeg returns 0 if success
if val > 0:
print('Encoding failed')

View File

@ -2,6 +2,7 @@
"""
Archive extract script with sub-directory creation
This tool is more or less same as atool
"""
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

16
gen-ctags.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
extract_ctags()
{
LIBRARY=$1
pacman -Ql $LIBRARY | grep -E -o '/usr/include/.*\.(h|hpp)' | grep -v "/usr/include/${LIBRARY}/typeof/" > ~/.vim/tags/$LIBRARY-filelist
ctags --sort=foldcase --c++-kinds=+p --fields=+iaS --extra=+q -f --language-force=C++ ~/.vim/tags/$LIBRARY -L ~/.vim/tags/$LIBRARY-filelist
}
echo "Generating CTAGS..."
#extract_ctags boost
#extract_ctags sdl2
#extract_ctags opencv
extract_ctags mesa

View File

@ -3,19 +3,17 @@ import os
import re
import logging
import subprocess
import lib.helpers as helpers
from subprocess import check_output
FORMAT = "%(asctime)s %(levelname)s %(message)s"
logging.basicConfig(format=FORMAT, filename=__file__+'.log', level=logging.DEBUG)
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
aticonfig_available = is_exe("/usr/bin/aticonfig")
sensors_available = is_exe("/usr/bin/sensors")
aticonfig_available = helpers.is_exe("/usr/bin/aticonfig")
sensors_available = helpers.is_exe("/usr/bin/sensors")
if sensors_available:
commands = ["sensors radeon-pci-* | grep temp1", "sensors thinkpad-isa-0000 | grep ATI"]
commands = ["sensors amdgpu-pci-* | grep temp1", "sensors radeon-pci-* | grep temp1", "sensors thinkpad-isa-0000 | grep ATI"]
for try_command in commands:
command = try_command

View File

@ -3,7 +3,7 @@
# First check if lastpass is correctly initialized
if lpass ls 1>/dev/null 2>&1; then
# success
lpass ls | grep $1 | awk '{ if (match($0,/\[id: (.*)?\]/,m)) print m[1] }' | xargs lpass show
lpass ls | grep --ignore-case $1 | awk '{ if (match($0,/\[id: (.*)?\]/,m)) print m[1] }' | xargs lpass show
# --color=always --format="%ai %au %ap" # unable to use because lpass is buggy with the formatting
else
# failure

21
lib/helpers.py Normal file
View File

@ -0,0 +1,21 @@
"""
General helpers for my scripts
"""
import os
from subprocess import call
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
__author__ = 'lanxu <jukka.lankinen@gmail.com>'
def is_exe(fpath):
""" Tells if the given file is an executable """
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def run_command(command):
""" Attempts to run the given command """
try:
return_value = call(command)
except (RuntimeError, TypeError, NameError, OSError) as error:
raise
return return_value

96
tools/print256colours.sh Executable file
View File

@ -0,0 +1,96 @@
#!/bin/bash
# Tom Hale, 2016. MIT Licence.
# Print out 256 colours, with each number printed in its corresponding colour
# See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163
set -eu # Fail on errors or undeclared variables
printable_colours=256
# Return a colour that contrasts with the given colour
# Bash only does integer division, so keep it integral
function contrast_colour {
local r g b luminance
colour="$1"
if (( colour < 16 )); then # Initial 16 ANSI colours
(( colour == 0 )) && printf "15" || printf "0"
return
fi
# Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
if (( colour > 231 )); then # Greyscale ramp
(( colour < 244 )) && printf "15" || printf "0"
return
fi
# All other colours:
# 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
# See http://stackoverflow.com/a/27165165/5353461
# r=$(( (colour-16) / 36 ))
g=$(( ((colour-16) % 36) / 6 ))
# b=$(( (colour-16) % 6 ))
# If luminance is bright, print number in black, white otherwise.
# Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601
(( g > 2)) && printf "0" || printf "15"
return
# Uncomment the below for more precise luminance calculations
# # Calculate percieved brightness
# # See https://www.w3.org/TR/AERT#color-contrast
# # and http://www.itu.int/rec/R-REC-BT.601
# # Luminance is in range 0..5000 as each value is 0..5
# luminance=$(( (r * 299) + (g * 587) + (b * 114) ))
# (( $luminance > 2500 )) && printf "0" || printf "15"
}
# Print a coloured block with the number of that colour
function print_colour {
local colour="$1" contrast
contrast=$(contrast_colour "$1")
printf "\e[48;5;%sm" "$colour" # Start block of colour
printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number
printf "\e[0m " # Reset colour
}
# Starting at $1, print a run of $2 colours
function print_run {
local i
for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do
print_colour "$i"
done
printf " "
}
# Print blocks of colours
function print_blocks {
local start="$1" i
local end="$2" # inclusive
local block_cols="$3"
local block_rows="$4"
local blocks_per_line="$5"
local block_length=$((block_cols * block_rows))
# Print sets of blocks
for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do
printf "\n" # Space before each set of blocks
# For each block row
for (( row = 0; row < block_rows; row++ )) do
# Print block columns for all blocks on the line
for (( block = 0; block < blocks_per_line; block++ )) do
print_run $(( i + (block * block_length) )) "$block_cols"
done
(( i += block_cols )) # Prepare to print the next row
printf "\n"
done
done
}
print_run 0 16 # The first 16 colours are spread over the whole spectrum
printf "\n"
print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive
print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey