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 1 commit
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
18 changes: 8 additions & 10 deletions at-date-console/src/main/kotlin/com/amerharb/atdate/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ fun main(args: Array<String>) {
if (args.isEmpty()) {
println("enter @...@ to encode, 0x... to decode, C to copy last result or Q to quit")
while (true) {
amerharb marked this conversation as resolved.
Show resolved Hide resolved
mainMenu()
if (!mainMenu()) {
println("Exiting 👋...")
break
}
}
} else {
val arg1 = args[0]
when {
arg1 == "-h" || arg1 == "--help" -> {
printHelp()
exitProcess(0)
}

arg1.startsWith("@") -> {
Expand All @@ -38,22 +40,23 @@ 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)
"c" -> copyLastResultToClipboard()
"q" -> exitProcess()
"q" -> return false
else -> {
println("Invalid input")
}
}
return true
}

fun encodeCommand(input: String) {
Expand Down Expand Up @@ -88,11 +91,6 @@ fun copyLastResultToClipboard() {
println("$lastResult\nhas been copied to clipboard!")
}

fun exitProcess(status: Int = 0) {
println("Exiting with status $status")
kotlin.system.exitProcess(status)
}

fun printEncodingResult(atDate: AtDate) {
println("Hex: ${getHex(atDate)}")
println("Bin: ${getBin(atDate)}")
Expand Down
36 changes: 36 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,6 +1,9 @@
package com.amerharb.atdate

import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.PrintStream
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -282,4 +285,37 @@ class MainTest {
assertEquals(expected, actual)
System.setOut(originalOut)
}

@Test
fun testCopyResult() {
val outContent = ByteArrayOutputStream()
val originalIn = System.`in`
System.setOut(PrintStream(outContent))
val systemInMock = InputStreamMock("0xa0\nc\nq")
System.setIn(systemInMock)
main(emptyArray())
val expected = "@P-tp@"
val actual = getCliboard()
assertEquals(expected, actual)
System.setIn(originalIn)
}
}

class InputStreamMock(input: String) : InputStream() {
private val data: MutableList<Char> = mutableListOf()

init {
this.data.addAll(input.toList())
}
override fun read(): Int {
if (data.isEmpty()) {
return -1
}
return data.removeAt(0).code
}
}

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