示例#1
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        const int   bufferSize = 1024;
        Span <byte> buffer     = stackalloc byte[bufferSize];

        foreach (var path in args.Skip(1))
        {
            if (!File.Exists(path))
            {
                consoleHost.WriteLine($"cat: {path}: No such file or directory"); continue;
            }
            if (Directory.Exists(path))
            {
                consoleHost.WriteLine($"cat: {path}: Is a directory"); continue;
            }
            using var stream = File.OpenRead(path);
            for (; ;)
            {
                var cbRead = stream.Read(buffer);
                if (cbRead == 0)
                {
                    break;
                }
                var decodedText = Encoding.UTF8.GetString(buffer.Slice(0, cbRead));
                consoleHost.Write(decodedText);
                if (cbRead < bufferSize)
                {
                    break;
                }
            }
        }
    }
示例#2
0
        public void Invoke(IConsoleHost consoleHost, string[] args)
        {
            var commandName = args[0];

            try
            {
                var options = CommandLineSwitch.Parse <Options>(ref args);
                if (args.Skip(1).Any() == false || options.Help)
                {
                    Usage(consoleHost, commandName);
                    return;
                }

                var found = _Fonts.Value.TryGetValue(options.Font.ToLower(), out var getFont);
                if (!found)
                {
                    throw new FontNotFoundException(options.Font);
                }

                var bannerText = getFont().Render(string.Join(' ', args.Skip(1)));
                consoleHost.WriteLine(bannerText);
            }

            catch (FontNotFoundException e) { consoleHost.WriteLine(Yellow(e.Message)); }
            catch (InvalidCommandLineSwitchException e)
            {
                consoleHost.WriteLine(Yellow(e.Message));
                Usage(consoleHost, commandName);
            }
        }
        private async Task Run(Options options, string userAgent)
        {
            var file = !string.IsNullOrEmpty(options.ScriptFile)
                ? LoadScriptFile(options.ScriptFile)
                : null;

            ConsoleHost = CreateSystemConsoleHost(options, file);

            var verbose = file == null || options.VerboseMode;

            var baseUrl = GetBaseUrl(options);

            if (verbose)
            {
                ConsoleHost.Write($"Loading from swagger endpoint '{options.SwaggerEndpoint}'...");
            }

            var commandParser = new CommandParser();
            var variables     = new InMemoryVariables();

            var watch = Stopwatch.StartNew();

            using var client = CreateHttpClient(baseUrl, userAgent);

            var classes = await CreateSwaggerClasses(client, options);

            if (verbose)
            {
                ConsoleHost.WriteLine($" Done ({watch.Elapsed.TotalMilliseconds:N0}ms).");
            }

            ConsoleHost.WriteLine();

            var commandProcessor = new CommandProcessor(commandParser, variables, classes, client);

            if (file != null)
            {
                if (verbose)
                {
                    ConsoleHost.Write("Running script... ");
                }

                var consoleHost = options.PrintScriptResponses
                    ? ConsoleHost
                    : new NullConsoleHost();
                await new ScriptedShell(consoleHost, commandParser, variables, commandProcessor, file).Run();

                if (verbose)
                {
                    ConsoleHost.WriteLine(" Done.");
                }

                ConsoleHost.WriteLine();
            }

            if (!options.RunInteractiveShell)
            {
                await new InteractiveShell(ConsoleHost, commandParser, variables, commandProcessor).Run();
            }
        }
示例#4
0
 private void Usage(IConsoleHost consoleHost, string commandName)
 {
     consoleHost.WriteLine($"Usage: {commandName} [-f <font name>] [-h] <message>");
     consoleHost.WriteLine("  Available font names:");
     foreach (var fontName in _Fonts.Value.Keys.OrderBy(name => name))
     {
         consoleHost.WriteLine($"  - {fontName}");
     }
 }
示例#5
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        var pathCollection = args.Skip(1).Where(p => !string.IsNullOrEmpty(p));

        if (!pathCollection.Any())
        {
            pathCollection = pathCollection.Append(Path.Combine(Environment.CurrentDirectory, "*.*"));
        }

        var multiEntries = pathCollection.Count() > 1;
        var firstEntry   = true;

        foreach (var path in pathCollection)
        {
            var fullPath = Path.GetFullPath(path);
            if (Directory.Exists(fullPath))
            {
                fullPath = Path.Combine(fullPath, "*.*");
            }
            var targetDir = Path.GetDirectoryName(fullPath);
            var wildCard  = Path.GetFileName(fullPath);

            var dirs = Directory.GetDirectories(targetDir, wildCard)
                       .Select(path => (IsDir: true, Name: Path.GetRelativePath(targetDir, path)));
            var files = Directory.GetFiles(targetDir, wildCard)
                        .Select(path => (IsDir: false, Name: Path.GetRelativePath(targetDir, path)));
            var entries = dirs.Concat(files)
                          .OrderBy(e => e.Name, StringComparer.Ordinal)
                          .Select(e => e.IsDir ? Blue(e.Name) : e.Name)
                          .ToArray();

            if (!firstEntry)
            {
                consoleHost.WriteLine();
            }
            if (multiEntries)
            {
                consoleHost.WriteLine($"{path}:");
            }

            if (!entries.Any() && !wildCard.Contains('*') && !wildCard.Contains('?'))
            {
                consoleHost.WriteLine($"ls: cannot access '{path}': No such file or directory");
            }
            else
            {
                consoleHost.WriteLine(string.Join("  ", entries));
            }

            firstEntry = false;
        }
    }
        public override async Task Run()
        {
            while (true)
            {
                var line = consoleHost.ReadLine("$ ").Trim();
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                try
                {
                    if (Exit(line))
                    {
                        break;
                    }

                    await Handle(line);
                }
                catch (ParserException ex)
                {
                    consoleHost.WriteLine(ex.Message);
                }
            }
        }
示例#7
0
    protected void Render(IConsoleHost consoleHost, string text)
    {
        var lines = FiggleFonts.Slant.Render(text).Split('\n').Select(s => s.TrimEnd('\r')).ToArray();

        for (int i = 0; i < lines.Length; i++)
        {
            consoleHost.WriteLine(_Colors[i].Invoke(lines[i]));
        }
    }
示例#8
0
 public void Invoke(IConsoleHost consoleHost, string[] args)
 {
     if (args.Skip(1).Any())
     {
         consoleHost.WriteLine($"Usage: {args[0]}");
     }
     else
     {
         consoleHost.Clear();
     }
 }
示例#9
0
        public void Invoke(IConsoleHost consoleHost, string[] args)
        {
            if (args.Skip(1).Any())
            {
                consoleHost.WriteLine($"Usage: {args[0]}");
            }
            else
            {
                var commands = _ServiceProvider.GetServices <ICommand>()
                               .OrderBy(cmd => cmd.Names.First())
                               .Select(cmd => (Names: string.Join(", ", cmd.Names), cmd.Description))
                               .ToArray();
                var maxWidthCmdNames = commands.Max(maxWidthCmdNames => maxWidthCmdNames.Names.Length);

                foreach (var command in commands)
                {
                    consoleHost.WriteLine($"{Cyan(command.Names.PadRight(maxWidthCmdNames))} {DarkGray("...")} {command.Description}");
                }
            }
        }
示例#10
0
 public void Invoke(IConsoleHost consoleHost, string[] args)
 {
     consoleHost.WriteLine($"{Cyan("Framework Description")}  - {RuntimeInformation.FrameworkDescription}");
     consoleHost.WriteLine($"{Cyan("Process Architecture")}   - {RuntimeInformation.ProcessArchitecture}");
     consoleHost.WriteLine($"{Cyan("OS Architecture")}        - {RuntimeInformation.OSArchitecture}");
     consoleHost.WriteLine($"{Cyan("OS Description")}         - {RuntimeInformation.OSDescription}");
     consoleHost.WriteLine($"{Cyan("OS Platform")}            - {Environment.OSVersion.Platform}");
     consoleHost.WriteLine($"{Cyan("OS Version")}             - {Environment.OSVersion.Version}");
     consoleHost.WriteLine($"{Cyan("OS Version ServicePack")} - {Environment.OSVersion.ServicePack}");
 }
示例#11
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        if (args.Length < 2)
        {
            consoleHost.WriteLine("mkdir: missing operand"); return;
        }
        foreach (var path in args.Skip(1))
        {
            var fullPath = Path.GetFullPath(path);
            if (Directory.Exists(fullPath))
            {
                consoleHost.WriteLine($"mkdir: cannot create directory ‘{path}’: File exists"); continue;
            }
            if (File.Exists(fullPath))
            {
                consoleHost.WriteLine($"mkdir: cannot create directory ‘{path}’: File exists"); continue;
            }

            Directory.CreateDirectory(fullPath);
        }
    }
示例#12
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        var envVals = Environment.GetEnvironmentVariables();

        if (args.Length == 1)
        {
            foreach (DictionaryEntry envVal in envVals)
            {
                consoleHost.WriteLine($"{envVal.Key}={envVal.Value}");
            }
        }
        else
        {
            foreach (var arg in args.Skip(1))
            {
                if (!envVals.Contains(arg))
                {
                    continue;
                }
                consoleHost.WriteLine(envVals[arg].ToString());
            }
        }
    }
示例#13
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        if (args.Length < 2)
        {
            return;
        }
        var path     = args[1];
        var fullPath = Path.GetFullPath(path);

        if (!Directory.Exists(fullPath))
        {
            consoleHost.WriteLine($"cd: {path}: No such file or directory");
            return;
        }
        Environment.CurrentDirectory = fullPath;
    }
        private byte[] Compile(string code)
        {
            try
            {
                return(compiler.Compile(code));
            }
            catch (CompilationErrorException ex)
            {
                foreach (var d in ex.Diagnostics)
                {
                    consoleHost.WriteLine(d.ToString());
                }

                throw;
            }
        }
示例#15
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        var assembly    = this.GetType().Assembly;
        var version     = assembly.GetName().Version;
        var productName = assembly.GetCustomAttribute <AssemblyProductAttribute>().Product;
        var copyright   = assembly.GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright;

        consoleHost.WriteLine($"{Yellow(productName)}");
        consoleHost.WriteLine($"{Cyan("Version")}   - {version}");
        consoleHost.WriteLine($"{Cyan("Copyright")} - {copyright}");
        consoleHost.WriteLine($"{Cyan("3rd party notice")} - {DarkCyan("[follow this link](https://github.com/jsakamoto/jsakamoto.github.io/blob/master/THIRD-PARTY-NOTICES.txt)")}");

        consoleHost.WriteLine();
        consoleHost.WriteLine(Cyan("Special Thanks to") + " -");
        consoleHost.Write("This project has been started after being inspired by [@AtriaSoft](https://twitter.com/AtriaSoft)'s ");
        consoleHost.WriteLine("[\"CUIPortfolio\"](https://github.com/Atria64/CUIPortfolio) project.");
    }
示例#16
0
    public void Invoke(IConsoleHost consoleHost, string[] args)
    {
        if (args.Skip(1).Any())
        {
            consoleHost.WriteLine($"Usage: {args[0]}");
            return;
        }

        consoleHost.WriteLine(Cyan("Name") + "      - J.Sakamoto");
        consoleHost.WriteLine(Cyan("Location") + "  - Sapporo, Hokkaido, Japan");

        consoleHost.WriteLine();

        consoleHost.WriteLine(Cyan("Biography") + " -");
        consoleHost.WriteLine("I'm a programmer since before 30 years over.");
        consoleHost.WriteLine("My most favorite technical area is building a Web application built on .NET technology.");
        consoleHost.WriteLine("I have been used C# (with Blazor, ASP.NET Core, EFCore) and TypeScript (with Angular) on Visual Studio and Windows OS for creating my products.");
        consoleHost.WriteLine("I also publish numerous NuGet packages on the nuget.org as an open-source.");

        consoleHost.WriteLine();

        consoleHost.WriteLine($"{Cyan("Twitter")} - {DarkCyan("[@jsakamoto](https://twitter.com/jsakamoto)")}");
        consoleHost.WriteLine($"{Cyan("GitHub ")} - {DarkCyan("[https://github.com/jsakamoto](https://github.com/jsakamoto)")}");
        consoleHost.WriteLine($"{Cyan("NuGet  ")} - {DarkCyan("[https://www.nuget.org/profiles/jsakamoto](https://www.nuget.org/profiles/jsakamoto)")}");
        consoleHost.WriteLine($"{Cyan("Blogs  ")} -");
        consoleHost.WriteLine($" - dev.to      - {DarkCyan("[https://dev.to/j_sakamoto](https://dev.to/j_sakamoto)")} {DarkGray(" (English contents)")}");
        consoleHost.WriteLine($" - Qiita       - {DarkCyan("[https://qiita.com/jsakamoto](https://qiita.com/jsakamoto)")} {DarkGray(" (Japanese contents)")}");
        consoleHost.WriteLine($" - excite blog - {DarkCyan("[https://devadjust.exblog.jp/](https://devadjust.exblog.jp/)")} {DarkGray(" (Japanese contents)")}");
    }
示例#17
0
 public void Invoke(IConsoleHost consoleHost, string[] args)
 {
     consoleHost.WriteLine(Environment.CurrentDirectory);
 }
示例#18
0
 public void HandleError(string message, Exception innerException)
 {
     consoleHost.WriteLine(message);
 }