Skip to content

Commit

Permalink
os.clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Feb 8, 2017
1 parent 0dafe69 commit b4aed9f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v0.0.5 (..)
-----------

* os.open, os.clipboard
* Support @shortFlag parameter attribute
* Support attributes with arguments; @alias attribute for methods
* os.read/Path.read
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/com/github/mdr/mash/ns/MashRoot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ object MashRoot {
"mkdir" -> CreateDirectoryFunction,
"pwd" -> CurrentDirectoryFunction) ++
(if (SystemUtils.IS_OS_MAC_OSX)
Map("df" -> DiskSpaceFunction,
Map(
"clipboard" -> ClipboardFunction,
"df" -> DiskSpaceFunction,
"open" -> OpenFunction)
else
Map())
Expand Down
53 changes: 53 additions & 0 deletions src/main/scala/com/github/mdr/mash/ns/os/ClipboardFunction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.github.mdr.mash.ns.os

import java.nio.charset.StandardCharsets

import com.github.mdr.mash.evaluator.{ Arguments, ToStringifier }
import com.github.mdr.mash.functions.{ MashFunction, Parameter, ParameterModel }
import com.github.mdr.mash.inference.TypeInferenceStrategy
import com.github.mdr.mash.ns.core.StringClass
import com.github.mdr.mash.runtime._
import org.apache.commons.io.IOUtils

object ClipboardFunction extends MashFunction("os.clipboard") {

object Params {
val Item = Parameter(
nameOpt = Some("item"),
summaryOpt = Some("Item to place on the clipboard. If not provided, the contents of the clipboard are returned."),
defaultValueGeneratorOpt = Some(() MashNull))
}

import Params._

val params = ParameterModel(Seq(Item))

override def apply(arguments: Arguments): MashValue = {
val boundParams = params.validate(arguments)
MashNull.option(boundParams(Item)) match {
case Some(xs: MashList) setClipboard(xs.elements.map(ToStringifier.stringify).mkString("\n"))
case Some(item) setClipboard(ToStringifier.stringify(item))
case None MashString(getClipboard)
}
}

private def setClipboard(contents: String): MashUnit = {
val process = new ProcessBuilder("pbcopy", contents).redirectInput(ProcessBuilder.Redirect.PIPE).start()
IOUtils.write(contents, process.getOutputStream, StandardCharsets.UTF_8)
process.getOutputStream.close()
process.waitFor()
MashUnit
}

private def getClipboard: String = {
val process = new ProcessBuilder("pbpaste").redirectOutput(ProcessBuilder.Redirect.PIPE).start()
val output = IOUtils.toString(process.getInputStream, StandardCharsets.UTF_8)
process.waitFor()
output
}

override def summaryOpt: Option[String] = Some("Interact with the copy/paste clipboard")

override def typeInferenceStrategy: TypeInferenceStrategy = StringClass

}

0 comments on commit b4aed9f

Please sign in to comment.