From d6b5ce6bd83fce2267c7134672b8a84e4c1440db Mon Sep 17 00:00:00 2001 From: Eric Yu Date: Fri, 3 Jan 2014 19:48:13 +0800 Subject: [PATCH] first commit --- .watsonrc | 21 ++++++++++ bak | 77 +++++++++++++++++++++++++++++++++++ gset | 47 +++++++++++++++++++++ pacbundle | 30 ++++++++++++++ paste3 | 14 +++++++ rimedm | 9 ++++ seaf-init | 64 +++++++++++++++++++++++++++++ tenseijingo | 76 ++++++++++++++++++++++++++++++++++ timeutil | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tmux-main | 17 ++++++++ 10 files changed, 470 insertions(+) create mode 100644 .watsonrc create mode 100755 bak create mode 100755 gset create mode 100755 pacbundle create mode 100755 paste3 create mode 100755 rimedm create mode 100755 seaf-init create mode 100755 tenseijingo create mode 100755 timeutil create mode 100755 tmux-main diff --git a/.watsonrc b/.watsonrc new file mode 100644 index 0000000..2bd4f9b --- /dev/null +++ b/.watsonrc @@ -0,0 +1,21 @@ +# watson rc +# watson - inline issue manager +# [goosecode] labs + + +# Directories +[dirs] +./ + + +# Tags +[tags] +todo +fix +review + + +# Ignores +[ignore] +.git +*.swp diff --git a/bak b/bak new file mode 100755 index 0000000..e192736 --- /dev/null +++ b/bak @@ -0,0 +1,77 @@ +#!/usr/bin/env ruby +# coding: utf-8 +# desc: backup files according to a yaml file +# author: eric +require 'yaml' + +BACKUP_DIR_PREFIX = "#{Dir.home}/.sync/" +DATA_PATH = "#{Dir.home}/.backup.yaml" + +def sync_exec args + rsync_opt = '--times --recursive' + file = args[:file] + dest = args[:dest] + + if args[:relative] == true + rsync_opt << " --relative" + # don't sub! this will change args[:file] too. + file = file.sub(args[:strip], "") if args[:strip] + end + + cmd = %[rsync #{rsync_opt} '#{file}' '#{dest}'] + + # pass "test" as command parameter to dry-run. + unless ARGV.include? "test" + if system cmd + print "[" + "\e[32m" + "done" + "\e[0m" + "]" # done: green + else + print "[" + "\e[31m" + "failed" + "\e[0m" + "]" # failed: red + end + puts args[:file] + else + puts cmd + end +end + +def sync list + dest = BACKUP_DIR_PREFIX + list['backup_dir'] + # relative copy + if list['relative_copy'] == true + # if need to strip prefix + if list['strip_prefix'] + Dir.chdir list['strip_prefix'] do + list['files'].each do |file| + sync_exec file: file, + dest: dest, + relative: true, + strip: list['strip_prefix'] + end + end + # standard relative copy + else + list['files'].each do |file| + sync_exec file: file, + dest: dest, + relative: true + end + end + # normal copy + else + list['files'].each do |file| + sync_exec file: file, dest: dest + end + end +end + +# main +data = File.open(DATA_PATH) { |f| YAML.load f } +data.each do |list| + if ARGV.include? "test" + print "\e[32m" + print list['strip_prefix'] + ' ' if list['strip_prefix'] + print "=> #{BACKUP_DIR_PREFIX}#{list['backup_dir']}" + print "\e[0m" + "\n" + end + + sync list +end diff --git a/gset b/gset new file mode 100755 index 0000000..512f096 --- /dev/null +++ b/gset @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby +# coding: utf-8 +# desc: gsettings automation based on yaml data +# author: eric +require 'yaml' + +data = File.expand_path "~/.gset.yaml" +settings = YAML.load_file data + +general = settings['general'] +keybindings = settings['keybindings'] + +def exc cmd + if system cmd + print "[" + "\e[32m" + "done" + "\e[0m" + "]" # done: green + else + print "[" + "\e[31m" + "failed" + "\e[0m" + "]" # failed: red + end + + print cmd + "\n\n" +end + +# general settings +general.each do |cfg| + cfg['setting'].each do |key, value| + exc %[gsettings set #{cfg['schema']} #{key} "#{value}"] + end +end + +# custom keybindings +keybindings_path = [] + +keybindings.size.times do |i| + path = "'/org/gnome/settings-daemon/plugins/media-keys/" + + "custom-keybindings/custom#{i}/'" + keybindings_path << path + + keybindings[i].each do |key, value| + exc %[gsettings set ] + + %[org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:] + + %[#{path.delete "'"} #{key} "#{value}"] + end +end + +exc %[gsettings set ] + + %[org.gnome.settings-daemon.plugins.media-keys custom-keybindings ] + + %["[#{keybindings_path.join(", ")}]"] diff --git a/pacbundle b/pacbundle new file mode 100755 index 0000000..e02bb2d --- /dev/null +++ b/pacbundle @@ -0,0 +1,30 @@ +#!/bin/zsh +# after base system and DE is installed... +pkg=( + base-devel abs + tp_smapi dosfstools + htop lsof net-tools strace + openssh ntp powertop lm_sensors rsync namcap + cmus wget aria2 opencc units xsel imagemagick + git hgsvn ctags + dos2unix python2-eyed3 + ffmpeg vorbis-tools flac mediainfo + atool p7zip unrar file-roller + xorg-fonts-type1 ttf-freefont ttf-droid + gvim vim-systemd xterm + ibus ibus-rime + firefox firefox-i18n-zh-cn flashplugin + wine winetricks + virtualbox + jre7-openjdk jdk7-openjdk icedtea-web-java7 + pidgin skype + uget + smplayer smplayer-themes vlc gecko-mediaplayer + puddletag + audacity + nomacs gimp + faenza-icon-theme + arch-wiki-lite arch-wiki-docs + ) + +sudo pacman -S --needed $pkg diff --git a/paste3 b/paste3 new file mode 100755 index 0000000..9e8a0f3 --- /dev/null +++ b/paste3 @@ -0,0 +1,14 @@ +#!/bin/sh +if test $IS_PHONE != true ; then + case $DISTRO in + cygwin) + echo -n "$@" > /dev/clipboard + ;; + *) + echo -n "$@" | xsel -ib + echo -n "$@" | xsel -ip + ;; + esac +fi + +echo "$@" > ~/.p diff --git a/rimedm b/rimedm new file mode 100755 index 0000000..a57e0b7 --- /dev/null +++ b/rimedm @@ -0,0 +1,9 @@ +#/bin/sh +# desc: rime dict manager +rime_conf_dir=$HOME/.config/ibus/rime + +# change directory in a subshell. +( +cd $rime_conf_dir +rime_dict_manager "$@" +) diff --git a/seaf-init b/seaf-init new file mode 100755 index 0000000..1e8e77f --- /dev/null +++ b/seaf-init @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby +# coding: utf-8 +# usage: send this script and .seaf.yaml to device, and sync. +# [todo] - sync per list +require 'yaml' +require 'highline/import' + +# constants +SEAF_PATH = File.expand_path "~/.seafile-client" +LIBRARY_DATA_PATH = File.expand_path "~/.seaf.yaml" +Lib = Struct.new "Lib", :server, :account, :password, :name, :uuid, :dir + +# method +def sync lib + puts "Name: #{lib.name}" + puts "UUID: #{lib.uuid}" + puts "Sync directory: #{lib.dir}" + + cmd = %[seaf-cli sync -s '#{lib.server}' -l '#{lib.uuid}' + -d "#{lib.dir}" -u '#{lib.account}' -p '#{lib.password}'] + + if system cmd + sleep 5 + puts "." * 24 + end +end + +# main +# initialize seafile if hasn't +system %[seaf-cli init -d #{SEAF_PATH}] if not Dir.exist? SEAF_PATH + +# read all libraries into lib_all +lib_all = [] +data = File.open(LIBRARY_DATA_PATH) { |d| YAML.load d } +data.each do |list| + # [todo] - ask for password only if there is unsynced libs in the list. + # get password for this list + puts "Server: #{list['server']}" + puts "Account: #{list['account']}" + password = ask("Your Password: ") { |q| q.echo = "*" } + puts "=" * 24 + + # add libraries into lib_all + list['libraries'].each do |lib| + lib_all << Lib.new( list['server'], list['account'], password, + lib['name'], lib['uuid'], + File.expand_path(lib['dir']) + ) + end +end + +# get unsynced libraries +synced_uuid = %x[seaf-cli list].scan /\h{8}-\h{4}-\h{4}-\h{4}-\h{12}/ +lib_unsynced = lib_all.delete_if { |l| synced_uuid.include? l.uuid } + +# sync libraries +lib_unsynced.each do |lib| + if $? !=nil and not $?.success? + puts "[seaf-init]" + "\e[31m" + "Erro occured." + "\e[0m" + break + end + + sync lib +end diff --git a/tenseijingo b/tenseijingo new file mode 100755 index 0000000..dc3ffa2 --- /dev/null +++ b/tenseijingo @@ -0,0 +1,76 @@ +#!/usr/bin/env ruby +# coding: utf-8 +# desc: get today's Tenseijingo column +# author: eric +require 'date' +require 'moji' + +Encoding.default_internal = "UTF-8" + +# variables +file = Dir.home + '/.sync/diudiudui/tensei/tensei.txt' + +today = Date.today +yesterday = today.prev_day + +cache_dir = Dir.home + '/.sync/diudiudui/tensei/cache/' +cache_file = cache_dir + "tensei-#{today.strftime '%y%m%d'}.txt" +cache_file_yesterday = cache_dir + + "tensei-#{yesterday.strftime '%y%m%d'}.txt" + +title_date = Moji.han_to_zen "#{today.year}年 #{today.mon}月#{today.day}日" + +page_url = 'http://www.asahi.com/paper/column.html' +element = '#MainInner .ArticleText' + +# main +puts "tenseijingo downloader" + +if File.exist? cache_file + puts "today's job is already done." + exit +end + +# get today's content +require 'nokogiri' +require 'open-uri' + +begin + puts "fetching web content..." + page = Nokogiri::HTML open(page_url, 'r:eucjp') +rescue + p $! + puts "error occured, exit." + exit +else + puts "content got" +end + +content = page.css(element).text.strip +success_message = "column is downloaded." + +# check newspaper holiday +if File.exist? cache_file_yesterday + content_yesterday = File.read( cache_file_yesterday, + { encoding: 'sjis' } + ).chomp + + if content == content_yesterday + content = "【休刊日】" + success_message = "oops, newspaper holiday :)" + end +end + +# write cache +File.open(cache_file, 'w:sjis') { |f| f.puts content } + +# add to file +File.open file, 'a:sjis' do |f| + f.puts + f.puts "[#5字下げ]#{title_date}[#「#{title_date}」は中見出し]" + f.puts + f.puts content +end + +# done +puts success_message diff --git a/timeutil b/timeutil new file mode 100755 index 0000000..940c31b --- /dev/null +++ b/timeutil @@ -0,0 +1,115 @@ +#!/usr/bin/env ruby +# author: eric +require 'optparse' +class Tiime #{{{1 + attr_reader :time + + def initialize(t) + if t.class == Fixnum + # 由整数构造对象 + @time = t + else + # 由字符构造对象 + # 用正则式抽取数字字符, + digits = t.scan /\d+/ + # 根据数组的元素个数进行判断 + # 只有一个则全为秒,两个则为mm:ss,三个则是hh:mm:ss + # 全部转换为秒单位 + case digits.length + when 1 + @time = digits[0].to_i + when 2 + @time = digits[0].to_i*60 + digits[1].to_i + when 3 + @time = digits[0].to_i*3600 + digits[1].to_i*60 + digits[2].to_i + end + end + end + + def + other + # 与other相加,并返回新Tiime对象 + Tiime.new(@time + other.time) + end + + def - other + # 与other求差,得绝对值并返回新Tiime对象 + Tiime.new((@time - other.time).abs) + end + + def to_s + # 输出字符串,以秒形式 + @time.to_s + end + + def to_hms + # 转成hh:mm:ss格式的字符串 + h, h_left = @time/3600, @time%3600 + m, s = h_left/60, h_left%60 + format "%02d:%02d:%02d\n", h, m, s + end + + def to_ms + # 转成mm:ss格式的字符串 + m, s = @time/60, @time%60 + format "%02d:%02d", m, s + end +end + +OptionParser.new do |opts| #{{{1 + # 帮助文档的顶部条 + opts.banner =<<-END + Time Utility... + Usage: timeutil.rb time [options] + END + + $options = {} + + # 设定开关参数的初始值 + $options[:to] = "sec" + # 设定开关 + opts.on('-t TIME_FORMAT', '--to', + 'output format: sec|ms|hms, omit: sec') \ + do |format| + $options[:to] = format + end + + $options[:add] = 0 + opts.on('-a TIME', '--add', 'add given time') \ + do |time| + $options[:add] = time + end + + $options[:minus] = 0 + opts.on('-m TIME', '--minus', 'minus given time') \ + do |time| + $options[:minus] = time + end + + # 打印用法说明 + opts.on('-h', '--help', 'display this screen') \ + do + puts opts + exit + end + +end.parse! # 执行分析,从入口参数组中去除开关及其参数,留下操作对象本身 + +# main {{{1 +if ARGV.length != 0 + # 计算加减结果 + result = Tiime.new(ARGV[0]) + + Tiime.new($options[:add]) - + Tiime.new($options[:minus]) + # 根据指定的输出格式输出 + case $options[:to] + when "sec" + puts result.to_s + when "hms" + puts result.to_hms + when "ms" + puts result.to_ms + else + # 默认以秒单位输出 + puts result.to_s + end +end diff --git a/tmux-main b/tmux-main new file mode 100755 index 0000000..5ea06fe --- /dev/null +++ b/tmux-main @@ -0,0 +1,17 @@ +#!/bin/zsh +if ! tmux has-session -t m &> /dev/null; then + # create tmux "m" session if it isn't running. + tmux new -d -s m -n ra ranger + tmux new-window -t m: +fi + +if [[ $1 != start ]]; then + # if not invoked as background autostart script, + # then tmux attach the session. + if [[ $DISTRO == cygwin || $IS_PHONE == true ]]; then + tmux attach -t m + else + # to maxmize window: --maximize + gnome-terminal -x tmux attach -t m + fi +fi