-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
53 changes: 53 additions & 0 deletions
53
src/main/scala/com/github/mdr/mash/ns/os/ClipboardFunction.scala
This file contains 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
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 | ||
|
||
} |