Exemplo n.º 1
0
        public void PrintUsages(ICommand cmd, IBaseWorker w, IList <string> remainArgs)
        {
            if (_quiteMode)
            {
                return;
            }

            // ReSharper disable once IdentifierTypo
            var cmds = cmd.backtraceTitles;

            if (string.IsNullOrWhiteSpace(cmds))
            {
                cmds = " [Commands]";
            }
            else if (cmd.SubCommands.Count > 0)
            {
                cmds += " [Sub-Commands]";
            }

            var tails = "[Tail Args]|[Options]|[Parent/Global Options]";

            if (!string.IsNullOrWhiteSpace(cmd.TailArgs))
            {
                tails = cmd.TailArgs;
            }

            oln("");
            oln("Usages:");
            oln($"    {_root?.AppInfo.AppName} {cmds} {tails}");
        }
Exemplo n.º 2
0
        public void ShowHelpScreen(IBaseWorker w, IList <string> remainArgs)
        {
            var commandLines = new SortedDictionary <string, List <TwoString> >();
            var optionLines  = new SortedDictionary <int, CmdFlagLines>();
            // var writer = ColorifyEnabler.Colorify; // Console.Out;
            var command = w.ParsedCommand ?? w.RootCommand;

            tabStopCalculated = w.TabStop;
            const bool noBacktrace = false;

            w.Walk(command,
                   commandsWatcherBuilder(w, commandLines, tabStopCalculated, noBacktrace),
                   flagsWatcherBuilder(w, optionLines, tabStopCalculated, noBacktrace));

            Painter.Setup(command, w, remainArgs);
            Painter.PrintPrologue(command, w, remainArgs);
            Painter.PrintPreface(command, w, remainArgs);
            Painter.PrintHeadLines(command, w, false, remainArgs);

            Painter.PrintUsages(command, w, remainArgs);
            Painter.PrintExamples(command, w, remainArgs);

            // ShowIt(command, commandLines, optionLines, writer, tabStop);
            Painter.PrintCommandsAndOptions(command, commandLines, optionLines,
                                            tabStopCalculated, false, w, remainArgs);

            Painter.PrintTailLines(command, w, remainArgs);
            Painter.PrintEpilogue(command, w, remainArgs);
            // writer.WriteLine("");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Terminates the workers.
        /// </summary>
        private void TerminateWorkers()
        {
            var workers = (IBaseWorker[] )null;

            lock (_workers)
            {
                workers = new IBaseWorker[_workers.Count];
                _workers.CopyTo(workers);
            }

            foreach (var worker in workers)
            {
                (worker as BaseWorker).Terminate();
            }

            foreach (var worker in workers)
            {
                TerminateWorker(worker);
            }

            while (true)
            {
                lock (_workers)
                {
                    if (_workers.Count == 0)
                    {
                        break;
                    }
                }

                Thread.Sleep(100);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Removes the worker.
 /// </summary>
 /// <param name="worker">The worker.</param>
 private void RemoveWorker(IBaseWorker worker)
 {
     lock (_workers)
     {
         _workers.Remove(worker);
     }
 }
Exemplo n.º 5
0
        // ReSharper disable once InconsistentNaming
        private static void listAction(IBaseWorker w, IBaseOpt cmd, IEnumerable <string> args)
        {
            var enumerable = args.ToList();
            var mdxFile    = enumerable.ElementAtOrDefault(0);
            var word       = enumerable.ElementAtOrDefault(1);

            if (string.IsNullOrWhiteSpace(mdxFile))
            {
                w.ShowHelpScreen(w, enumerable);
                // w.ErrorPrint("no valid mdx file specified.");
                return;
            }

            Console.WriteLine($"{mdxFile} / {word}");
            var mdx = new MDictLoader(mdxFile)
            {
                PreloadAll = true
            };

            if (!mdx.Process())
            {
                // w.ErrorPrint($"CANNOT load and process the mdx file: {mdxFile}.");
                return;
            }

            Console.WriteLine($"Header: \n\n{mdx.DictHeader}\n\nIndex: \n\n{mdx.DictIndex}\n\n");
        }
Exemplo n.º 6
0
        public void PrintExamples(ICommand cmd, IBaseWorker w, IList <string> remainArgs)
        {
            if (_quiteMode)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(cmd.DescriptionLong))
            {
                oln("\nDescription:");
                olnIndent(cmd.DescriptionLong, ColorDesc, 4);
            }
            else if (!string.IsNullOrWhiteSpace(cmd.Description))
            {
                oln("\nDescription:");
                olnIndent(cmd.Description, ColorDesc, 4);
            }

            // ReSharper disable once InvertIf
            if (!string.IsNullOrWhiteSpace(cmd.Examples))
            {
                oln("");
                oln("Examples:");
                olnIndent(cmd.Examples, ColorDesc, 4);
            }
        }
Exemplo n.º 7
0
        // ReSharper disable once InconsistentNaming
        private static void lookupAction(IBaseWorker w, IBaseOpt cmd, IEnumerable <string> args)
        {
            var enumerable = args.ToList();
            var mdxFile    = enumerable.ElementAtOrDefault(0);
            var word       = enumerable.ElementAtOrDefault(1);

            if (string.IsNullOrWhiteSpace(mdxFile))
            {
                w.ShowHelpScreen(w, enumerable);
                // w.ErrorPrint("no valid mdx file specified.");
                return;
            }

            var mdx = new MDictLoader(mdxFile);

            if (!mdx.Process())
            {
                // w.ErrorPrint($"CANNOT load and process the mdx file: {mdxFile}.");
                return;
            }

            Console.WriteLine($"<!--\n\nHeader: \n\n{mdx.DictHeader}\n\nIndex: \n\n{mdx.DictIndex}\n\n-->\n");

            if (!string.IsNullOrWhiteSpace(word))
            {
                // Console.WriteLine($"Lookup for word '{word}'...");
                // Console.WriteLine(word);
                var s = mdx.Query(word);
                Console.WriteLine(s);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Spawns the worker.
        /// </summary>
        /// <param name="worker">The worker.</param>
        /// <param name="waitmsec">The waitmsec.</param>
        /// <returns></returns>
        protected IBaseWorker SpawnWorker(IBaseWorker worker, int waitmsec = 0)
        {
            WorkPackage.WorkManager.SpawnWorker(worker);
            WaitForWorkerOnState(worker, BaseThreadState.Started, waitmsec);

            return(worker);
        }
Exemplo n.º 9
0
        void IAction.Invoke(IBaseWorker w, IEnumerable <string> remainsArgs)
        {
            //     foreach (var filename in remainsArgs)
            //     {
            //         if (string.IsNullOrEmpty(filename)) continue;
            //
            //         w.log.Information($"loading {filename} ...");
            //         // using var l = new MDictLoader(filename);
            //         try
            //         {
            //             // l.Process();
            //             // Console.WriteLine($"header: {l.DictHeader}");
            //         }
            //         catch (Exception ex)
            //         {
            //             Debug.WriteLine(ex.ToString());
            //             // throw;
            //         }
            //         finally
            //         {
            //             w.ParsedCount++;
            //             // w.log.Information($"#{w.ParsedCount} parsed.");
            //
            //             // l.Dispose();
            //         }
            //     }
            //
            //     if (w.ParsedCount == 0)
            //         w.log.Warning("Nothing to parsed.");

            w.ShowHelpScreen(w, remainsArgs.ToArray());
        }
Exemplo n.º 10
0
        public void ShowBuildInfoScreen(IBaseWorker w, IList <string> remainArgs)
        {
            var command = w.ParsedCommand ?? w.RootCommand;

            tabStopCalculated = w.TabStop;

            Painter.Setup(command, w, remainArgs);
            if (w.AppQuietMode)
            {
                Painter.PrintBuildInfo(command, tabStopCalculated, w, remainArgs);
                return;
            }

            Painter.PrintPrologue(command, w, remainArgs);
            Painter.PrintPreface(command, w, remainArgs);
            Painter.PrintHeadLines(command, w, true, remainArgs);

            // Painter.PrintUsages(command, w, remainArgs);
            // Painter.PrintExamples(command, w, remainArgs);

            Painter.PrintBuildInfo(command, tabStopCalculated, w, remainArgs);

            // Painter.PrintTailLines(command, w, remainArgs);
            Painter.PrintEpilogue(command, w, remainArgs);
            // writer.WriteLine("");
        }
Exemplo n.º 11
0
 /// <summary>
 /// Adds the worker.
 /// </summary>
 /// <param name="worker">The worker.</param>
 private void AddWorker(IBaseWorker worker)
 {
     lock (_workers)
     {
         _workers.Add(worker);
     }
 }
Exemplo n.º 12
0
 public void PrintEpilogue(ICommand cmd, IBaseWorker w, IList <string> remainArgs)
 {
     if (_quiteMode)
     {
         return;
     }
     oln("");
 }
Exemplo n.º 13
0
        internal Func <ICommand, ICommand, int, bool>?commandsWatcherBuilder(IBaseWorker w,
                                                                             IDictionary <string, List <TwoString> > commandLines,
                                                                             int tabStop, bool noBacktrace)
        {
            return((owner, cmd, level) =>
            {
                if (level > 0)
                {
                    return true;
                }
                if (cmd.Hidden)
                {
                    return true;
                }

                var sb = new StringBuilder("  ");
                if (!string.IsNullOrWhiteSpace(cmd.Short))
                {
                    sb.Append($"{cmd.Short}, ");
                }
                if (!string.IsNullOrWhiteSpace(cmd.Long))
                {
                    sb.Append($"{cmd.Long}");
                }
                if (cmd.Aliases.Length > 0)
                {
                    sb.Append(", ").AppendJoin(',', cmd.Aliases);
                }

                var sb2 = new StringBuilder();
                if (!string.IsNullOrWhiteSpace(cmd.Description))
                {
                    sb2.Append(cmd.Description);
                }
                else if (!string.IsNullOrWhiteSpace(cmd.DescriptionLong))
                {
                    sb2.Append(cmd.DescriptionLong);
                }

                if (!commandLines.ContainsKey(cmd.Group))
                {
                    commandLines.TryAdd(cmd.Group, new List <TwoString>());
                }

                if (sb.Length >= tabStopCalculated)
                {
                    tabStopCalculated = sb.Length + 1;
                }
                commandLines[cmd.Group].Add(new TwoString
                {
                    Level = level, Part1 = sb.ToString(), Part2 = sb2.ToString()
                });

                return true;
            });
        }
Exemplo n.º 14
0
        public void Setup(ICommand cmd, IBaseWorker w, IList <string> remainArgs)
        {
            // Console.WriteLine("Setup...");
            // _quiteMode = Parse(Cmdr.Instance.Store.Get("quiet").ToString());
            _quiteMode = Cmdr.Instance.Store.GetAs("quiet", _quiteMode);
            _root      = cmd.FindRoot();

            // int qi = Cmdr.Instance.Store.GetAs("quiet", 0);
            // Console.WriteLine($"qi={qi}");
        }
Exemplo n.º 15
0
 public void Execute(IBaseWorker w, IBaseOpt cmd, IEnumerable <string> remainArgs)
 {
     // throw new System.NotImplementedException();
     Console.WriteLine(
         value:
         $"Hit: {cmd}, Remains: {remainArgs}. Count: {Cmdr.Instance.Store.GetAs<int>(key: "count")}");
     // for (var i = 0; i < Count; i++)
     // {
     //     // Prompt.GetPassword("Enter your password: ");
     // }
 }
Exemplo n.º 16
0
        public void PrintTailLines(ICommand cmd, IBaseWorker w, IList <string> remainArgs)
        {
            if (_quiteMode)
            {
                return;
            }

            oln("", ColorDesc);
            oln("Type '-h'/'-?' or '--help' to get command help screen.", ColorDesc);
            oln("More: '-D'/'--debug', '-v'/'--verbose', '-V'/'--version', '-#'/'--build-info'...", ColorDesc);
        }
Exemplo n.º 17
0
 /// <summary>
 /// Joins the specified worker.
 /// </summary>
 /// <param name="worker">The worker.</param>
 /// <param name="waitmsec">The waitmsec.</param>
 public void Join(IBaseWorker worker, int waitmsec)
 {
     if (waitmsec != 0)
     {
         long sleep = waitmsec;
         while ((sleep > 0 || waitmsec == Timeout.Infinite) && !_terminated && worker.ActiveThread != null)
         {
             PassiveSleep(100);
             sleep -= 100;
         }
     }
 }
Exemplo n.º 18
0
        public void PrintHeadLines(ICommand cmd, IBaseWorker w, bool singleLine, IList <string> remainArgs)
        {
            if (_quiteMode)
            {
                return;
            }

            var author = _root?.AppInfo.Author;

            if (string.IsNullOrWhiteSpace(author))
            {
                author = "Freeman";
            }

            oln($"{_root?.AppInfo.AppName} - {_root?.AppInfo.AppVersion} - {author}");

            if (singleLine)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(_root?.AppInfo.Copyright))
            {
                olnIndent(_root?.AppInfo.Copyright, ColorDesc, 4);
            }

            if (!string.IsNullOrWhiteSpace(_root?.Description))
            {
                // oln("\nDescription:");
                oln("");
                olnIndent(_root?.Description, ColorDesc, 4);
            }

            // ReSharper disable once InvertIf
            if (Cmdr.Instance.Store.GetAs <bool>("verbose"))
            {
                if (!string.IsNullOrWhiteSpace(_root?.DescriptionLong))
                {
                    oln("");
                    olnIndent(_root?.DescriptionLong, ColorDesc, 4);
                }

                // ReSharper disable once InvertIf
                if (!string.IsNullOrWhiteSpace(_root?.Examples))
                {
                    oln("");
                    oln("Examples:");
                    olnIndent(_root?.Examples, ColorDesc, 4);
                }
            }
        }
Exemplo n.º 19
0
 private void buildParentFlags(IBaseWorker w, ICommand?oo,
                               IDictionary <int, CmdFlagLines> optionLines,
                               int tabStop, bool noBacktrace)
 {
     // var oo = owner.Owner;
     while (oo != null && !ReferenceEquals(oo, oo.Owner))
     {
         w.Walk(oo,
                (o, c, l) => true,
                flagsWatcherBuilder(w, optionLines, tabStop, noBacktrace)
                );
         oo = oo.Owner;
     }
 }
        public SpeechDisordersDetector(
            IAudioService audioService,
            IFileWorkerService fileWorkerService,
            IFeatureExtractorService featureExtractorService,
            INeuralNetworkService neuralNetworkService,
            IBaseWorker baseWorker)
        {
            _audioService            = audioService;
            _fileWorkerService       = fileWorkerService;
            _featureExtractorService = featureExtractorService;
            _neuralNetworkService    = neuralNetworkService;
            _baseWorker = baseWorker;

            InitializeComponent();
        }
Exemplo n.º 21
0
 /// <summary>
 /// Disposes the worker.
 /// </summary>
 /// <param name="worker">The worker.</param>
 private void DisposeWorker(IBaseWorker worker)
 {
     try
     {
         if (worker != null)
         {
             worker.Dispose();
         }
     }
     catch (Exception ex)
     {
         var eMsg = string.Format("An unhandled exception was detected disposing Worker '{0}'", worker.Name);
         Notification.SendException(new WorkManagerException(eMsg, ex), true);
     }
 }
Exemplo n.º 22
0
        public void Invoke(IBaseWorker w, IEnumerable <string> remainsArgs)
        {
            var count = 0;

            foreach (var filename in remainsArgs)
            {
                count++;
                if (string.IsNullOrEmpty(filename))
                {
                    continue;
                }

                w.log.logInfo($"loading {filename} ...");
                using var l = new MDictLoader(filename);
                try
                {
                    l.Process();
                    Console.WriteLine($"header: {l.DictHeader}");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    // throw;
                }
                finally
                {
                    w.ParsedCount++;
                    // w.log.Information($"#{w.ParsedCount} parsed.");

                    // l.Dispose();
                }
            }

            if (count == 0)
            {
                w.ShowHelpScreen(w, remainsArgs.ToArray());
                return;
            }

            if (w.ParsedCount == 0)
            {
                w.log.logWarning(null, "Nothing to parsed.");
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Waits the state of for worker on.
        /// </summary>
        /// <param name="worker">The worker.</param>
        /// <param name="state">The state.</param>
        /// <param name="waitmsec">The waitmsec.</param>
        /// <returns></returns>
        protected bool WaitForWorkerOnState(IBaseWorker worker, BaseThreadState state, int waitmsec)
        {
            if (waitmsec != 0)
            {
                long sleep = waitmsec;
                while ((sleep > 0 || waitmsec == Timeout.Infinite) && !_terminated)
                {
                    if (worker.ThreadState == state)
                    {
                        break;
                    }

                    PassiveSleep(100);
                    sleep -= 100;
                }
            }

            return(ThreadState == state);
        }
Exemplo n.º 24
0
        public void PrintDumpForDebug(ICommand cmd, IBaseWorker w, int tabStop, bool hitOnly = true,
                                      bool enabled = false)
        {
            if (!enabled)
            {
                return;
            }

            if (Util.GetEnvValueBool("CMDR_DUMP_NO_STORE") == false)
            {
                oln("Dump the Store:");
                var store = Cmdr.Instance.Store;
                store.Dump(o);
            }

            if (Util.GetEnvValueBool("CMDR_DUMP_NO_HIT") == false)
            {
                oln("\n\nDump the Flags (Hit only):");
                dumpValues(w.RootCommand, w, tabStop, hitOnly);
                oln(string.Empty);
            }
        }
Exemplo n.º 25
0
 public void OnSetHandler(IBaseWorker w, object newValue, object oldValue)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 26
0
 public void PostInvoke(IBaseWorker w, IEnumerable <string> remainsArgs)
 {
     Console.WriteLine($"{w}: {remainsArgs}");
 }
Exemplo n.º 27
0
 /// <summary>
 /// Terminates the worker.
 /// </summary>
 /// <param name="worker">The worker.</param>
 internal void TerminateWorker(IBaseWorker worker)
 {
     _messageManager.SendMessage(MessageManagerId.TerminateWorker, worker);
 }
Exemplo n.º 28
0
 /// <summary>
 /// Spawns the worker.
 /// </summary>
 /// <param name="worker">The worker.</param>
 /// <returns></returns>
 internal IBaseWorker SpawnWorker(IBaseWorker worker)
 {
     _messageManager.SendMessage(MessageManagerId.SpawnWorker, worker);
     return(worker);
 }
Exemplo n.º 29
0
 private static void ExternalConfigLoader(IBaseWorker w, IRootCommand root)
 {
     // throw new NotImplementedException();
 }
Exemplo n.º 30
0
        public void PrintCommandsAndOptions(ICommand cmd,
                                            SortedDictionary <string, List <TwoString> > commandLines,
                                            SortedDictionary <int, CmdFlagLines> optionLines, // Format writer,
                                            int tabStop, bool treeMode, IBaseWorker w, IList <string> remainArgs)
        {
            if (_quiteMode)
            {
                return;
            }

            if (treeMode)
            {
                var title = cmd.IsRoot ? "-ROOT-" : cmd.backtraceTitles;
                if (commandLines.Count > 0)
                {
                    oln($"\nCommands Tree For '{title}':");
                    ShowLinesOneByOne(commandLines, tabStop);
                }
                else
                {
                    oln($"\nNO SUB-COMMANDS For '{title}'");
                }
            }
            else
            {
                if (commandLines.Count > 0)
                {
                    if (cmd.Owner != null && cmd.Owner.IsRoot)
                    {
                        oln("\nCommands:");
                    }
                    else
                    {
                        oln("\nSub-Commands:");
                    }
                    ShowLinesOneByOne(commandLines, tabStop);
                }
            }

            // ReSharper disable once InvertIf
            if (optionLines.Count > 0)
            {
                var step = 0;
                // ReSharper disable once UnusedVariable
                foreach (var(lvl, cf) in optionLines)
                {
                    if (!treeMode)
                    {
                        // writer.WriteLine($"\nOptions {UpperBoundLevel - lvl}:");
                        if (cf.cmd.IsRoot)
                        {
                            oln($"\nGlobal Options:");
                        }
                        else if (step == 0)
                        {
                            oln($"\nOptions:"); // {lvl}/{WorkerFunctions.UpperBoundLevel - lvl}:");
                        }
                        else if (step == 1)
                        {
                            oln($"\nParent Options ({cf.cmd.backtraceTitles}):");
                        }
                        else
                        {
                            oln($"\nParents Options ({cf.cmd.backtraceTitles}):");
                        }
                    }

                    ShowLinesOneByOne(cf.lines, tabStop, step);
                    step++;
                }
            }
            else if (!treeMode)
            {
                oln($"\nOptions:\n  None");
            }
        }