Esempio n. 1
0
        static void Main(string[] args)
        {
            // This application demostrate the basics of ConsoleAsync

            // Create forst console
            IConsole console = ConsoleAsync.CreateConsole("Console");

            // Add commands
            console.AddCommand("quit", (writer, strings) => ConsoleAsync.Quit());
            console.AddCommand("print", (writer, strings) => strings.ForEach(s => writer.Text(s).NewLine()));

            // Execute operation on console
            console.Execute(writer =>
            {
                writer.Info("ConsoleAsync").NewLine().NewLine();
                writer.Text(@"
Available commands:

quit   : close entirely the app
print  : print in console all the arguments
").NewLine();
            });

            // Wait commands from user
            ConsoleAsync.Run();
        }
Esempio n. 2
0
        static void CreateWorkerConsole(string name, TimeSpan interval, double margin)
        {
            // Create console
            IConsole console = ConsoleAsync.CreateConsole(name);

            // Create new instance of worker
            TestWorker worker = new TestWorker(interval, console.Name, margin);

            // Add worker to collection
            workers.Add(worker);

            // Add worker to newly created console
            IConsoleWorker workerInterface = console.AddWorker(worker);

            // Add command suspend
            console.AddCommand("suspend", (writer, list) =>
            {
                // Suspend worker and write a message
                workerInterface.Suspend();
                writer.Info("Worker suspended").NewLine();
            });

            // Add command resume
            console.AddCommand("resume", (writer, list) =>
            {
                // Resume worker and write a message
                workerInterface.Resume();
                writer.Info("Worker resumed").NewLine();
            });

            // Add command quit
            console.AddCommand("quit", (writer, list) => ConsoleAsync.Quit());
        }
Esempio n. 3
0
        static void CreateNewConsole()
        {
            // Create console name
            string name = string.Format("Console #{0:00}", consoleIndex + 1);

            // Create console
            IConsole console = ConsoleAsync.CreateConsole(name);

            // Add default command
            console.AddCommand("quit", (writer, strings) => ConsoleAsync.Quit());
            console.AddCommand("print", (writer, strings) => strings.ForEach(s => writer.Text(s).NewLine()));

            // Call execute to write a message
            console.Execute(writer =>
            {
                writer.Info(name).NewLine().NewLine();
                writer.Text(@"
Available commands:

quit   : close entirely the app
print  : print in console all the arguments
");
            });

            // Increment console count
            consoleIndex++;
        }
Esempio n. 4
0
        static void CreateControlConsole()
        {
            // Define an interval
            TimeSpan interval = TimeSpan.FromMilliseconds(500);

            // Create the console
            IConsole control = ConsoleAsync.CreateConsole("Control Console");

            // Add quit command
            control.AddCommand("quit", (writer, list) => ConsoleAsync.Quit());

            // Create worker from builtin type TimedWorker
            ConsoleWorker controlWorker = new TimedWorker(interval, (writer, span) =>
            {
                // Clear console and write title and info
                writer.Clear();
                writer.Info("CONTROL CONSOLE").NewLine();
                writer.Muted("Elapsed: {0}", span).NewLine().NewLine();

                // Cicle workers
                foreach (TestWorker worker in workers)
                {
                    // Calculate percentage of success over 60 characters
                    int success = (60 * worker.Successes + 1) / (worker.Successes + worker.Failures + 1);
                    int failure = 60 - success;

                    // Write worker name fitted on 16 char (to make bars aligned)
                    writer.Text(worker.Name.Fit(16));

                    // Write success bar
                    writer.Success("".Fit(success, '\u25A0'));

                    // Write error bar
                    writer.Error("".Fit(failure, '\u25A0'));

                    writer.NewLine();
                }

                // Write a message and commands help
                writer.NewLine().NewLine().NewLine().Info("Available commands").NewLine();
                writer.Text(@"
suspend     : suspend worker (not in control console)
resume      : resume worker (not in control console)
quit        : close application (in any console)
");
            });

            // Add worker to control console
            control.AddWorker(controlWorker);
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            // Create console
            IConsole console = ConsoleAsync.CreateConsole("Folder Browser");

            // Add quit command to console
            ConsoleAsync.AddCommandToAllConsole("quit", (writer, strings) => ConsoleAsync.Quit());

            // Add help command to console
            console.AddCommand("help", (writer, strings) => writer.Text(@"
Switch to second console with TAB button
the available command are:

dir                          show content of a current directory
cd ..                        back to parent directory
cd <directory name>          enter in specified directory
md <directory name>          create a directory
rd <directory name>          delete a directory
").NewLine().NewLine());

            // Add cd commands to console
            console.AddCommand("cd", (writer, strings) =>
            {
                // check if there at least one parameter
                if (strings.Count > 0)
                {
                    ChangeFolder(writer, strings[0]);
                }
            });

            // Add cd.. commands to console
            console.AddCommand("cd..", (writer, strings) => ChangeFolder(writer, ".."));

            // Add cd commands to console
            console.AddCommand("dir", (writer, strings) => WriteFolder(writer));

            // Add md commands to console
            console.AddCommand("md", (writer, strings) =>
            {
                // check if there at least one parameter
                if (strings.Count < 1)
                {
                    // If not throw an exception
                    WriteError(writer, new ArgumentException("makedir require a parameter"));
                    return;
                }

                try
                {
                    // Try to create directory with passed parameter
                    Directory.CreateDirectory(Path.Combine(actualFolder, strings[0]));
                    writer.Info("Directory created").NewLine();
                }
                catch (Exception ex)
                {
                    // Write exception
                    WriteError(writer, ex);
                }
            });

            console.AddCommand("rd", (writer, strings) =>
            {
                // check if there at least one parameter
                if (strings.Count < 1)
                {
                    // If not throw an exception
                    WriteError(writer, new ArgumentException("removedir require a parameter"));
                    return;
                }

                try
                {
                    // Try to delete directory with passed parameter
                    Directory.Delete(Path.Combine(actualFolder, strings[0]));
                    writer.Info("Directory deleted").NewLine();
                }
                catch (Exception ex)
                {
                    // Write exception
                    WriteError(writer, ex);
                }
            });

            // Call an execute to write initial message to console
            console.Execute(writer =>
            {
                writer.Info("Folder Explorer sample").NewLine().NewLine();
                console.SendCommand("help");
            });

            // Wait for user commands
            ConsoleAsync.Run();
        }