mirror of
https://github.com/theniceboy/.config.git
synced 2025-12-26 14:44:57 +08:00
new scripts
This commit is contained in:
parent
e16cede414
commit
34e9a1427e
3 changed files with 227 additions and 0 deletions
77
bin/ciu
Executable file
77
bin/ciu
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'colorize'
|
||||
require 'json'
|
||||
|
||||
# Ciu, short for CanIUse
|
||||
# A shell script that downloads JSON data and displays results
|
||||
# in table form with colors
|
||||
module Ciu
|
||||
MAX_AGE = 86_400 # 1 day in seconds
|
||||
STATUSSES = { 'rec' => 'rc', 'unoff' => 'un', 'other' => 'ot' }.freeze
|
||||
COL = ' '.freeze
|
||||
VER_BACK = 2
|
||||
FETCH_URL = 'https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json'.freeze
|
||||
STAT_SYMS = { 'n' => '-', 'y' => '+', 'p' => '~' }.freeze
|
||||
LIST = %w[ie edge safari ios_saf opera
|
||||
op_mob firefox chrome android bb].freeze
|
||||
NAMES = { 'firefox' => 'ff', 'chrome' => 'chr', 'safari' => 'saf',
|
||||
'ios_saf' => 'saf_ios', 'opera' => 'opr', 'op_mob' => 'opr_mob',
|
||||
'android' => 'andr' }.freeze
|
||||
|
||||
def self.features
|
||||
data
|
||||
end
|
||||
|
||||
def self.row(ft)
|
||||
[
|
||||
status(ft['status']).green,
|
||||
support(ft['usage_perc_y']).light_black,
|
||||
title(ft['title']),
|
||||
stats(ft['stats']).light_black
|
||||
].join(COL).bold
|
||||
end
|
||||
|
||||
def self.title(str, maxw = 30, fill = '...')
|
||||
return str[0..(maxw - fill.size - 1)] + fill if str.size >= maxw
|
||||
str + (' ' * (maxw - str.size))
|
||||
end
|
||||
|
||||
def self.status(status)
|
||||
'[' + (STATUSSES[status] || status) + ']'
|
||||
end
|
||||
|
||||
def self.support(percentage)
|
||||
(percentage.to_s + '%').ljust(6)
|
||||
end
|
||||
|
||||
def self.stats(stats)
|
||||
LIST.map do |browser|
|
||||
stats[browser].values.last(VER_BACK)
|
||||
.map { |ypn| STAT_SYMS.fetch ypn, '-' }
|
||||
.join + (NAMES[browser] || browser)
|
||||
end.join COL
|
||||
end
|
||||
|
||||
def self.data
|
||||
@data ||= begin
|
||||
path = File.expand_path '~/.caniuse-fzf-data'
|
||||
exists = File.exist? path
|
||||
update = exists && File.mtime(path).to_i < Time.now.to_i - MAX_AGE
|
||||
|
||||
exists && !update ? File.read(path) : download!
|
||||
end
|
||||
end
|
||||
|
||||
def self.download!
|
||||
path = File.expand_path '~/.caniuse-fzf-data'
|
||||
json = JSON.parse `curl --silent #{FETCH_URL}`
|
||||
data = json['data'].values.map(&method(:row)).join "\n"
|
||||
|
||||
File.open(path, 'w') { |f| f.write data }
|
||||
data
|
||||
end
|
||||
end
|
||||
|
||||
puts Ciu.features
|
||||
147
bin/fimg
Executable file
147
bin/fimg
Executable file
|
|
@ -0,0 +1,147 @@
|
|||
#!/usr/bin/env bash
|
||||
# This is just an example how ueberzug can be used with fzf.
|
||||
# Copyright (C) 2019 Nico Baeurer
|
||||
|
||||
# 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 <https://www.gnu.org/licenses/>.
|
||||
readonly BASH_BINARY="$(which bash)"
|
||||
readonly REDRAW_COMMAND="toggle-preview+toggle-preview"
|
||||
readonly REDRAW_KEY="µ"
|
||||
declare -r -x DEFAULT_PREVIEW_POSITION="right"
|
||||
declare -r -x UEBERZUG_FIFO="$(mktemp --dry-run --suffix "fzf-$$-ueberzug")"
|
||||
declare -r -x PREVIEW_ID="preview"
|
||||
|
||||
|
||||
function is_option_key [[ "${@}" =~ ^(\-.*|\+.*) ]]
|
||||
function is_key_value [[ "${@}" == *=* ]]
|
||||
|
||||
|
||||
function map_options {
|
||||
local -n options="${1}"
|
||||
local -n options_map="${2}"
|
||||
|
||||
for ((i=0; i < ${#options[@]}; i++)); do
|
||||
local key="${options[$i]}" next_key="${options[$((i + 1))]:---}"
|
||||
local value=true
|
||||
is_option_key "${key}" || \
|
||||
continue
|
||||
if is_key_value "${key}"; then
|
||||
<<<"${key}" \
|
||||
IFS='=' read key value
|
||||
elif ! is_option_key "${next_key}"; then
|
||||
value="${next_key}"
|
||||
fi
|
||||
options_map["${key}"]="${value}"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
function parse_options {
|
||||
declare -g -a script_options=("${@}")
|
||||
declare -g -A mapped_options
|
||||
map_options script_options mapped_options
|
||||
declare -g -r -x PREVIEW_POSITION="${mapped_options[--preview-window]%%:[^:]*}"
|
||||
}
|
||||
|
||||
|
||||
function start_ueberzug {
|
||||
mkfifo "${UEBERZUG_FIFO}"
|
||||
<"${UEBERZUG_FIFO}" \
|
||||
ueberzug layer --parser bash --silent &
|
||||
# prevent EOF
|
||||
3>"${UEBERZUG_FIFO}" \
|
||||
exec
|
||||
}
|
||||
|
||||
|
||||
function finalise {
|
||||
3>&- \
|
||||
exec
|
||||
&>/dev/null \
|
||||
rm "${UEBERZUG_FIFO}"
|
||||
&>/dev/null \
|
||||
kill $(jobs -p)
|
||||
}
|
||||
|
||||
|
||||
function calculate_position {
|
||||
# TODO costs: creating processes > reading files
|
||||
# so.. maybe we should store the terminal size in a temporary file
|
||||
# on receiving SIGWINCH
|
||||
# (in this case we will also need to use perl or something else
|
||||
# as bash won't execute traps if a command is running)
|
||||
< <(</dev/tty stty size) \
|
||||
read TERMINAL_LINES TERMINAL_COLUMNS
|
||||
|
||||
case "${PREVIEW_POSITION:-${DEFAULT_PREVIEW_POSITION}}" in
|
||||
left|up|top)
|
||||
X=1
|
||||
Y=1
|
||||
;;
|
||||
right)
|
||||
X=$((TERMINAL_COLUMNS - COLUMNS - 2))
|
||||
Y=1
|
||||
;;
|
||||
down|bottom)
|
||||
X=1
|
||||
Y=$((TERMINAL_LINES - LINES - 1))
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
function draw_preview {
|
||||
calculate_position
|
||||
|
||||
>"${UEBERZUG_FIFO}" declare -A -p cmd=( \
|
||||
[action]=add [identifier]="${PREVIEW_ID}" \
|
||||
[x]="${X}" [y]="${Y}" \
|
||||
[width]="${COLUMNS}" [height]="${LINES}" \
|
||||
[scaler]=forced_cover [scaling_position_x]=0.5 [scaling_position_y]=0.5 \
|
||||
[path]="${@}")
|
||||
# add [synchronously_draw]=True if you want to see each change
|
||||
}
|
||||
|
||||
|
||||
function print_on_winch {
|
||||
# print "$@" to stdin on receiving SIGWINCH
|
||||
# use exec as we will only kill direct childs on exiting,
|
||||
# also the additional bash process isn't needed
|
||||
</dev/tty \
|
||||
exec perl -e '
|
||||
require "sys/ioctl.ph";
|
||||
while (1) {
|
||||
local $SIG{WINCH} = sub {
|
||||
ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV;
|
||||
};
|
||||
sleep;
|
||||
}' \
|
||||
"${@}" &
|
||||
}
|
||||
|
||||
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
trap finalise EXIT
|
||||
parse_options "${@}"
|
||||
# print the redraw key twice as there's a run condition we can't circumvent
|
||||
# (we can't know the time fzf finished redrawing it's layout)
|
||||
print_on_winch "${REDRAW_KEY}${REDRAW_KEY}"
|
||||
start_ueberzug
|
||||
|
||||
export -f draw_preview calculate_position
|
||||
SHELL="${BASH_BINARY}" \
|
||||
fzf --preview "draw_preview {}" \
|
||||
--preview-window "${DEFAULT_PREVIEW_POSITION}" \
|
||||
--bind "${REDRAW_KEY}:${REDRAW_COMMAND}" \
|
||||
"${@}"
|
||||
fi
|
||||
3
bin/git-pull-all
Executable file
3
bin/git-pull-all
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch --track $brname $remote/$brname || true; done 2>/dev/null
|
||||
Loading…
Add table
Add a link
Reference in a new issue