public IObservable <string> Evaluate(ParsedLine line) { if (!line.IsCommand || line.Command.ToLower() != "countdown") { return(null); } var options = new CountdownTimerOptions(); if (!Parser.Default.ParseArguments(line.Args, options)) { return(Return(options.GetUsage())); } try { int seconds = options.DurationSeconds; int interval = options.IntervalSeconds; var unitLabels = options.ParseUnitLabels(options.IntervalString); // Create an interval sequence that fires off a value every [interval] seconds IObservable <string> seq = Interval(TimeSpan.FromSeconds(interval)) // Run that seq until the total time has exceeded the [seconds] value .TakeWhile(l => ((l + 1) * interval) < seconds) // Project each element in the sequence to a human-readable time value .Select( l => String.Format("{0} remaining...", FormatRemaining(seconds - ((l + 1) * interval), unitLabels))); // If there are any other events configured, merge them into the sequence if (options.Events.Any()) { var events = options.Events.OrderBy(@event => @event.Target); var eventTimes = Interval(TimeSpan.FromSeconds(1)) .TakeWhile(l => l < seconds) .Where(l => events.Any(@event => @event.Target == l)) .Select(l => events.First(@event => @event.Target == l).Message); seq = seq.Merge(eventTimes); } // Add a start and end message return(Return(String.Format("{0} remaining...", FormatRemaining(seconds, options.ParseUnitLabels(options.DurationString)))) .Concat(seq) .Concat(Return(String.Join(" ", options.FinishedMessage)))); } catch (ArgumentException) { return(Return(options.GetUsage())); } }
public IObservable<string> Evaluate(ParsedLine line) { if(!line.IsCommand || line.Command.ToLower() != "countdown") { return null; } var options = new CountdownTimerOptions(); if(!Parser.Default.ParseArguments(line.Args, options)) { return Return(options.GetUsage()); } try { int seconds = options.DurationSeconds; int interval = options.IntervalSeconds; var unitLabels = options.ParseUnitLabels(options.IntervalString); // Create an interval sequence that fires off a value every [interval] seconds IObservable<string> seq = Interval(TimeSpan.FromSeconds(interval)) // Run that seq until the total time has exceeded the [seconds] value .TakeWhile(l => ((l + 1) * interval) < seconds) // Project each element in the sequence to a human-readable time value .Select( l => String.Format("{0} remaining...", FormatRemaining(seconds - ((l + 1) * interval), unitLabels))); // If there are any other events configured, merge them into the sequence if(options.Events.Any()) { var events = options.Events.OrderBy(@event => @event.Target); var eventTimes = Interval(TimeSpan.FromSeconds(1)) .TakeWhile(l => l < seconds) .Where(l => events.Any(@event => @event.Target == l)) .Select(l => events.First(@event => @event.Target == l).Message); seq = seq.Merge(eventTimes); } // Add a start and end message return Return(String.Format("{0} remaining...", FormatRemaining(seconds, options.ParseUnitLabels(options.DurationString)))) .Concat(seq) .Concat(Return(String.Join(" ", options.FinishedMessage))); } catch(ArgumentException) { return Return(options.GetUsage()); } }