From 692cd94ce8d7731fc6670c6cfcaffdcbfcda4bbb Mon Sep 17 00:00:00 2001
From: Orestis <orestis.malaspinas@pm.me>
Date: Tue, 13 Feb 2024 20:59:16 +0100
Subject: [PATCH] made things more functional

---
 NodeApp/src/helpers/AutoCompletionHelper.ts | 33 +++++++++++----------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/NodeApp/src/helpers/AutoCompletionHelper.ts b/NodeApp/src/helpers/AutoCompletionHelper.ts
index f5391fa..2e1f42e 100644
--- a/NodeApp/src/helpers/AutoCompletionHelper.ts
+++ b/NodeApp/src/helpers/AutoCompletionHelper.ts
@@ -43,7 +43,7 @@ function computeDepth(cmd: Command | undefined): number {
 }
 
 // Computes the maximum number of commands until the root is reached
-function computeHeigth(cmd: Command | null): number {
+function computeHeight(cmd: Command | null): number {
     let height = 0
     let tmp = cmd
     while (tmp !== null) {
@@ -124,29 +124,30 @@ function generateCommandChain(cmd: Command | null): string {
     return data.trimEnd()
 }
 
+function hasOptions(cmd: Command): boolean {
+    return cmd.options.length > 0
+}
+
 function optionsToString(cmd: Command): string {
-    if (cmd.options.length == 0) {
-        return ''
-    }
     return cmd.options.filter(opt => !opt.hidden).map(opt => {
-        return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a "--${opt.name()}" -d "${opt.description}"`
+        return `${prefix} ${computeHeight(cmd)} ${generateCommandChain(cmd)}' -a "${opt.short ?? ''} ${opt.long ?? ''}" -d "${opt.description}"`
     }).join('\n').concat('\n')
 }
 
-
 export function writeFishCompletion(root: Command, filename: string) {
     const commands = flatten(root)
 
-    let data = fishFunction
-    // add commands and subcommands
-    commands.forEach(cmd => {
-        data += optionsToString(cmd)
-        if (!isLeaf(cmd)) {
-            data += cmd.commands.map(subCmd => {
-                return `${prefix} ${computeHeigth(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"`
-            }).join('\n').concat('\n')
-        }
-    })
+    const data = fishFunction.concat(
+        // add completions for options
+        commands.filter(cmd => hasOptions(cmd)).map(cmd => optionsToString(cmd)).filter(str => str != '').join('')
+    ).concat(
+        // add completions for commands and subcommands
+        commands.filter(
+            cmd => !isLeaf(cmd)).map(
+                cmd => cmd.commands.map(subCmd => {
+                    return `${prefix} ${computeHeight(cmd)} ${generateCommandChain(cmd)}' -a ${subCmd.name()} -d "${subCmd.description()}"`
+                }).join('\n').concat('\n')).join('')
+    )
 
     writeFileSync(filename, data);
 }
-- 
GitLab