Skip to content

Instantly share code, notes, and snippets.

@journey-ad
Last active August 14, 2017 03:59
Show Gist options
  • Save journey-ad/9b13d34ec7fd55e3629763fd91ac44e4 to your computer and use it in GitHub Desktop.
Save journey-ad/9b13d34ec7fd55e3629763fd91ac44e4 to your computer and use it in GitHub Desktop.
fish shell 的自定义函数,用于读出一段文本(类似于 macOS 的 say 命令)
function say
set text $argv[1]
set text (echo $text | tr -d '\n')
set salt (date +%s)
set str1 "2015063000000001""$text""$salt""12345678"
set sign (echo -n $str1 | md5sum | cut -d ' ' -f1)
set lang (curl -s "http://api.fanyi.baidu.com/api/trans/vip/translate?from=$lang&to=$transto&appid=2015063000000001&salt=$salt&sign=$sign" --data-urlencode "q=$text" | grep -oP '(?<="from":")[^"]*')
for i in (echo $argv | sed 's|--*|\\'\n'|g' | sed 's/[ \t]*$//g' | grep -v '^$')
echo $i | read -l option value
switch $option
case l lang
set lang $value
case s speed
set speed $value
end
end
if [ $text ]
t $text -l $lang
mpv "https://fanyi.baidu.com/gettts?lan=$lang&text=$text&spd=$speed&source=web" > /dev/null
else
echo "say text -l language(zh cte en jp kor pt de spa ru fra) -s speed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment