1 /*
<lambda>null2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package bench.flame.diff.ui
17
18 import bench.flame.diff.interop.FileWithId
19 import bench.flame.diff.interop.exitProcessWithError
20 import bench.flame.diff.interop.file
21 import bench.flame.diff.interop.id
22 import bench.flame.diff.interop.withId
23 import com.github.ajalt.clikt.core.CliktCommand
24 import com.github.ajalt.clikt.core.terminal
25 import com.github.ajalt.mordant.rendering.Whitespace
26 import com.github.ajalt.mordant.table.table
27 import com.github.ajalt.mordant.terminal.ConversionResult
28 import com.github.ajalt.mordant.terminal.YesNoPrompt
29 import java.io.File
30 import java.text.SimpleDateFormat
31 import java.util.Date
32
33 internal fun CliktCommand.printFileTable(files: List<FileWithId>, trimBaseDir: File?) {
34 val trimPrefix: String = if (trimBaseDir == null) "" else "${trimBaseDir.canonicalPath}/"
35 terminal.println(table {
36 whitespace = Whitespace.PRE_WRAP
37 header { row("Id", "Name", "Last Modified") }
38 body {
39 files.map {
40 val lastModified = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
41 .format(Date(it.file.lastModified()))
42 row(it.id, it.file.canonicalPath.removePrefix(trimPrefix), lastModified)
43 }
44 }
45 })
46 }
47
promptPickFilenull48 internal fun CliktCommand.promptPickFile(candidates: List<FileWithId>, trimBaseDir: File?): File {
49 printFileTable(candidates, trimBaseDir)
50 return terminal.prompt("Choose a file by id") {
51 val number = it.toIntOrNull()
52 val min = candidates.minOf { f -> f.id }
53 val max = candidates.maxOf { f -> f.id }
54 when {
55 number == null || candidates.none { f -> f.id == number } ->
56 ConversionResult.Invalid("Choose a number between $min and $max")
57 else ->
58 ConversionResult.Valid(candidates.single { f -> f.id == number }.file)
59 }
60 }!!
61 }
62
promptProvideFilenull63 internal fun CliktCommand.promptProvideFile(
64 prompt: String,
65 includePattern: String = ".*",
66 excludePattern: String? = null,
67 srcDir: File? = null,
68 defaultSrcDir: File? = null
69 ): File {
70 check(srcDir == null || srcDir.isDirectory)
71 check(defaultSrcDir == null || defaultSrcDir.isDirectory)
72 val baseDir = run {
73 val src = srcDir ?: terminal.prompt(prompt, defaultSrcDir) {
74 when {
75 it.isBlank() || !File(it).exists() -> ConversionResult.Invalid("Invalid value")
76 else -> ConversionResult.Valid(File(it))
77 }
78 }!!
79 if (src.isFile) return src
80 src
81 }
82
83 check(baseDir.isDirectory)
84 echo("Looking for files in '${baseDir.absolutePath}' matching '$includePattern'...")
85 val candidates = baseDir.walkTopDown()
86 .filter { it.isFile }
87 .filter { it.name.matches(Regex(includePattern)) }
88 .filter { if (excludePattern == null) true else !it.name.matches(Regex(excludePattern)) }
89 .sortedBy { -it.lastModified() }
90 .withId()
91 .toList()
92
93 if (candidates.isEmpty()) exitProcessWithError(
94 "No files matching '$includePattern'" +
95 (if (excludePattern == null) "" else " (and excluding $excludePattern)") +
96 " in '$baseDir'"
97 )
98 return promptPickFile(candidates, baseDir)
99 }
100
promptOverwriteFilenull101 internal fun CliktCommand.promptOverwriteFile(file: File): Boolean = YesNoPrompt(
102 "Overwrite existing file '${file.absolutePath}'",
103 terminal
104 ).ask()!!
105