Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy last result to clipboard #21

Merged
merged 25 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'adopt'

- name: Cache Gradle packages
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
Expand Down
6 changes: 6 additions & 0 deletions at-date-console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog at-date-console
<!-- https://keepachangelog.com/en/1.0.0/ -->

## [0.2.0] 2023-09-21
### Added
- add new command `c` to copy the last result to the clipboard.
2 changes: 1 addition & 1 deletion at-date-console/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.amerharb.atdate"
version = "0.1.1"
version = "0.2.0"

dependencies {
implementation(project(":at-date-lib"))
Expand Down
48 changes: 33 additions & 15 deletions at-date-console/src/main/kotlin/com/amerharb/atdate/Main.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.amerharb.atdate

import java.awt.Toolkit
import java.awt.datatransfer.StringSelection

var lastResult: String = ""
fun main(args: Array<String>) {
println("@Date")
println("input: ${args.joinToString(" ")}")
if (args.isEmpty()) {
println("enter @...@ to encode, 0x... to decode or Q to Quit")
while (true) {
mainMenu()
}
println("enter @...@ to encode, 0x... to decode, C to copy last result or Q to quit")
while (mainMenu()) {}
println("Quiting 👋...")
} else {
val arg1 = args[0]
when {
arg1 == "-h" || arg1 == "--help" -> {
printHelp()
exitProcess(0)
}

arg1.startsWith("@") -> {
Expand All @@ -34,26 +36,30 @@ fun main(args: Array<String>) {
}
}

fun mainMenu() {
fun mainMenu(): Boolean {
print(">")
val command = readlnOrNull()
if (command.isNullOrBlank()) {
println("Invalid input")
return
return true
}
when (command[0].lowercase()) {
"@" -> encodeCommand(command)
"0" -> decodeCommand(command)
"q" -> exitProcess()
"c" -> copyLastResultToClipboard()
"q" -> return false
else -> {
println("Invalid input")
}
}
return true
}

fun encodeCommand(input: String) {
try {
printEncodingResult(encode(input))
val result = encode(input)
lastResult = getHex(result)
printEncodingResult(result)
} catch (e: Exception) {
println("Error: ${e.message}")
}
Expand All @@ -62,22 +68,34 @@ fun encodeCommand(input: String) {
fun decodeCommand(input: String) {
try {
val arrayOfUBytes = getByteArrayFromHexString(input)
printDecodingResult(decode(arrayOfUBytes))
val result = decode(arrayOfUBytes)
lastResult = result.getNotation()
printDecodingResult(result)
} catch (e: Exception) {
println("Error: ${e.message}")
}
}

fun exitProcess(status: Int = 0) {
println("Exiting with status $status")
kotlin.system.exitProcess(status)
/** copy previous result to clipboard */
fun copyLastResultToClipboard() {
if (lastResult.isBlank()) {
println("No result to copy")
return
}
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
clipboard.setContents(StringSelection(lastResult), null)
println("$lastResult\nhas been copied to clipboard!")
}

fun printEncodingResult(atDate: AtDate) {
println("Hex: 0x${atDate.getPayload().joinToString("") { it.toString(16).padStart(2, '0') }}")
println("Bin: 0b${atDate.getPayload().joinToString("") { it.toString(2).padStart(8, '0') }}")
println("Hex: ${getHex(atDate)}")
println("Bin: ${getBin(atDate)}")
}

fun getHex(atDate: AtDate) = "0x${atDate.getPayload().joinToString("") { it.toString(16).padStart(2, '0') }}"

fun getBin(atDate: AtDate) = "0b${atDate.getPayload().joinToString("") { it.toString(2).padStart(8, '0') }}"

fun printDecodingResult(atDate: AtDate) {
println("Notation: ${atDate.getNotation()}")
}
Expand Down
50 changes: 50 additions & 0 deletions at-date-console/src/test/kotlin/com/amerharb/atdate/MainTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.amerharb.atdate

import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.PrintStream
import java.util.LinkedList
import java.util.Queue
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -282,4 +289,47 @@ class MainTest {
assertEquals(expected, actual)
System.setOut(originalOut)
}

@Test
@Ignore // Ignored as this test can not work in GitHub Actions
fun testCopyResult() {
val originalInputStream = System.`in`
try {
clearClipboard()
System.setIn(InputStreamMock("0xa0\nc\nq"))
main(emptyArray())
val expected = "@P-tp@"
val actual = getClipboard()
assertEquals(expected, actual)
} finally {
System.setIn(originalInputStream)
}
}
}

class InputStreamMock(input: String) : InputStream() {
private val queue: Queue<Char> = LinkedList()

init {
this.queue.addAll(input.toList())
}

override fun read(): Int {
return this.queue.poll()?.code ?: -1
}
}

fun getClipboard(): String {
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
return clipboard.getData(DataFlavor.stringFlavor) as String
}

fun clearClipboard() {
try {
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
clipboard.setContents(StringSelection(""), null)
} catch (e: Exception) {
println("Error: ${e.message}")
System.err.println(e.message)
}
}