Skip to content

Commit

Permalink
Merge pull request #393 from alexarchambault/escape-pipe-completions
Browse files Browse the repository at this point in the history
Fix '|' escaping in zsh completion output
  • Loading branch information
alexarchambault authored Jun 20, 2022
2 parents 92897e3 + d629a6c commit 4d7dd0e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ trait PlatformCommandsMethods {
private lazy val fs = g.require("fs")
protected def writeCompletions(script: String, dest: String): Unit =
fs.writeFileSync(dest, script)
protected def completeMainHook(args: Array[String]): Unit = ()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package caseapp.core.app

import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
import java.nio.file.{Files, Paths, StandardOpenOption}

trait PlatformCommandsMethods {
protected def writeCompletions(script: String, dest: String): Unit = {
val destPath = Paths.get(dest)
Files.write(destPath, script.getBytes(StandardCharsets.UTF_8))
}
protected def completeMainHook(args: Array[String]): Unit =
Option(System.getenv("CASEAPP_COMPLETION_DEBUG")).foreach { pathStr =>
val path = Paths.get(pathStr)
val output = s"completeMain(${args.toSeq})"
Files.write(path, output.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ trait PlatformCommandsMethods {
val destPath = Paths.get(dest)
Files.write(destPath, script.getBytes(StandardCharsets.UTF_8))
}
protected def completeMainHook(args: Array[String]): Unit = ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ abstract class CommandsEntryPoint extends PlatformCommandsMethods {
RuntimeCommandParser.complete(defaultCommand0, commands, args.toList, index)
}

def completeMain(args: Array[String]): Unit =
def completeMain(args: Array[String]): Unit = {
completeMainHook(args)
args match {
case Array(format, indexStr, userArgs @ _*) =>
val index = indexStr.toInt - 2 // -1 for argv[0], and -1 as indices start at 1
Expand All @@ -87,9 +88,12 @@ abstract class CommandsEntryPoint extends PlatformCommandsMethods {
PlatformUtil.exit(1)
}
case _ =>
System.err.println(s"Usage: $progName $completeCommandName format index ...args...")
System.err.println(
s"Usage: $progName ${completeCommandName.mkString(" ")} format index ...args..."
)
PlatformUtil.exit(1)
}
}

def main(args: Array[String]): Unit = {
val actualArgs = PlatformUtil.arguments(args)
Expand Down
12 changes: 10 additions & 2 deletions core/shared/src/main/scala/caseapp/core/complete/Zsh.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ object Zsh {
if (hash < 0) (hash * -1).toString
else hash.toString
}
private def escape(s: String): String =
s.replace("'", "\\'").replace("`", "\\`").linesIterator.toStream.headOption.getOrElse("")
private def escape(input: String): String =
input
.replace("'", "\\'")
.replace("`", "\\`")
.replace("|", "\\|")
.linesIterator
.take(1)
.toList
.headOption
.getOrElse("")
private def defs(item: CompletionItem): Seq[String] = {
val (options, arguments) = item.values.partition(_.startsWith("-"))
val optionsOutput =
Expand Down

0 comments on commit 4d7dd0e

Please sign in to comment.