Beispiel #1
0
        public void List(Filter filter, FilterProp what)
        {
            List <HostsFileLine> hosts;

            using (new Watch("read file")) {
                hosts = _hostsFile.GetHosts().ToList();
            }

            var filterd = Filter(hosts, filter, what);

            Print(filterd, hosts.Count, what);
        }
Beispiel #2
0
 private void Print(List <HostModel> filterd, int countall, FilterProp what)
 {
     using (new Watch("print")) {
         ConsoleEx.PrintWithPaging(
             list: filterd,
             countAll: countall,
             header: ConsoleWriter.CreateLine(" Line |"),
             line: (x, nr) => new ConsoleWriter()
             .FormatLine("{line,6}|{disabled} {ip}   {domain}    {comment}", parms => parms
                         .Add("line", x.LineNumber, what == FilterProp.Line)
                         .Add("disabled", x.IsDisabled ? "#" : "")
                         .Add("ip", x.Ip, what == FilterProp.Domain)
                         .Add("domain", x.Domain, what == FilterProp.Domain)
                         .Add("comment", x.Commentar, what == FilterProp.Commentar)
                         )
             );
     }
 }
Beispiel #3
0
        public void Duplicates(Filter filter, FilterProp what)
        {
            List <HostsFileLine> hosts;

            using (new Watch("read file")) {
                hosts = _hostsFile.GetHosts().ToList();
            }

            var filterd = Filter(hosts, filter, what);

            var duplicates = (from f in filterd
                              group f by $"{f.Ip.Value} {f.Domain.Value}"
                              into g
                              where g.Count() > 1
                              select g).ToList();

            ConsoleEx.PrintWithPaging(
                list: duplicates,
                countAll: hosts.Count,
                line: (models, i) => new ConsoleWriter()
                .FormatLine("{count,5} {domain}", parms => parms
                            .Add("count", models.Count())
                            .Add("domain", models.Key)
                            )
                .FormatLines("          {ip} {domain}", models.ToList(), (model, parms) => parms
                             .Add("ip", model.Ip)
                             .Add("domain", model.Domain)
                             )
                );

            if (duplicates.Any())
            {
                var toremove = duplicates.SelectMany(x => x.Skip(1).Select(_ => _.Model)).ToList();
                if (ConsoleEx.AskForYes($"remove {toremove.Count} duplicates?"))
                {
                    _hostsFile.Remove(toremove);
                    toremove.ForEach(x => Console.WriteLine($"removed: {HostsFile.CreateTextLine(x)}"));
                }
            }
        }