Skip to content

Instantly share code, notes, and snippets.

View virasak's full-sized avatar

Virasak Dungsrikaew virasak

View GitHub Profile
@virasak
virasak / CompileMessageLogger.scala
Created July 9, 2010 05:32
error message filter and redirect to error file
trait CompileMessageLogger extends Project {
override def logImpl: Logger = {
val logFile = (info.projectPath / "target" / "error").asFile
new MultiLogger(super.logImpl::new WarnAndErrorLogger(logFile):: Nil)
}
}
@virasak
virasak / tint2rc
Created June 14, 2010 11:35
my simple tint2rc
#---------------------------------------------
# TINT2 CONFIG FILE
#---------------------------------------------
#---------------------------------------------
# BACKGROUND AND BORDER
#---------------------------------------------
rounded = 0
border_width = 0
background_color = #1c1c1c 100
@virasak
virasak / sbt-error-filter
Created April 21, 2010 16:09
error message filter for sbt and redirect to 'error' file. In vim call ':cf target/error' or 'set errorfile=target/error' and then call ':cf'.
#!/bin/bash
sbt "$@" | tee >( awk '
BEGIN {
error_file = "target/error";
}
END {
system("rm " error_file);
}
require 'buildr/jetty'
require 'readline'
define "webapp" do
....
task("jetty"=>[package(:war), jetty.use]) do |task|
jetty.deploy("http://localhost:8080", task.prerequisites.first)
Readline::readline('[Type ENTER to stop Jetty]')
@virasak
virasak / MyServer.scala
Created April 4, 2010 13:39
Run webapp project from webapp directory
import org.mortbay.jetty._
import org.mortbay.jetty.webapp.WebAppContext
object MyServer {
def main(args: Array[String]) = {
val server = new Server(8080)
val webAppContext = new WebAppContext("src/main/webapp", "/")
webAppContext.setConfigurationClasses(Array[String](
"org.mortbay.jetty.webapp.WebInfConfiguration",
"org.mortbay.jetty.webapp.WebXmlConfiguration"
@virasak
virasak / PixelCount.scala
Created March 31, 2010 04:40
Pixel count from image files with preset threshold at 127
if (!args.isEmpty) {
println("Filename\t\tTotal\tBlack\tWhite")
args map to_file filter only_real_file foreach { file =>
val image = javax.imageio.ImageIO.read(file)
val (height, width, total) = (image.getHeight, image.getWidth, image.getHeight*image.getWidth)
val rgbs = for (y <- 0 until height; x <- 0 until width) yield image.getRGB(x,y)
val black = rgbs map to_gray count if_under_threshold
println("%s\t\t%d\t%d\t%d" format (file.getName, total, black, total - black))
}
} else {
@virasak
virasak / WordFreq.scala
Created December 30, 2009 07:22
Remove projection if you don't want to println(dir)
import java.io.{Closeable, File, FileWriter, PrintWriter}
val rootDir = new File("/home/virasak/.vim")
if (!rootDir.exists) throw new IllegalArgumentException(rootDir + " does not exist")
val counts = scala.collection.mutable.Map.empty[String, Int]
for {
dir <- rootDir.listFiles.projection if dir.isDirectory && println("Processing " + dir) == ()
file <- dir.listFiles if file.isFile
line <- scala.io.Source.fromFile(file, "ISO-8859-1").getLines
object Cli {
sealed abstract class EvalResult
object Exit extends EvalResult
case class ReadMore(lines: List[String]) extends EvalResult
case class PrintMe(result: (String => Unit) => Unit) extends EvalResult
def prompt(ps1: String, ps2: String)(eval: List[String] => EvalResult): Unit = {
def run(ps: String, acc: List[String]): Unit = eval(readLine(ps) :: acc) match {
case Exit => ()
case ReadMore(lines) => run(ps2, lines)
@virasak
virasak / server.java
Last active July 8, 2023 04:03
Embedded Jetty
/*
* The web app uses the normal java servlet classes plus Jetty
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@virasak
virasak / SQLiteDialect.java
Created January 29, 2009 07:26
SQLite dialect for Hibernate
/*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*
*/
package org.hibernate.dialect;