mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-10-31 09:09:25 +08:00 
			
		
		
		
	Make it possible to set Value directly
This commit is contained in:
		 Oskar Klintrot
					Oskar Klintrot
				
			
				
					committed by
					
						 Patrik Svensson
						Patrik Svensson
					
				
			
			
				
	
			
			
			 Patrik Svensson
						Patrik Svensson
					
				
			
						parent
						
							3a42c0a119
						
					
				
				
					commit
					c64884854f
				
			| @@ -115,5 +115,74 @@ namespace Spectre.Console.Tests.Unit | ||||
|             task.MaxValue.ShouldBe(20); | ||||
|             task.Value.ShouldBe(20); | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Setting_Value_Should_Override_Incremented_Value() | ||||
|         { | ||||
|             // Given | ||||
|             var task = default(ProgressTask); | ||||
|             var console = new FakeConsole(); | ||||
|             var progress = new Progress(console) | ||||
|                 .Columns(new[] { new ProgressBarColumn() }) | ||||
|                 .AutoRefresh(false) | ||||
|                 .AutoClear(false); | ||||
|  | ||||
|             // When | ||||
|             progress.Start(ctx => | ||||
|             { | ||||
|                 task = ctx.AddTask("foo"); | ||||
|                 task.Increment(50); | ||||
|                 task.Value = 20; | ||||
|             }); | ||||
|  | ||||
|             // Then | ||||
|             task.MaxValue.ShouldBe(100); | ||||
|             task.Value.ShouldBe(20); | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Setting_Value_To_MaxValue_Should_Finish_Task() | ||||
|         { | ||||
|             // Given | ||||
|             var task = default(ProgressTask); | ||||
|             var console = new FakeConsole(); | ||||
|             var progress = new Progress(console) | ||||
|                 .Columns(new[] { new ProgressBarColumn() }) | ||||
|                 .AutoRefresh(false) | ||||
|                 .AutoClear(false); | ||||
|  | ||||
|             // When | ||||
|             progress.Start(ctx => | ||||
|             { | ||||
|                 task = ctx.AddTask("foo"); | ||||
|                 task.Value = task.MaxValue; | ||||
|             }); | ||||
|  | ||||
|             // Then | ||||
|             task.IsFinished.ShouldBe(true); | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Should_Increment_Manually_Set_Value() | ||||
|         { | ||||
|             // Given | ||||
|             var task = default(ProgressTask); | ||||
|             var console = new FakeConsole(); | ||||
|             var progress = new Progress(console) | ||||
|                 .Columns(new[] { new ProgressBarColumn() }) | ||||
|                 .AutoRefresh(false) | ||||
|                 .AutoClear(false); | ||||
|  | ||||
|             // When | ||||
|             progress.Start(ctx => | ||||
|             { | ||||
|                 task = ctx.AddTask("foo"); | ||||
|                 task.Value = 50; | ||||
|                 task.Increment(10); | ||||
|             }); | ||||
|  | ||||
|             // Then | ||||
|             task.Value.ShouldBe(60); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -40,5 +40,22 @@ namespace Spectre.Console | ||||
|             task.MaxValue = value; | ||||
|             return task; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Sets the value of the task. | ||||
|         /// </summary> | ||||
|         /// <param name="task">The task.</param> | ||||
|         /// <param name="value">The value.</param> | ||||
|         /// <returns>The same instance so that multiple calls can be chained.</returns> | ||||
|         public static ProgressTask Value(this ProgressTask task, double value) | ||||
|         { | ||||
|             if (task is null) | ||||
|             { | ||||
|                 throw new ArgumentNullException(nameof(task)); | ||||
|             } | ||||
|  | ||||
|             task.Value = value; | ||||
|             return task; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -14,6 +14,7 @@ namespace Spectre.Console | ||||
|  | ||||
|         private double _maxValue; | ||||
|         private string _description; | ||||
|         private double _value; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the task ID. | ||||
| @@ -39,9 +40,13 @@ namespace Spectre.Console | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the value of the task. | ||||
|         /// Gets or sets the value of the task. | ||||
|         /// </summary> | ||||
|         public double Value { get; private set; } | ||||
|         public double Value | ||||
|         { | ||||
|             get => _value; | ||||
|             set => Update(value: value); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the start time of the task. | ||||
| @@ -100,6 +105,7 @@ namespace Spectre.Console | ||||
|             _samples = new List<ProgressSample>(); | ||||
|             _lock = new object(); | ||||
|             _maxValue = maxValue; | ||||
|             _value = 0; | ||||
|  | ||||
|             _description = description?.RemoveNewLines()?.Trim() ?? throw new ArgumentNullException(nameof(description)); | ||||
|             if (string.IsNullOrWhiteSpace(_description)) | ||||
| @@ -109,7 +115,6 @@ namespace Spectre.Console | ||||
|  | ||||
|             Id = id; | ||||
|             State = new ProgressTaskState(); | ||||
|             Value = 0; | ||||
|             StartTime = autoStart ? DateTime.Now : null; | ||||
|         } | ||||
|  | ||||
| @@ -151,7 +156,8 @@ namespace Spectre.Console | ||||
|         private void Update( | ||||
|             string? description = null, | ||||
|             double? maxValue = null, | ||||
|             double? increment = null) | ||||
|             double? increment = null, | ||||
|             double? value = null) | ||||
|         { | ||||
|             lock (_lock) | ||||
|             { | ||||
| @@ -175,13 +181,18 @@ namespace Spectre.Console | ||||
|  | ||||
|                 if (increment != null) | ||||
|                 { | ||||
|                     Value += increment.Value; | ||||
|                     _value += increment.Value; | ||||
|                 } | ||||
|  | ||||
|                 if (value != null) | ||||
|                 { | ||||
|                     _value = value.Value; | ||||
|                 } | ||||
|  | ||||
|                 // Need to cap the max value? | ||||
|                 if (Value > _maxValue) | ||||
|                 if (_value > _maxValue) | ||||
|                 { | ||||
|                     Value = _maxValue; | ||||
|                     _value = _maxValue; | ||||
|                 } | ||||
|  | ||||
|                 var timestamp = DateTime.Now; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user