This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
import json
|
||||
import sys
|
||||
import re
|
||||
|
||||
from urllib.request import urlopen
|
||||
|
||||
Usage = """
|
||||
Usage: python3 dict.sh [word]
|
||||
"""
|
||||
|
||||
HAS_RICH = 0
|
||||
try:
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
from rich import print
|
||||
HAS_RICH = 1
|
||||
except:
|
||||
pass
|
||||
|
||||
def basic_paring(data):
|
||||
""" Display data in the most simple way
|
||||
"""
|
||||
word = data["word"]
|
||||
defs = data["def"]
|
||||
samples = data["sample_sentence"]
|
||||
ponetic = data["phonetic"]
|
||||
|
||||
print(word)
|
||||
|
||||
if len(ponetic.items()) > 0:
|
||||
for _, v in ponetic.items():
|
||||
print(v, end=" ")
|
||||
print()
|
||||
print()
|
||||
|
||||
if len(defs) > 0:
|
||||
for k in defs:
|
||||
print(k, defs[k])
|
||||
print()
|
||||
|
||||
for s in samples[:2]:
|
||||
print("-", end=" ")
|
||||
print(s['en'])
|
||||
print()
|
||||
|
||||
def rich_paring(data):
|
||||
console = Console()
|
||||
word = data["word"]
|
||||
defs = data["def"]
|
||||
ponetic = data["phonetic"]
|
||||
samples = data["sample_sentence"]
|
||||
|
||||
console.print(word, style="bold magenta")
|
||||
print()
|
||||
|
||||
if len(ponetic.items()) > 0:
|
||||
for _, v in ponetic.items():
|
||||
#v = v.replace('[', '\[').replace(']', ']')
|
||||
console.print(Text(v, style="yellow"))
|
||||
print()
|
||||
|
||||
if len(defs) > 0:
|
||||
for k in defs:
|
||||
console.print(Text(k, style="green"))
|
||||
console.print(Text(defs[k], style="cyan"))
|
||||
print()
|
||||
|
||||
for s in samples[:2]:
|
||||
ens = s['en'].replace('"', '\"')
|
||||
ens = re.sub(r'\*\*([^ ]*?)\*\*', r'[/][red]\1[/][yellow4]', ens)
|
||||
|
||||
text = Text.from_markup("[yellow4]" + ens + "[/]", justify="left")
|
||||
panel = Panel(text)
|
||||
|
||||
console.print(panel)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print(Usage)
|
||||
exit(1)
|
||||
|
||||
word = sys.argv[1]
|
||||
url = "http://do.useless.pw/{}".format(word)
|
||||
r = urlopen(url)
|
||||
content = r.read()
|
||||
json_content = json.loads(content)
|
||||
if not HAS_RICH:
|
||||
basic_paring(json_content)
|
||||
else:
|
||||
rich_paring(json_content)
|
||||
@@ -1,228 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# For any command require a file path, this function
|
||||
# helps to select the correct file with key words
|
||||
# Usage:
|
||||
# Add the command to desired list
|
||||
|
||||
# fzf match everything
|
||||
fzf_command_list_all=(\
|
||||
)
|
||||
|
||||
# fzf match files
|
||||
fzf_command_list_file=(\
|
||||
set_wallpaper \
|
||||
)
|
||||
|
||||
# fzf only match folders
|
||||
fzf_command_list_dir=(\
|
||||
cd \
|
||||
)
|
||||
|
||||
# in case above functions are broken
|
||||
function no_fzf(){
|
||||
for c in "${fzf_command_list_all[@]}"; do
|
||||
unalias $c
|
||||
done
|
||||
for c in "${fzf_command_list_file[@]}"; do
|
||||
unalias $c
|
||||
done
|
||||
for c in "${fzf_command_list_dir[@]}"; do
|
||||
unalias $c
|
||||
done
|
||||
}
|
||||
|
||||
function dp(){
|
||||
local debug=0
|
||||
[ "$debug" = "1" ] && echo $1
|
||||
}
|
||||
|
||||
# SHELL specific setting
|
||||
if [ ! -z "$ZSH_NAME" ]; then
|
||||
SHELL_NAME=zsh
|
||||
dp "[bash_tools]: In ZSH shell"
|
||||
# disable error output when
|
||||
# wildcard found 0 match
|
||||
setopt +o nomatch
|
||||
|
||||
#setopt extended_glob
|
||||
|
||||
# show hidden file when using wildcard
|
||||
# use in fzf_selector
|
||||
# only for zsh
|
||||
setopt dotglob
|
||||
setopt glob_dots
|
||||
else
|
||||
# temporary
|
||||
SHELL_NAME=bash
|
||||
dp "[bash_tools]: In non-ZSH shell"
|
||||
# only for bash
|
||||
shopt -s dotglob
|
||||
|
||||
# allow reverse wildcard exp. !(b*)
|
||||
# shopt -s extglob
|
||||
|
||||
# allows filename patterns which match no
|
||||
# files to expand to a null string, rather
|
||||
# than themselves
|
||||
shopt -s nullglob
|
||||
fi
|
||||
|
||||
ret=''
|
||||
|
||||
# $1 keyword
|
||||
# $2
|
||||
# 0: show all
|
||||
# 1: only show directories
|
||||
# 2: only show files
|
||||
function fzf_selector() {
|
||||
ret=$1
|
||||
if [ "$1" = "." ] || [ "$1" = ".." ] || \
|
||||
[[ "$1" == -* ]] || [[ "$1" == */* ]]; then
|
||||
dp "fzf return directly"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ $2 = 0 ]; then
|
||||
all_items=(*)
|
||||
elif [ $2 = 1 ]; then
|
||||
dp "try to find dirs"
|
||||
all_items=(*/)
|
||||
elif [ $2 = 2 ]; then
|
||||
dp "try to find files"
|
||||
if [ "$SHELL_NAME" = "zsh" ]; then
|
||||
eval "all_items=(*(^/))"
|
||||
elif [ "$SHELL_NAME" = "bash" ]; then
|
||||
# no easy way to list just regular files
|
||||
# in bash???
|
||||
all=(*)
|
||||
all_items=()
|
||||
for item in "${all[@]}"; do
|
||||
[ -f "$item" ] && all_items+=("$item")
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
dp "fzf-keyword: $1"
|
||||
files=()
|
||||
# match
|
||||
# 1. try match file/dir equals to keyword completely
|
||||
# 2. try match file/dir which keyword is substring of
|
||||
# 3. try match file/dir equals to keyword completely after ignoring cases
|
||||
# 4. try match file/dir which keyword is substring of after ignoring cases
|
||||
|
||||
for item in "${all_items[@]}"; do
|
||||
dp "item: $item"
|
||||
# 1.
|
||||
if [ "$item" = "$1" ] || [ "$item" = "$1/" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# 2.
|
||||
# this line ignore cases
|
||||
# [[ $1 =~ [A-Z] ]] && \
|
||||
grep $1 <<< $item > /dev/null 2>&1 && \
|
||||
files+=("$item")
|
||||
done
|
||||
|
||||
dp "len contains upper cases ${#files[@]}"
|
||||
|
||||
if [ "${#files[@]}" = "0" ]; then
|
||||
for item in "${all_items[@]}"; do
|
||||
# 3.
|
||||
if ([ "$SHELL_NAME" = "bash" ] && ( [ "${item,,}" = "${1,,}" ] || [ "${item,,}" = "${1,,}/" ] )) || \
|
||||
([ "$SHELL_NAME" = "zsh" ] && ( [ "${item:l}" = "${1:l}" ] || [ "${item:l}" = "${1:l}/" ] )) ; then
|
||||
ret=$item
|
||||
return 0
|
||||
fi
|
||||
# 4.
|
||||
grep -i $1 <<< $item > /dev/null 2>&1 && files+=("$item")
|
||||
done
|
||||
fi
|
||||
|
||||
if [ "${#files[@]}" = "0" ]; then
|
||||
# if grep find zero file
|
||||
ret=$1
|
||||
elif [ "${#files[@]}" = "1" ]; then
|
||||
ret=$files
|
||||
else
|
||||
select file in "${files[@]}"; do
|
||||
ret=$file
|
||||
break
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# $1 command for fzf substuting(like ls)
|
||||
# $2 original args pass to function
|
||||
# $3 command require file type (file: 0 |directory: 1)
|
||||
function fzf_command_execute() {
|
||||
local args=($@)
|
||||
local len=${#args[@]}
|
||||
|
||||
dp "args: $args"
|
||||
dp "lens: $len"
|
||||
|
||||
# zsh shell's array starts with 1 rather than 0
|
||||
if [ ! -z "$ZSH_NAME" ]; then
|
||||
dp "zsh"
|
||||
flag="${args[2]}"
|
||||
unset 'args[2]'
|
||||
command_prefix="${args[1]}"
|
||||
else
|
||||
dp "not zsh"
|
||||
flag="${args[1]}"
|
||||
unset 'args[1]'
|
||||
command_prefix="${args[0]}"
|
||||
fi
|
||||
|
||||
dp "flag: $flag"
|
||||
|
||||
if [ "$len" = "2" ]; then
|
||||
# flag will take 1 length
|
||||
# so if len == 2 means no argument is given
|
||||
${args[@]}
|
||||
return 0
|
||||
fi
|
||||
# probably work for bash
|
||||
#keyword="${args: -1}"
|
||||
|
||||
keyword=""
|
||||
slice_flag=1
|
||||
for arg in "${args[@]:2}"; do
|
||||
dp "argg $arg"
|
||||
if [ "$slice_flag" = "1" ]; then
|
||||
if [ "$keyword" = "" ]; then
|
||||
keyword="$arg"
|
||||
else
|
||||
keyword="$keyword $arg"
|
||||
fi
|
||||
elif [[ "$arg" == "-"* ]]; then
|
||||
command_prefix="$command_prefix $arg"
|
||||
else
|
||||
slice_flag=1
|
||||
fi
|
||||
done
|
||||
dp "command_prefix: $command_prefix"
|
||||
dp "keyword: $keyword"
|
||||
|
||||
# Delete last argument
|
||||
# Here we assume it is a file path
|
||||
|
||||
fzf_selector "$keyword" $flag
|
||||
dp "ret: $ret"
|
||||
|
||||
$command_prefix "$ret"
|
||||
}
|
||||
|
||||
for c in "${fzf_command_list_all[@]}"; do
|
||||
alias $c="fzf_command_execute $c 0"
|
||||
done
|
||||
for c in "${fzf_command_list_file[@]}"; do
|
||||
alias $c="fzf_command_execute $c 2"
|
||||
done
|
||||
for c in "${fzf_command_list_dir[@]}"; do
|
||||
alias $c="fzf_command_execute $c 1"
|
||||
done
|
||||
|
||||
@@ -1,23 +1,9 @@
|
||||
__dotfiles_git_prompt() {
|
||||
local branch marks=''
|
||||
branch=$(git symbolic-ref --quiet --short HEAD 2>/dev/null) || return
|
||||
git diff --no-ext-diff --quiet --ignore-submodules -- 2>/dev/null || marks+=' *'
|
||||
git diff --cached --no-ext-diff --quiet --ignore-submodules -- 2>/dev/null || marks+=' +'
|
||||
printf '\[\e[38;5;166m\](%s\[\e[38;5;204m\]%s\[\e[38;5;166m\])\[\e[0m\]' "$branch" "$marks"
|
||||
}
|
||||
|
||||
# display git status
|
||||
export GIT_PS1_SHOWDIRTYSTATE=1
|
||||
if [[ $(type -t __git_ps1) != function ]]; then
|
||||
__git_ps1(){
|
||||
|
||||
local branch=`git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/\1/'`
|
||||
|
||||
if [ -n "$branch" ]; then
|
||||
status=`git status 2>/dev/null`
|
||||
local stage_info="not staged for commit"
|
||||
local commit_info="Changes to be committed"
|
||||
|
||||
local stage_sign=''
|
||||
local commit_sign=''
|
||||
[[ "$status" == *"$stage_info"* ]] && stage_sign=" *"
|
||||
[[ "$status" == *"$commit_info"* ]] && commit_sign=" +"
|
||||
echo "(${branch}${stage_sign}${commit_sign})"
|
||||
fi
|
||||
}
|
||||
fi
|
||||
export PS1=' \[\e[0;32m\]\w\[\e[01;31m\]$(__git_ps1 "(%s)")\[\e[00m\] '
|
||||
# »
|
||||
PS1=' \[\e[38;5;106m\]\w\[\e[0m\]$(__dotfiles_git_prompt)\$ '
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
# Autoload zsh add-zsh-hook and vcs_info functions (-U autoload w/o substition, -z use zsh style)
|
||||
autoload -Uz add-zsh-hook vcs_info
|
||||
autoload -U colors && colors
|
||||
# Enable substitution in the prompt.
|
||||
setopt prompt_subst
|
||||
# Run vcs_info just before a prompt is displayed (precmd)
|
||||
add-zsh-hook precmd vcs_info
|
||||
# Enable checking for (un)staged changes, enabling use of %u and %c
|
||||
zstyle ':vcs_info:*' check-for-changes true
|
||||
# Set custom strings for an unstaged vcs repo changes (*) and staged changes (+)
|
||||
zstyle ':vcs_info:*' unstagedstr ' *'
|
||||
zstyle ':vcs_info:*' stagedstr ' +'
|
||||
# Set the format of the Git information for vcs_info
|
||||
|
||||
# 3-Digit codes for various colors:
|
||||
# https://unix.stackexchange.com/a/124409/194343
|
||||
|
||||
vcs_info='%F{166}(%b%F{204}%u%c%f%F{166})%f'
|
||||
zstyle ':vcs_info:git:*' formats $vcs_info
|
||||
|
||||
zstyle ':vcs_info:git:*' actionformats '(%b|%a%u%c)'
|
||||
|
||||
#export PS1='%F{green}%1~ %F{cyan}${vcs_info_msg_0_}%f '
|
||||
|
||||
local user="%B%(!.%F{red}.%F{green})%n%f "
|
||||
|
||||
local user_symbol='%(!.#.$)'
|
||||
local current_dir="%B%~%b"
|
||||
local vcs_branch='${vcs_info_msg_0_}'
|
||||
|
||||
#PROMPT="╭─${user}${current_dir}${vcs_branch}
|
||||
#╰─%B${user_symbol}%b "
|
||||
|
||||
#PROMPT="%F{magenta}› ${current_dir}${vcs_branch} "
|
||||
|
||||
PROMPT=" %F{106}${current_dir}%f${vcs_branch}$ "
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
|
||||
# Linux specific functions
|
||||
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
|
||||
|
||||
set_gnome_wp() {
|
||||
gsettings set org.gnome.desktop.background picture-uri "file://$1"
|
||||
gsettings set org.gnome.desktop.background picture-uri-dark "file://$1"
|
||||
}
|
||||
|
||||
function set_wallpaper(){
|
||||
local file=$1
|
||||
# convert to abs path
|
||||
if [[ ! "$file" = /* ]]; then
|
||||
file=$(pwd)/$1
|
||||
fi
|
||||
set_gnome_wp $file
|
||||
}
|
||||
|
||||
fi
|
||||
|
||||
# Mac OSX specific functions
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
|
||||
function set_wallpaper(){
|
||||
local file=$1
|
||||
# convert to abs path
|
||||
if [[ ! "$file" = /* ]]; then
|
||||
file=$(pwd)/$1
|
||||
fi
|
||||
|
||||
apple_script_single_desk="tell application \"Finder\" to set desktop picture to POSIX file \"$file\""
|
||||
osascript -e "$apple_script_single_desk"
|
||||
}
|
||||
|
||||
fi
|
||||
Reference in New Issue
Block a user