Skip to content

Commit

Permalink
[Mosaic] Add a PlatformUtil to implement terminal copy to clipboard f…
Browse files Browse the repository at this point in the history
…eature
  • Loading branch information
opatry committed Jan 3, 2024
1 parent bc30ee2 commit 09feb5e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ accompanist-insets = "com.google.accompanist:accompanist-insets:0.25.1"
gson = "com.google.code.gson:gson:2.10.1"

jline = "org.jline:jline:3.25.0"
turtle = "com.lordcodes.turtle:turtle:0.9.0"

junit4 = "junit:junit:4.13.2"

Expand Down
3 changes: 3 additions & 0 deletions wordle-compose-mosaic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ dependencies {
implementation(libs.jline) {
because("need to handle terminal keyboard input")
}
implementation(libs.turtle) {
because("need to copy results to clipboard (using `pbcopy`, `xclip`, `clip` or equivalent)")
}
implementation(project(":word-data"))
implementation(project(":game-logic"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2022 Olivier Patry
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.opatry.game.wordle

import com.lordcodes.turtle.shellRun
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

enum class OSName {
UNKNOWN,
WINDOWS,
MAC,
LINUX,
}

val OS: OSName
get() {
val os = System.getProperty("os.name").lowercase()
return when {
os.contains("mac") -> OSName.MAC
os.contains("win") -> OSName.WINDOWS
os.contains("nix") || os.contains("nux") || os.contains("aix") -> OSName.LINUX
else -> OSName.UNKNOWN
}
}

suspend fun String.copyToClipboard(): Boolean {
// TODO check for command availability and find fallbacks if possible
val copyCommand = when (OS) {
OSName.MAC -> "pbcopy"
OSName.WINDOWS -> "clip" // in WSL2 there is clipcopy
OSName.LINUX -> "xclip" // there is also xsel --clipboard --input
else -> null
}
return if (copyCommand != null) {
withContext(Dispatchers.IO) {
try {
// XXXcopy <<< "str"
shellRun("/bin/sh", listOf("-c", "$copyCommand <<< \"${this@copyToClipboard}\""))
true
} catch (e: Exception) {
false
}
}
} else {
false
}
}

0 comments on commit 09feb5e

Please sign in to comment.