From 697c7e105f0b52f49b2658c16c554c82116c318d Mon Sep 17 00:00:00 2001 From: Alexander Gehrke Date: Thu, 2 Mar 2023 16:02:42 +0100 Subject: [PATCH] Update some scripts --- devel/update-amm | 23 +++++++++++++++-------- misc/khal-notify | 2 +- misc/mail-notify | 23 +++++++++++++---------- misc/mdcatw | 7 +++---- xdg-wrappers/sbt | 2 +- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/devel/update-amm b/devel/update-amm index 9c934a8..f38b1f7 100755 --- a/devel/update-amm +++ b/devel/update-amm @@ -1,14 +1,21 @@ #!/bin/zsh -versions=$(curl -L https://api.github.com/repos/lihaoyi/Ammonite/releases/latest) +versions=$(curl -sL https://api.github.com/repos/lihaoyi/Ammonite/releases/latest) download-version() { - ( - echo "#/usr/bin/env python --version sh" && \ - curl -L $( echo -E $versions \ - | jq -r ".assets | map(select(.name | startswith(\"$1\")))[0].browser_download_url") - ) > $2 - chmod +x $2 + version=$(jq ".assets | map(select(.name | startswith(\"$1\")))[0]" <<<$versions) + target=$(jq -r ".browser_download_url" <<<$version) + timestamp=$(jq -r ".updated_at" <<<$version) + + if [[ $1 == "-n" ]]; then + echo $target + elif [[ $(date -d $timestamp +%s) -lt $(date -r $2 +%s) ]]; then + echo "No update for $1" + else + echo "Updating $1 to $(jq .name <<<$version)" + curl -L $target > $2 + chmod +x $2 + fi } download-version 2.13 $(which amm)2.13 download-version 2.12 $(which amm)2.12 -download-version 3.1 $(which amm)3 +download-version 3.2 $(which amm)3 diff --git a/misc/khal-notify b/misc/khal-notify index f02e5fe..8b32dbe 100755 --- a/misc/khal-notify +++ b/misc/khal-notify @@ -31,7 +31,7 @@ notify() { events=$(khal list now 1h) -if [[ $events == "no events" ]]; then +if [[ $events == "No events" ]]; then exit 0 else notify "Upcoming events" "$(tail -n +2 <<<"$events"| awk '{printf("%s%s", sep, $0); sep="
"} END {print ""}')" "$(find /usr/share/icons/ -ipath \*48x48/\*appointment-soon.\* | head -n 1)" diff --git a/misc/mail-notify b/misc/mail-notify index 259dfba..574fa34 100755 --- a/misc/mail-notify +++ b/misc/mail-notify @@ -1,7 +1,7 @@ #!/usr/bin/env amm import $ivy.`com.sun.mail:javax.mail:1.6.2` -import $ivy.`com.lihaoyi::os-lib:0.7.1` +import $ivy.`com.lihaoyi::os-lib:0.8.1` import javax.mail.internet._ import os.up @@ -12,25 +12,29 @@ val icon = "/usr/share/icons/Adwaita/48x48/actions/mail-message-new.png" val cacheFile = envPath("XDG_CACHE_HOME", os.home/".cache") / "seen-mails" val maildir = envPath("MAILDIR", os.home/"Mail") +def from(msg: MimeMessage): String = + msg.getFrom.headOption + .map(_.asInstanceOf[InternetAddress]) + .map(addr => Option(addr.getPersonal).map(_.replace("&", "&")).getOrElse(addr.getAddress)) + .getOrElse("unknown") + + case class MailInfo(from: String, subject: String, mailbox: String) -object MailInfo { - def fromPath(p: os.Path, maildir: os.Path) = { +object MailInfo: + def fromPath(p: os.Path, maildir: os.Path) = val msg = readMessage(p) val address = msg.getFrom.head.asInstanceOf[InternetAddress] MailInfo( - Option(address.getPersonal).map(_.replace("&", "&")).getOrElse(address.getAddress), + from(msg), msg.getSubject.replace("&", "&"), (p/up/up relativeTo maildir).toString ) - } -} val session = javax.mail.Session.getDefaultInstance(new java.util.Properties) -def readMessage(path: os.Path) = { +def readMessage(path: os.Path): MimeMessage = val in = os.read.inputStream(path) new MimeMessage(session, in) -} def envPath(envVar: String, default: os.Path): os.Path = sys.env.get(envVar).map(s => os.Path(s)).getOrElse(default) @@ -45,9 +49,8 @@ val unseenMailPaths = newMailPaths &~ cache val unseenMails = unseenMailPaths.map(p => MailInfo.fromPath(os.Path(p), maildir)) -if (!unseenMails.isEmpty) { +if !unseenMails.isEmpty then val markup = unseenMails.map(msg => s"${msg.from}: ${msg.subject}
").mkString os.proc("notify-send", s"--icon=$icon", "Mails", markup).call() -} os.write.over(cacheFile, newMailPaths.mkString("", "\n", "\n")) diff --git a/misc/mdcatw b/misc/mdcatw index b1f105c..d1dc97a 100755 --- a/misc/mdcatw +++ b/misc/mdcatw @@ -5,11 +5,10 @@ if [[ $2 ]]; then encoding=$2 else - encoding=$(chardetect "$1" | grep -oP '(?<=: )\S*') + encoding='utf-8' + #encoding=$(chardetect "$1" | grep -op '(?<=: )\s*') fi -if ! iconv -f $encoding <<<"" &> /dev/null; then - cat "$1" -elif [[ $encoding != 'utf-8' ]]; then +if [[ $encoding != 'UTF-8' ]]; then iconv -f $encoding -t 'utf-8' < "$1" | mdcat /dev/stdin else mdcat $1 diff --git a/xdg-wrappers/sbt b/xdg-wrappers/sbt index 96cd937..e337ca2 100755 --- a/xdg-wrappers/sbt +++ b/xdg-wrappers/sbt @@ -1,7 +1,7 @@ #!/bin/zsh source ${$(realpath "$0"):h:h}/lib/next-in-path.zsh opts=( - "-Dsbt.ivy.home=$XDG_CACHE_HOME/ivy" + "-Dsbt.ivy.home=$XDG_CACHE_HOME/ivy" "-Dsbt.boot.directory=$XDG_CACHE_HOME/sbt/boot" "-Dsbt.preloaded=$XDG_CACHE_HOME/sbt/preloaded" "-Dsbt.global.base=$XDG_CACHE_HOME/sbt"