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.FileWithId 20 import bench.flame.diff.interop.withId 21 import bench.flame.diff.ui.printFileTable 22 import com.github.ajalt.clikt.core.CliktCommand 23 import com.github.ajalt.mordant.markdown.Markdown 24 import java.io.File 25 import kotlin.collections.List 26 27 class List : CliktCommand(help = "List all saved trace files.") { runnull28 override fun run() { 29 val savedTracesDir = Paths.savedTracesDir.toFile() 30 val traces = savedTraces(savedTracesDir) 31 32 if (traces.isEmpty()) { 33 echo( 34 Markdown( 35 "No trace files saved. Run the **${Save().commandName}** command" + 36 " to save traces for future comparison." 37 ) 38 ) 39 return 40 } 41 42 printFileTable(traces, savedTracesDir) 43 } 44 45 companion object { 46 /** Returns a list of saved traces sorted by 'most recently modified first' */ savedTracesnull47 internal fun savedTraces(savedTracesDir: File): List<FileWithId> = 48 savedTracesDir 49 .listFiles() 50 .let { it ?: return emptyList() } <lambda>null51 .filter { !it.name.lowercase().endsWith(".json") } <lambda>null52 .sortedBy { -it.lastModified() } 53 .asSequence() 54 .withId() 55 .toList() 56 } 57 } 58