mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Add Write(...), ``WriteLine(...), Read() and ReadLine() extension methods for IConsole (#140)
This commit is contained in:
@@ -9,7 +9,7 @@ using CliFx.Infrastructure;
|
||||
namespace CliFx.Demo.Commands;
|
||||
|
||||
[Command("book add", Description = "Adds a book to the library.")]
|
||||
public partial class BookAddCommand(LibraryProvider libraryProvider) : ICommand
|
||||
public class BookAddCommand(LibraryProvider libraryProvider) : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||
public required string Title { get; init; }
|
||||
@@ -18,47 +18,38 @@ public partial class BookAddCommand(LibraryProvider libraryProvider) : ICommand
|
||||
public required string Author { get; init; }
|
||||
|
||||
[CommandOption("published", 'p', Description = "Book publish date.")]
|
||||
public DateTimeOffset Published { get; init; } = CreateRandomDate();
|
||||
public DateTimeOffset Published { get; init; } =
|
||||
new(
|
||||
Random.Shared.Next(1800, 2020),
|
||||
Random.Shared.Next(1, 12),
|
||||
Random.Shared.Next(1, 28),
|
||||
Random.Shared.Next(1, 23),
|
||||
Random.Shared.Next(1, 59),
|
||||
Random.Shared.Next(1, 59),
|
||||
TimeSpan.Zero
|
||||
);
|
||||
|
||||
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
|
||||
public Isbn Isbn { get; init; } = CreateRandomIsbn();
|
||||
public Isbn Isbn { get; init; } =
|
||||
new(
|
||||
Random.Shared.Next(0, 999),
|
||||
Random.Shared.Next(0, 99),
|
||||
Random.Shared.Next(0, 99999),
|
||||
Random.Shared.Next(0, 99),
|
||||
Random.Shared.Next(0, 9)
|
||||
);
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
if (libraryProvider.TryGetBook(Title) is not null)
|
||||
throw new CommandException("Book already exists.", 10);
|
||||
throw new CommandException($"Book '{Title}' already exists.", 10);
|
||||
|
||||
var book = new Book(Title, Author, Published, Isbn);
|
||||
libraryProvider.AddBook(book);
|
||||
|
||||
console.Output.WriteLine("Book added.");
|
||||
console.Output.WriteBook(book);
|
||||
console.WriteLine($"Book '{Title}' added.");
|
||||
console.WriteBook(book);
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class BookAddCommand
|
||||
{
|
||||
private static readonly Random Random = new();
|
||||
|
||||
private static DateTimeOffset CreateRandomDate() =>
|
||||
new(
|
||||
Random.Next(1800, 2020),
|
||||
Random.Next(1, 12),
|
||||
Random.Next(1, 28),
|
||||
Random.Next(1, 23),
|
||||
Random.Next(1, 59),
|
||||
Random.Next(1, 59),
|
||||
TimeSpan.Zero
|
||||
);
|
||||
|
||||
private static Isbn CreateRandomIsbn() =>
|
||||
new(
|
||||
Random.Next(0, 999),
|
||||
Random.Next(0, 99),
|
||||
Random.Next(0, 99999),
|
||||
Random.Next(0, 99),
|
||||
Random.Next(0, 9)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ public class BookCommand(LibraryProvider libraryProvider) : ICommand
|
||||
var book = libraryProvider.TryGetBook(Title);
|
||||
|
||||
if (book is null)
|
||||
throw new CommandException("Book not found.", 10);
|
||||
throw new CommandException($"Book '{Title}' not found.", 10);
|
||||
|
||||
console.Output.WriteBook(book);
|
||||
console.WriteBook(book);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ public class BookListCommand(LibraryProvider libraryProvider) : ICommand
|
||||
{
|
||||
// Add margin
|
||||
if (i != 0)
|
||||
console.Output.WriteLine();
|
||||
console.WriteLine();
|
||||
|
||||
// Render book
|
||||
var book = library.Books[i];
|
||||
console.Output.WriteBook(book);
|
||||
console.WriteBook(book);
|
||||
}
|
||||
|
||||
return default;
|
||||
|
||||
@@ -17,11 +17,11 @@ public class BookRemoveCommand(LibraryProvider libraryProvider) : ICommand
|
||||
var book = libraryProvider.TryGetBook(Title);
|
||||
|
||||
if (book is null)
|
||||
throw new CommandException("Book not found.", 10);
|
||||
throw new CommandException($"Book '{Title}' not found.", 10);
|
||||
|
||||
libraryProvider.RemoveBook(book);
|
||||
|
||||
console.Output.WriteLine($"Book {Title} removed.");
|
||||
console.WriteLine($"Book '{Title}' removed.");
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
@@ -33,4 +33,7 @@ internal static class ConsoleExtensions
|
||||
using (writer.Console.WithForegroundColor(ConsoleColor.White))
|
||||
writer.WriteLine(book.Isbn);
|
||||
}
|
||||
|
||||
public static void WriteBook(this IConsole console, Book book) =>
|
||||
console.Output.WriteBook(book);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user