From 09feb5e84a44511301bcd7ff7e79ea6e264d68c2 Mon Sep 17 00:00:00 2001 From: Olivier Patry Date: Thu, 13 Jan 2022 13:11:30 +0100 Subject: [PATCH] [Mosaic] Add a PlatformUtil to implement terminal copy to clipboard feature --- gradle/libs.versions.toml | 1 + wordle-compose-mosaic/build.gradle.kts | 3 + .../net/opatry/game/wordle/PlatformUtil.kt | 68 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/PlatformUtil.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 35ef109..a8ac5f5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" diff --git a/wordle-compose-mosaic/build.gradle.kts b/wordle-compose-mosaic/build.gradle.kts index dbd939f..60e4508 100644 --- a/wordle-compose-mosaic/build.gradle.kts +++ b/wordle-compose-mosaic/build.gradle.kts @@ -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")) } diff --git a/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/PlatformUtil.kt b/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/PlatformUtil.kt new file mode 100644 index 0000000..f8ef69c --- /dev/null +++ b/wordle-compose-mosaic/src/main/java/net/opatry/game/wordle/PlatformUtil.kt @@ -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 + } +}