Update some scripts
This commit is contained in:
		
							parent
							
								
									83fb9a083a
								
							
						
					
					
						commit
						697c7e105f
					
				
					 5 changed files with 33 additions and 24 deletions
				
			
		|  | @ -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="<br>"} END {print ""}')" "$(find /usr/share/icons/ -ipath \*48x48/\*appointment-soon.\* | head -n 1)" | ||||
|  |  | |||
|  | @ -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"<b>${msg.from}:</b> ${msg.subject}<br>").mkString | ||||
|   os.proc("notify-send", s"--icon=$icon", "Mails", markup).call() | ||||
| } | ||||
| 
 | ||||
| os.write.over(cacheFile, newMailPaths.mkString("", "\n", "\n")) | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Alexander Gehrke
						Alexander Gehrke