-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputFormatters.cs
106 lines (93 loc) · 4.14 KB
/
OutputFormatters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Net;
using static System.Console;
using static System.ConsoleColor;
using static IPKalkulator.IPAddressExtensions;
namespace IPKalkulator
{
static class OutputFormatters
{
const int RightPadding = 15, ThirdRow = 40;
//Metoda izpiše tekst prvega stolpca, IP naslov v decimalni in v binarni obliki.
public static void WriteStandardFormat(string firstCol, IPAddress address, ConsoleColor binaryColor = DarkGray)
{
Write(firstCol.PadRight(RightPadding));
ForegroundColor = Blue;
Write(address.ToString().PadRight(RightPadding));
ForegroundColor = binaryColor;
SetCursorPosition(ThirdRow, CursorTop);
Write(address.ToBinary());
ResetColor();
WriteLine();
}
//Metoda za izpis obarvane bitne maske omrežja za razredni del.
public static void WriteWithColoredStartChars(string line, int firstColoredChars)
{
int firstBitPoz = line.IndexOfAny(new[] { '0', '1' });
ForegroundColor = DarkGray;
Write(line.Substring(0, firstBitPoz));
ForegroundColor = Green;
Write(line.Substring(firstBitPoz, firstColoredChars));
ForegroundColor = DarkGray;
Write(line.Substring(firstBitPoz + firstColoredChars));
}
//Oblikujemo izpis za omrežje, kjer so dodatno obrvani prvi biti glede na razred IP naslova.
public static void WriteNetwork(IPAddress ipAddress, IPAddress subnetMask)
{
var networkAddress = ipAddress.GetNetworkAddress(subnetMask);
var ipClass = networkAddress.GetClass();
Write("Network: ".PadRight(RightPadding));
ForegroundColor = Blue;
string network = $"{networkAddress}/{SubnetMask.GetNetBitLength(subnetMask)}";
Write(network.PadRight(RightPadding));
ForegroundColor = DarkGray;
int numOfColoredBits = (int)ipClass + 1;
if (numOfColoredBits == 5) numOfColoredBits--;
SetCursorPosition(ThirdRow, CursorTop);
if (ipClass > IPClass.Undefined && ipClass < IPClass.Invalid)
WriteWithColoredStartChars(networkAddress.ToBinary(), numOfColoredBits);
else
Write(networkAddress.ToBinary());
Write(" (");
ForegroundColor = Green;
if(ipClass > IPClass.Undefined && ipClass < IPClass.Invalid)
Write($"Class {ipClass}");
else
Write(ipClass);
ForegroundColor = DarkGray;
Write(")");
ResetColor();
WriteLine();
}
//Izpišemo masko podmrežja v obeh oblikah.
public static void WriteNetmask(IPAddress subnetMask)
{
Write("Netmask: ".PadRight(RightPadding));
ForegroundColor = Blue;
string netMask = $"{subnetMask} = {SubnetMask.GetNetBitLength(subnetMask)}";
Write(netMask.PadRight(RightPadding));
ForegroundColor = Red;
SetCursorPosition(ThirdRow, CursorTop);
Write(subnetMask.ToBinary());
ResetColor();
WriteLine();
}
//Izpišemo koliko uporabnih naslovov je na voljo in še ali omrežje spada med nejavne naslove.
public static void WriteAvailableHosts(IPAddress subnetMask, IPAddress ipAddress)
{
Write("Hosts/Net: ".PadRight(RightPadding));
ForegroundColor = Blue;
Write($"{SubnetMask.NumberOfUsableHosts(subnetMask)}".PadRight(RightPadding));
ForegroundColor = DarkBlue;
string specialOutput = "";
if (ipAddress.IsPrivate())
specialOutput = "(Private Internet)";
else if (ipAddress.Equals(IPAddress.Loopback))
specialOutput = "(LOCALHOST)";
SetCursorPosition(ThirdRow, CursorTop);
Write(specialOutput);
ResetColor();
WriteLine();
}
}
}