Skip to content

Commit 98e98b6

Browse files
feat: Added support for fish CLI (#3104)
1 parent 4119040 commit 98e98b6

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

modules/cli/src/main/scala/scala/cli/commands/installcompletions/InstallCompletions.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala.cli.commands.installcompletions
22

33
import caseapp.*
4-
import caseapp.core.complete.{Bash, Zsh}
4+
import caseapp.core.complete.{Bash, Fish, Zsh}
55
import caseapp.core.help.HelpFormat
66

77
import java.io.File
@@ -47,6 +47,7 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
4747
)
4848
System.err.println(s"$name install completions --shell zsh")
4949
System.err.println(s"$name install completions --shell bash")
50+
System.err.println(s"$name install completions --shell fish")
5051
sys.exit(1)
5152
}
5253
}
@@ -76,6 +77,10 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
7677
"compinit"
7778
).map(_ + System.lineSeparator()).mkString
7879
(script, defaultRcFile)
80+
case Fish.id | "fish" =>
81+
val script = Fish.script(name)
82+
val defaultRcFile = os.home / ".config" / "fish" / "config.fish"
83+
(script, defaultRcFile)
7984
case _ =>
8085
System.err.println(s"Unrecognized or unsupported shell: $format")
8186
sys.exit(1)
@@ -118,6 +123,7 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
118123
EnvVar.Misc.shell.valueOpt.map(_.split("[\\/]+").last).map {
119124
case "bash" => Bash.id
120125
case "zsh" => Zsh.id
126+
case "fish" => Fish.id
121127
case other => other
122128
}
123129
}

modules/cli/src/main/scala/scala/cli/commands/uninstallcompletions/UninstallCompletions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ object UninstallCompletions extends ScalaCommand[UninstallCompletionsOptions] {
3737
.getOrElse(os.home)
3838
val rcFiles = options.shared.rcFile.map(file => Seq(os.Path(file, os.pwd))).getOrElse(Seq(
3939
zDotDir / ".zshrc",
40-
os.home / ".bashrc"
40+
os.home / ".bashrc",
41+
os.home / ".config" / "fish" / "config.fish"
4142
)).filter(os.exists(_))
4243

4344
rcFiles.foreach { rcFile =>

modules/integration/src/test/scala/scala/cli/integration/InstallAndUninstallCompletionsTests.scala

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,41 @@ import scala.util.Properties
88
class InstallAndUninstallCompletionsTests extends ScalaCliSuite {
99
val zshRcFile: String = ".zshrc"
1010
val bashRcFile: String = ".bashrc"
11+
val fishRcFile: String = "config.fish"
1112
val rcContent: String = s"""
1213
|dummy line
1314
|dummy line""".stripMargin
1415
val testInputs: TestInputs = TestInputs(
15-
os.rel / zshRcFile -> rcContent,
16-
os.rel / bashRcFile -> rcContent
16+
os.rel / zshRcFile -> rcContent,
17+
os.rel / bashRcFile -> rcContent,
18+
os.rel / ".config" / "fish" / fishRcFile -> rcContent
1719
)
1820

1921
def runInstallAndUninstallCompletions(): Unit = {
2022
testInputs.fromRoot { root =>
2123
val zshRcPath = root / zshRcFile
2224
val bashRcPath = root / bashRcFile
25+
val fishRcPath = root / ".config" / "fish" / fishRcFile
2326
// install completions to the dummy rc files
2427
os.proc(TestUtil.cli, "install-completions", "--rc-file", zshRcPath, "--shell", "zsh").call(
2528
cwd = root
2629
)
2730
os.proc(TestUtil.cli, "install-completions", "--rc-file", bashRcPath, "--shell", "bash").call(
2831
cwd = root
2932
)
33+
os.proc(TestUtil.cli, "install-completions", "--rc-file", fishRcPath, "--shell", "fish").call(
34+
cwd = root
35+
)
3036
expect(os.read(bashRcPath).contains(bashRcScript))
3137
expect(os.read(zshRcPath).contains(zshRcScript))
38+
expect(os.read(fishRcPath).contains(fishRcScript))
3239
// uninstall completions from the dummy rc files
3340
os.proc(TestUtil.cli, "uninstall-completions", "--rc-file", zshRcPath).call(cwd = root)
3441
os.proc(TestUtil.cli, "uninstall-completions", "--rc-file", bashRcPath).call(cwd = root)
42+
os.proc(TestUtil.cli, "uninstall-completions", "--rc-file", fishRcPath).call(cwd = root)
3543
expect(os.read(zshRcPath) == rcContent)
3644
expect(os.read(bashRcPath) == rcContent)
45+
expect(os.read(fishRcPath) == rcContent)
3746
}
3847
}
3948

@@ -57,6 +66,15 @@ class InstallAndUninstallCompletionsTests extends ScalaCliSuite {
5766
addTags(script)
5867
}
5968

69+
lazy val fishRcScript: String = {
70+
val progName = "scala-cli"
71+
val script =
72+
s"""
73+
complete $progName -a '($progName complete fish-v1 (math 1 + (count (__fish_print_cmd_args))) (__fish_print_cmd_args))'
74+
|""".stripMargin
75+
addTags(script)
76+
}
77+
6078
lazy val zshRcScript: String = {
6179
val projDirs = ProjectDirectories.from(null, null, "ScalaCli")
6280
val dir = os.Path(projDirs.dataLocalDir, TestUtil.pwd) / "completions" / "zsh"

website/docs/_advanced_install.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,11 @@ Alternatively, you can directly download it from the Maven repository [here](htt
447447
values={[
448448
{label: 'Bash', value: 'bash'},
449449
{label: 'zsh', value: 'zsh'},
450+
{label: 'fish', value: 'fish'},
450451
]}>
451452
<TabItem value="bash"></TabItem>
452453
<TabItem value="zsh"></TabItem>
454+
<TabItem value="fish"></TabItem>
453455
</Tabs>
454456
</SectionAbout>
455457

@@ -463,6 +465,7 @@ Try the completions with
463465
values={[
464466
{label: 'Bash', value: 'bash'},
465467
{label: 'zsh', value: 'zsh'},
468+
{label: 'fish', value: 'fish'},
466469
]}>
467470
<TabItem value="bash">
468471

@@ -479,6 +482,15 @@ eval "$(scala-cli install completions --env --shell zsh)"
479482
scala-cli --<TAB>
480483
```
481484

485+
</TabItem>
486+
<TabItem value="fish">
487+
488+
```
489+
eval "$(scala-cli install completions --env --shell fish)"
490+
fish
491+
scala-cli --<TAB>
492+
```
493+
482494
</TabItem>
483495
</Tabs>
484496

@@ -493,6 +505,7 @@ with `--shell`
493505
values={[
494506
{label: 'Bash', value: 'bash'},
495507
{label: 'zsh', value: 'zsh'},
508+
{label: 'fish', value: 'fish'},
496509
]}>
497510
<TabItem value="bash">
498511

@@ -507,6 +520,13 @@ scala-cli install completions --shell bash
507520
scala-cli install completions --shell zsh
508521
```
509522

523+
</TabItem>
524+
<TabItem value="fish">
525+
526+
```bash
527+
scala-cli install completions --shell fish
528+
```
529+
510530
</TabItem>
511531
</Tabs>
512532
</div></div>

0 commit comments

Comments
 (0)