-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add String.copyToClipboard() extension function
This commit also adds the ability to run a list of strings as a command.
- Loading branch information
Showing
2 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
lib-publishing-helpers/src/main/kotlin/lib_publisher_tools/clipboard/Clipboard.kt
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,24 @@ | ||
package lib_publisher_tools.clipboard | ||
|
||
import lib_publisher_tools.process.execute | ||
|
||
fun String.copyToClipboard() { | ||
val escapedText = this.escape(charsToEscape = "\"`$") | ||
|
||
val osName = System.getProperty("os.name").lowercase() | ||
val isMacOs: Boolean = "mac" in osName | ||
val isWindows: Boolean = "win" in osName | ||
when { | ||
isMacOs -> listOf("sh", "-c", "echo \"$escapedText\" | pbcopy") | ||
isWindows -> listOf("cmd", "/C", "echo \"$escapedText\" | clip") //TODO: Test that | ||
else -> listOf("sh", "-c", "echo \"$escapedText\" | xclip -sel clip") //TODO: Test that | ||
}.execute() | ||
} | ||
|
||
private fun String.escape(charsToEscape: String): String = buildString { | ||
append(this@escape) | ||
for (i in lastIndex downTo 0) { | ||
val c = this[i] | ||
if (c in charsToEscape) insert(i, '\\') | ||
} | ||
} |
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