Skip to content

Commit a863cff

Browse files
committed
support for listing Sketchbook
1 parent b3fb558 commit a863cff

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

app/src/processing/app/Processing.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.github.ajalt.clikt.parameters.options.flag
1111
import com.github.ajalt.clikt.parameters.options.help
1212
import com.github.ajalt.clikt.parameters.options.option
1313
import processing.app.api.Contributions
14+
import processing.app.api.Sketchbook
1415
import processing.app.ui.Start
1516
import java.io.File
1617
import java.util.prefs.Preferences
@@ -50,7 +51,8 @@ suspend fun main(args: Array<String>){
5051
.subcommands(
5152
LSP(),
5253
LegacyCLI(args),
53-
Contributions()
54+
Contributions(),
55+
Sketchbook()
5456
)
5557
.main(args)
5658
}

app/src/processing/app/api/Contributions.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,7 @@ class Contributions: SuspendingCliktCommand(){
2929
}
3030

3131
class ExamplesList: SuspendingCliktCommand("list") {
32-
@Serializable
33-
data class Sketch(
34-
val type: String = "sketch",
35-
val name: String,
36-
val path: String,
37-
val mode: String = "java",
38-
)
3932

40-
@Serializable
41-
data class Folder(
42-
val type: String = "folder",
43-
val name: String,
44-
val path: String,
45-
val mode: String = "java",
46-
val children: List<Folder> = emptyList(),
47-
val sketches: List<Sketch> = emptyList()
48-
)
4933

5034
val serializer = Json {
5135
prettyPrint = true

app/src/processing/app/api/Sketch.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
package processing.app.api
22

3+
import kotlinx.serialization.Serializable
34
import java.io.File
45

56
class Sketch {
67
companion object{
7-
fun getSketches(file: File): Contributions.ExamplesList.Folder {
8+
@Serializable
9+
data class Sketch(
10+
val type: String = "sketch",
11+
val name: String,
12+
val path: String,
13+
val mode: String = "java",
14+
)
15+
16+
@Serializable
17+
data class Folder(
18+
val type: String = "folder",
19+
val name: String,
20+
val path: String,
21+
val mode: String = "java",
22+
val children: List<Folder> = emptyList(),
23+
val sketches: List<Sketch> = emptyList()
24+
)
25+
26+
fun getSketches(file: File, filter: (File) -> Boolean = { true }): Folder {
827
val name = file.name
9-
val (sketchesFolders, childrenFolders) = file.listFiles().partition { isSketchFolder(it) }
28+
val (sketchesFolders, childrenFolders) = file.listFiles()?.partition { isSketchFolder(it) } ?: return Folder(
29+
name = name,
30+
path = file.absolutePath,
31+
sketches = emptyList(),
32+
children = emptyList()
33+
)
1034

11-
val children = childrenFolders.map { getSketches(it) }
12-
val sketches = sketchesFolders.map { Contributions.ExamplesList.Sketch(name = it.name, path = it.absolutePath) }
13-
return Contributions.ExamplesList.Folder(
35+
val children = childrenFolders.filter(filter) .map { getSketches(it) }
36+
val sketches = sketchesFolders.map { Sketch(name = it.name, path = it.absolutePath) }
37+
return Folder(
1438
name = name,
1539
path = file.absolutePath,
1640
children = children,

app/src/processing/app/api/Sketchbook.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,40 @@ package processing.app.api
33
import com.github.ajalt.clikt.command.SuspendingCliktCommand
44
import com.github.ajalt.clikt.core.Context
55
import com.github.ajalt.clikt.core.subcommands
6-
import processing.app.Base
6+
import kotlinx.serialization.encodeToString
7+
import kotlinx.serialization.json.Json
8+
import processing.app.Platform
9+
import processing.app.Preferences
710
import processing.app.api.Sketch.Companion.getSketches
11+
import java.io.File
812

913
class Sketchbook: SuspendingCliktCommand() {
14+
15+
1016
override fun help(context: Context) = "Manage the sketchbook"
1117
override suspend fun run() {
1218
System.setProperty("java.awt.headless", "true")
1319
}
1420
init {
1521
subcommands(SketchbookList())
1622
}
23+
24+
1725
class SketchbookList: SuspendingCliktCommand("list") {
26+
val serializer = Json {
27+
prettyPrint = true
28+
}
29+
1830
override fun help(context: Context) = "List all sketches"
1931
override suspend fun run() {
20-
val sketchbookFolder = Base.getSketchbookFolder()
32+
Platform.init()
33+
// TODO: Allow the user to change the sketchbook location
34+
// TODO: Currently blocked since `Base.getSketchbookFolder()` is not available in headless mode
35+
val sketchbookFolder = Platform.getDefaultSketchbookFolder()
2136

22-
val sketches = getSketches(sketchbookFolder)
37+
val sketches = getSketches(sketchbookFolder.resolve("sketchbook"))
38+
val json = serializer.encodeToString(listOf(sketches))
39+
println(json)
2340
}
2441
}
2542
}

0 commit comments

Comments
 (0)