// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using SystemConsole = System.Console;
namespace Migration.Console
{
///
/// This class defines methods that help writing to the .
///
public static class ConsoleHelper
{
///
/// Writes as an error text to the .
///
/// A composite format string.
/// An array of objects to write using .
/// Thrown when any of the input parameters is null.
/// Thrown when an I/O error occurred.
/// Thrown when the format specification in is invalid.
///
public static void WriteErrorLine(string format, params object[] args)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
SystemConsole.ForegroundColor = ConsoleColor.Red;
SystemConsole.WriteLine(format, args);
SystemConsole.ResetColor();
}
///
/// Writes as a description text to the .
///
/// A composite format string.
/// An array of objects to write using .
/// Thrown when any of the input parameters is null.
/// Thrown when an I/O error occurred.
/// Thrown when the format specification in is invalid.
///
public static void WriteCommandDescriptionLine(string format, params object[] args)
{
const int paddingLeft = 10;
const int paddingRight = 1;
WriteLineWithPadding(format, args, paddingLeft, paddingRight);
SystemConsole.WriteLine();
}
private static void WriteLineWithPadding(string format, object[] args, int paddingLeft, int paddingRight)
{
if (format == null)
{
throw new ArgumentNullException(nameof(format));
}
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
var windowWidth = GetWindowWidth() - paddingRight;
int bufferSize = windowWidth - paddingLeft;
var paddingString = new string(' ', paddingLeft);
foreach (var line in format.SplitByLength(bufferSize))
{
SystemConsole.WriteLine($@"{paddingString}{line.TrimStart()}", args);
}
}
private static int GetWindowWidth()
{
try
{
return SystemConsole.WindowWidth;
}
catch (IOException)
{
return 80;
}
}
private static IEnumerable SplitByLength(this string str, int maxLength)
{
for (int index = 0; index < str.Length; index += maxLength)
{
yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
}
}
}
}