Skip to content

Instantly share code, notes, and snippets.

@koduki
Created December 27, 2008 22:42
Show Gist options
  • Save koduki/40327 to your computer and use it in GitHub Desktop.
Save koduki/40327 to your computer and use it in GitHub Desktop.
import java.io._
import java.net._
import scala.io._
import scala.xml._
object Shell {
val sjis = "Shift_JIS"
val utf8 = "UTF8"
val jis = "jis"
val eucjp = "EUC_JP"
implicit def toRichSource(src:Source) = new {
def read = src.getLines.foldLeft(""){(r, line) => r + line + "\n"}
}
implicit def strAddtoURL(s:String) = new { def toURL = new URL(s) }
implicit def regexAddmatchRegex(r:scala.util.matching.Regex) = new { def =~(s:String) = s.matches(r.toString)}
implicit def strAddmatchRegex(s1:String) = new {
def =~(s2:String) = s2.matches(s1.toString)
def =~(r:scala.util.matching.Regex) = s1.matches(r.toString)
def encode(charset:String) = new String(s1.getBytes("iso-8859-1"), charset)
}
def open(url:URL):Source = {
Source.fromURL( url )
}
implicit val path = "./"
def ls(implicit path:String) = {
val f = new File(path)
if (f.isFile) List(f)
else if(f.isDirectory) f.listFiles.toList
else List()
}
def exec(cmd:String)(implicit filter:(String) => String) = {
val process = Runtime.getRuntime().exec(cmd)
val in = process.getInputStream()
val br = new BufferedReader(new InputStreamReader(in))
val sw = new StringWriter()
val pw = new PrintWriter(sw)
var s = br.readLine
while(s != null){
pw.println( filter(s) )
s = br.readLine
}
sw.toString
}
def main(args : Array[String]) : Unit = {
println( exec("ls -l /"){ x => "\\\\ " + x.split(" ")(0) + " //" } )
println( exec("ls -l /") )
// val xml = XML.loadString(html.encode(sjis).replaceAll("FIXED HREF","href").replaceAll("<!--.*",""))
//val url = xml \\ "ref" \ "@href" text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment