Browse files

terminal gui's wooo ahhh

  • Loading branch information...
1 parent 69093ff commit 3848012efb463df66bb04bb92ccff40ed9b283b5 @softprops softprops committed May 1, 2012
Showing with 70 additions and 41 deletions.
  1. +48 −37 app/src/main/scala/discover.scala
  2. +22 −4 app/src/main/scala/giter8.scala
View
85 app/src/main/scala/discover.scala
@@ -1,72 +1,69 @@
package giter8
-sealed trait Term {
- def bold: String => String
- def reversed: String => String
- def reset: String => String
-}
-
-object NoTerm extends Term {
- def bold = { s => s }
- def reversed = { s => s }
- def reset = { s => s }
-}
-
-object ClrTerm extends Term {
- def reversed = Console.REVERSED + _
- def reset = _ + Console.RESET
- def bold = Console.BOLD + _
-}
-
trait Discover { self: Giter8 =>
import dispatch._
import dispatch.liftjson.Js._
import net.liftweb.json.JsonAST._
+ import java.net.UnknownHostException
case class Template(user: String, name: String, desc: String)
val RepoNamed = """(\S+)\.g8""".r
val Limit = 10
- def term: Term = ClrTerm
+ def term: Term = Term.support
+
+ def bold(in: String) = (term.bold andThen term.reset)(in)
- def template(in: String) = (term.bold andThen term.reset)(in)
+ def reversed(in: String) = (term.reversed andThen term.reset)(in)
- def description(in: String) = in
+ def clear = println(term.clear)
- def pager(q: Option[String], page: Int, more: Boolean): Unit =
+ def pager(user: Option[String],
+ name: Option[String],
+ page: Int,
+ more: Boolean): Unit =
Console.readLine(
if(more) ":"
- else (term.reversed andThen term.reset)("(END)")) match {
+ else reversed("(END)")) match {
case "b" | "B" | "u" | "U" if(page > 0) =>
- discover(q, page - 1)
+ clear
+ discover(user, name, page - 1)
case _ if(more) =>
- discover(q, page + 1)
+ clear
+ discover(user, name, page + 1)
case _ => ()
}
def show(ps: Seq[Template], more: Boolean) =
ps.map { t =>
- (template(t.user +"/"+ t.name)
+ (bold(t.user +"/"+ t.name)
+ " \n\t "
- + description(if(t.desc.isEmpty) "-" else t.desc))
+ + (if (t.desc.isEmpty) "-" else t.desc))
} mkString(" ", "\n ","")
- def discover(query: Option[String] = None, page: Int = 0) =
- remoteTemplates(query, page).right.flatMap { templates =>
+ def discover(user: Option[String] = None,
+ name: Option[String] = None,
+ page: Int = 1) =
+ remoteTemplates(user, name, page).right.flatMap { templates =>
templates match {
case Nil =>
- Right("No templates matching %s" format query)
+ Right("No templates matching %s/%s" format(user.getOrElse("*"),
+ name.getOrElse("*")))
+ case template :: Nil =>
+ println(show(templates, false))
+ pager(user, name, page, false)
+ Right("")
case templates =>
println(show(templates.init, templates.size > Limit))
- pager(query, page, templates.size > Limit)
+ pager(user, name, page, templates.size > Limit)
Right("")
}
}
- def remoteTemplates(query: Option[String], page: Int = 0) =
- http x (ls(query, page) ># { j => j }) {
+ def remoteTemplates(user: Option[String], name: Option[String], page: Int = 0) =
+ try { http x (ls(user, name, page) ># { j => j }) {
case (200, _, _, js) =>
Right(for {
JArray(repos) <- js()
@@ -76,10 +73,24 @@ trait Discover { self: Giter8 =>
JField("description", JString(desc)) <- fields
} yield Template(userName, name, desc))
case (404, _, _, _) =>
- Left("Unable to find github repositories like : %s" format query)
+ Left("Unable to find templates like : %s/%s" format(user.getOrElse("*"),
+ name.getOrElse("*")))
+ } } catch {
+ case e: UnknownHostException =>
+ Left("Failed to resolve ls host %s" format e.getMessage)
}
- def ls(query: Option[String], page: Int = 0) = :/("ls.implicit.ly") / "api" / "1" / "g8" <<? Map(
- "page" -> page.toString, "limit" -> (Limit + 1).toString
- ) ++ query.map("query" -> _)
+ def api = :/("ls.implicit.ly") / "api" / "1" / "g8"
+
+ def ls(user: Option[String], name: Option[String], page: Int = 0) =
+ (user, name) match {
+ case (None, None) =>
+ api <<? Map(
+ "page" -> page.toString, "limit" -> (Limit + 1).toString
+ )
+ case _ =>
+ api / user.getOrElse("*") / name.getOrElse("*") <<? Map(
+ "page" -> page.toString, "limit" -> (Limit + 1).toString
+ )
+ }
}
View
26 app/src/main/scala/giter8.scala
@@ -6,7 +6,7 @@ class Giter8 extends xsbti.AppMain
val Repo = """^(\S+)/(\S+?)(?:\.g8)?$""".r
val Branch = """^-(b|-branch)$""".r
- val RemoteTemplates = """^-(l|-list)$""".r
+ val RemoteTemplates = """^-(l|-ls)$""".r
val Auth = """^-(a|-auth)$""".r
java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.SEVERE)
@@ -22,15 +22,17 @@ class Giter8 extends xsbti.AppMain
inspect("%s/%s.g8".format(user, proj), None, params)
case (params, Array(Repo(user, proj), Branch(_), branch)) =>
inspect("%s/%s.g8".format(user, proj), Some(branch), params)
-
case (params, Array(Auth(param), userpass)) =>
userpass.split(":", 2) match {
case Array(user, pass) => auth(user, pass)
case _ =>
Left("-%s requires username and password separated by `:`".format(
param))
}
- case (_, Array(RemoteTemplates(_))) => discover()
+ case (_, Array(RemoteTemplates(_))) =>
+ discover()
+ case (_, Array(RemoteTemplates(_), Repo(user, proj))) =>
+ discover(Some(user), Some(proj))
case _ => Left(usage)
}) fold ({ error =>
System.err.println("\n%s\n" format error)
@@ -66,7 +68,10 @@ class Giter8 extends xsbti.AppMain
| --paramname=paramvalue
| Set given parameter value and bypass interaction.
| -l, --ls
- | List's all
+ | List's all available templates
+ | -p, --publish
+ | Publishes template for others to find with --ls flag
+ |
|
|Apply template and interactively fulfill parameters.
| g8 n8han/giter8
@@ -79,6 +84,19 @@ class Giter8 extends xsbti.AppMain
|
|Acquire Github authorization
| g8 -a login:password
+ |
+ |Lists published templates
+ | g8 -l
+ |
+ |List any users template containing the name unfiltered
+ | g8 -l */unfiltered
+ |
+ |List all of xuwei-k's templates
+ | g8 -l xuwei-k
+ |
+ |Publish your template
+ | g8 -p gh-username/repo-name.g8
+ |
|""".stripMargin format (BuildInfo.version)
}

0 comments on commit 3848012

Please sign in to comment.