プログラミング

WSL BashとPowershellで簡単なタイマーアプリを作る

WSL BashとPowershellで簡単なタイマーアプリを作る方法について紹介します。

声で知らせてくれるタイマーです。タイマーが過ぎたら経過時間を表示したりします。

e.g. ttstimer "15m" "cooking"で15分経過したらcooking timer fired. 15 m passed.と声で知らせてくれます。


ttstimer(){

: e.g. ttstimer "15m" "cooking"

json=
ttstimer_d=
ttstimer_w=

[ -z "${1}" ] && return 1

# python3 - $@ <<EOF | tee >(read ttstimer_d < <(parsejson '[0]')) >(read ttstimer_w < <(parsejson '[1]')) # => x
json="$(python3 - $@ <<EOF
# -*- coding: utf-8 -*-

import sys
args = sys.argv

import re
import json

text = """${1}"""
result = re.match(r'(\d+)([a-z]{1})', text, re.M)
print (json.dumps(list(result.groups())))

EOF
)"


[ -z "${json}" ] && return 1

ttstimer_d="$( echo $json | parsejson '[0]' )"
ttstimer_w="$( echo $json | parsejson '[1]' )"

# sleep ${1} && tts "${2} timer fired. ${ttstimer_d} ${ttstimer_w} passed." && termdown
    sleep ${1} && ( tts "${2} timer fired. ${ttstimer_d} ${ttstimer_w} passed." & termdown )

}

最初3行だったんだけど気づいたら肥大していた。

その他のコード

// .bashrc

parsejson(){
    : e.g. parsejson '[0]["title"]'
    python3 -c "import json,sys;print(json.load(sys.stdin)${1})"
}

tts(){
powershell.exe - <<EOF
tts "${1}"
EOF
}

// Microsoft.PowerShell_profile.ps1

function tts(){

Add-Type -AssemblyName System.speech
$tts = New-Object System.Speech.Synthesis.SpeechSynthesizer

$Phrase = @"
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
xml:lang="en-US">
<voice xml:lang="en-US">
<prosody rate="1">
<p>$($args[0])</p>
</prosody>
</voice>
</speak>
"@
$tts.SpeakSsml($Phrase)

}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です