1 /*
2  * 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.command
17 
18 import bench.flame.diff.config.Paths
19 import bench.flame.diff.interop.log
20 import bench.flame.diff.interop.openFileInOs
21 import bench.flame.diff.interop.withId
22 import bench.flame.diff.ui.promptPickFile
23 import com.github.ajalt.clikt.core.CliktCommand
24 import com.github.ajalt.mordant.markdown.Markdown
25 
26 class Open : CliktCommand(help = "Open a saved diff.") {
runnull27     override fun run() {
28         val diffsDir = Paths.savedDiffsDir.toFile()
29         val diffFiles =
30             diffsDir.walkTopDown().filter { it.name == "index.html" }
31                 .sortedBy { -it.lastModified() }.withId().toList()
32 
33         if (diffFiles.isEmpty()) {
34             echo(
35                 Markdown(
36                     "No diffs saved. Run the **${Diff().commandName}** command to compare traces."
37                 )
38             )
39             return
40         }
41 
42         val pickedDiff = promptPickFile(diffFiles, diffsDir)
43         openFileInOs(pickedDiff)
44         log("Opened '${pickedDiff.canonicalPath}' in the browser.", isStdErr = true)
45     }
46 }
47