Skip to content

Commit

Permalink
help: simplify generated code and make extensible (#5120)
Browse files Browse the repository at this point in the history
usage example for extension mechanism:
```
echo 'Help.additionalHelpEntries += (("aa", "bb", "cc"))' > test-help.sc
./joern --import test-help.sc

joern> help
...
┌────────────────┬────────────────┬─────────────────────────┐
│command         │description     │example                  │
├────────────────┼────────────────┼─────────────────────────┤
│aa              │bb              │cc                       │
...
```
  • Loading branch information
mpollmeier authored Nov 19, 2024
1 parent 14adabf commit c115e87
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions console/src/main/scala/io/joern/console/Help.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package io.joern.console

import flatgraph.help.DocFinder.*
import flatgraph.help.Table.AvailableWidthProvider
import flatgraph.help.Table.{AvailableWidthProvider, Row}
import flatgraph.help.{DocFinder, Table}

object Help {

/** allows users to extend the help table with additional entries */
val additionalHelpEntries = Seq.newBuilder[Tuple3[String, String, String]]

def overview(clazz: Class[?])(using AvailableWidthProvider): String = {
val columnNames = List("command", "description", "example")
val rows = DocFinder
.findDocumentedMethodsOf(clazz)
.map { case StepDoc(_, funcName, doc) =>
List(funcName, doc.info, doc.example)
}
.toList ++ List(runRow)

val rows = Seq.newBuilder[Row]
rows += runRow
DocFinder.findDocumentedMethodsOf(clazz).foreach { case StepDoc(_, funcName, doc) =>
rows += List(funcName, doc.info, doc.example)
}
additionalHelpEntries.result().foreach { case (a, b, c) =>
rows += List(a, b, c)
}

val header = formatNoQuotes("""
|
Expand All @@ -27,7 +33,7 @@ object Help {
|
|
|""".stripMargin)
header + "\n" + Table(columnNames, rows.sortBy(_.head)).render
header + "\n" + Table(columnNames, rows.result().sortBy(_.head)).render
}

def format(text: String): String = {
Expand All @@ -45,8 +51,7 @@ object Help {
List("run", "Run analyzer on active CPG", "run.securityprofile")

// Since `run` is generated dynamically, it's not picked up when looking
// through methods via reflection, and therefore, we are adding
// it manually.
// through methods via reflection, and therefore, we are adding it manually.
def runLongHelp: String =
Help.format("""
|
Expand All @@ -60,11 +65,10 @@ object Help {
}
.mkString("\n")

val overview = Help.overview(clazz)
s"""
| class Helper() {
| def run: String = Help.runLongHelp
| override def toString: String = \"\"\"$overview\"\"\"
| override def toString: String = Help.overview(classOf[${clazz.getName}])
|
| $membersCode
| }
Expand Down

0 comments on commit c115e87

Please sign in to comment.