1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using AntShell;
9 using AntShell.Commands;
10 using Antmicro.Renode.UserInterface.Tokenizer;
11 
12 namespace Antmicro.Renode.UserInterface.Commands
13 {
14     public class WatchCommand : AutoLoadCommand
15     {
PrintHelp(ICommandInteraction writer)16         public override void PrintHelp(ICommandInteraction writer)
17         {
18             base.PrintHelp(writer);
19 
20             writer.WriteLine("\nUsage:");
21             writer.WriteLine("watch \"<command>\" <refresh period in ms>");
22         }
23 
24         [Runnable]
Run(ICommandInteraction writer, StringToken command, DecimalIntegerToken delay)25         public void Run(ICommandInteraction writer, StringToken command, DecimalIntegerToken delay)
26         {
27             var terminal = writer as CommandInteraction;
28             if(terminal == null)
29             {
30                 writer.WriteError("Watch command can be used on a full-featured terminal only");
31                 return;
32             }
33 
34             var eater = new CommandInteractionEater();
35             while(!terminal.HasNewInput)
36             {
37                 terminal.SaveCursor();
38                 monitor.Parse(command.Value, eater);
39 
40                 writer.Write(eater.GetContents());
41                 var error = eater.GetError();
42                 if(error.Length > 0)
43                 {
44                     writer.WriteError(error);
45                     break;
46                 }
47                 eater.Clear();
48 
49                 System.Threading.Thread.Sleep((int)delay.Value);
50                 terminal.RestoreCursor();
51                 terminal.ClearToEnd();
52             }
53         }
54 
WatchCommand(Monitor monitor)55         public WatchCommand(Monitor monitor) : base(monitor, "watch", "executes a command periodically, showing output in monitor", "w")
56         {
57         }
58     }
59 }