Last active
April 7, 2017 00:48
-
-
Save onilton/4a5d13fff66c1c96dcedbb2ec9f6b793 to your computer and use it in GitHub Desktop.
Shell Play
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class StringArrayImprovements(a: Array[String]) { def |[T](op: Array[String] => T) = op(a) } | |
implicit class OptionImprovements(a: Option[String]) { def |(op: Option[String] => String) = op(a) } | |
import sys.process._ | |
case class CustomCommand(cmd: Symbol, var args: List[Symbol] = List.empty, var largs: List[Symbol] = List.empty) { | |
def -(x: Symbol) = this.copy(args = args :+ x) | |
def --(x: Symbol) = this.copy(largs = largs :+ x) | |
def !() = (cmd.name + " " + args.map("-" + _.name).mkString(" ") + " " + largs.map("--" + _.name).mkString(" ")).!! | |
} | |
implicit class SymbolImprovements(a: Symbol) { | |
def -(s: Symbol) = CustomCommand(a, List(s)) | |
def --(s: Symbol) = CustomCommand(a, List.empty ,List(s)) | |
} | |
case class ls(recursive: Boolean, reverse: Boolean) { | |
def apply(path: String = ".") = if (recursive) s"ls -r $path".!!.split("\n") else s"ls $path".!!.split("\n") } | |
object ls { def r = new ls(true, false) ; def apply(d: String = ".") = (new ls(false, false)).apply(d) } | |
// case class grep(ignoreCase: Boolean) { | |
// def apply(p: String = ".") = if (recursive) s"ls -r $path".!!.split("\n") else s"ls $path".!!.split("\n") } | |
// object grep { def r = new ls(true, false) ; def apply(p: String) = (new grep(false, false)).apply(d) } | |
def ls(d: String = ".") = s"ls $d".!!.split("\n") | |
def getOrElse[T](v: T)(a: Option[T]): T = a.getOrElse(v) | |
def grep(p: String)(a: Array[String]) = a.foldLeft(Array.empty[String])((r, i) => if (i.contains(p)) r :+ i else r) | |
def wc(a: Array[String]) = Array(a.size.toString) | |
def sort(invert: Boolean = true)(a: Array[String]) = if (invert) a.sortBy(identity).reverse else a.sortBy(identity) | |
def head(n: Int = 5)(a: Array[String]) = a.take(n) | |
def tail(n: Int = 5)(a: Array[String]) = a.reverse.take(n).reverse | |
def filter[T](f: String => Boolean)(a: Array[String]) = a.filter(f) | |
def map[T](f: String => T)(a: Array[String]) = a.map(f) | |
def headOption(a: Array[String]) = a.headOption | |
ls() | grep("ansible") | grep("e") | wc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment