コード例 #1
0
ファイル: SearchCommand.cs プロジェクト: hnrt/TextSearch
 private void FormatQueryResults(IEnumerable <HitFile> results)
 {
     foreach (var f in results)
     {
         var contents = FileContents.Find(f.Fid);
         if (contents == null)
         {
             contents = file.DownloadFile(f.Fid);
         }
         Console.WriteLine("{0}", contents.Path);
         foreach (var entry in f.Rows)
         {
             Console.WriteLine("{0,6}: {1}", entry.Row + 1, contents.Lines[entry.Row]);
         }
     }
 }
コード例 #2
0
 public void Register(CommandLine commandLine, CommandQueue commandQueue)
 {
     commandLine
     .AddHandler("-print-files", (e) =>
     {
         string group = DEFAULT_GROUP;
         if (e.MoveNext())
         {
             group = (string)e.Current;
         }
         else
         {
             commandLine.NoMoreArg = true;
         }
         commandQueue.Add(() =>
         {
             var fi = file.GetFiles(group);
             foreach (var entry in fi.OrderBy(x => x.Fid))
             {
                 Console.WriteLine("[{0}] {1}", entry.Fid, entry.Path);
             }
         });
     })
     .AddHandler("-print-file-stats", (e) =>
     {
         string group = DEFAULT_GROUP;
         if (e.MoveNext())
         {
             group = (string)e.Current;
         }
         else
         {
             commandLine.NoMoreArg = true;
         }
         commandQueue.Add(() =>
         {
             var stats = file.GetFileStats(group);
             Console.WriteLine("{0}", stats);
         });
     })
     .AddHandler("-print-file", (e) =>
     {
         if (!e.MoveNext())
         {
             throw new Exception("Group name is not specified.");
         }
         var group = (string)e.Current;
         if (!e.MoveNext())
         {
             throw new Exception("Path is not specified.");
         }
         var path = (string)e.Current;
         commandQueue.Add(() =>
         {
             var info     = file.GetFile(group, path);
             var contents = FileContents.Find(info.Fid);
             if (contents == null)
             {
                 contents = file.DownloadFile(info.Fid);
             }
             foreach (var line in contents.Lines)
             {
                 Console.WriteLine(line);
             }
         });
     })
     .AddHandler("-index-files", (e) =>
     {
         var forceIndexing = false;
         var commandLine2  = new CommandLine();
         commandLine2
         .AddHandler("-force", (ee) =>
         {
             forceIndexing = true;
         })
         .AddHandler("-concurrency", (ee) =>
         {
             if (!e.MoveNext())
             {
                 throw new Exception("Concurrency level number is not specified.");
             }
             concurrencyLevel = int.Parse((string)ee.Current);
             if (concurrencyLevel < 1 || 255 < concurrencyLevel)
             {
                 throw new Exception("Concurrency level number is out of the valid range.");
             }
         })
         .AddHandler("-immediate", (ee) =>
         {
             indexMode = IndexMode.Immediate;
         })
         .AddTranslation("-f", "-force")
         .AddTranslation("-c", "-concurrency")
         .AddTranslation("-i", "-immediate");
         e = commandLine2.Parse(e);
         if (e == null)
         {
             throw new Exception("Group name is not specified.");
         }
         var group = (string)e.Current;
         if (!e.MoveNext())
         {
             throw new Exception("Path is not specified.");
         }
         List <string> paths = new List <string>()
         {
             (string)e.Current
         };
         while (e.MoveNext())
         {
             paths.Add((string)e.Current);
         }
         commandLine.NoMoreArg = true;
         commandQueue.Add(() =>
         {
             IndexFiles(group, paths, forceIndexing);
         });
     })
     .AddHandler("-delete-files", (e) =>
     {
         string group = DEFAULT_GROUP;
         if (e.MoveNext())
         {
             group = (string)e.Current;
         }
         else
         {
             commandLine.NoMoreArg = true;
         }
         commandQueue.Add(() =>
         {
             Console.WriteLine("Started deleting files...");
             var t1 = DateTime.Now;
             file.DeleteFiles(group);
             var t2 = DateTime.Now;
             Console.WriteLine("Done. Elapsed time: {0}", t2 - t1);
         });
     })
     .AddHandler("-delete-stale-files", (e) =>
     {
         string group = DEFAULT_GROUP;
         if (e.MoveNext())
         {
             group = (string)e.Current;
         }
         else
         {
             commandLine.NoMoreArg = true;
         }
         commandQueue.Add(() =>
         {
             Console.WriteLine("Started deleting stale files...");
             var t1 = DateTime.Now;
             file.DeleteStaleFiles(group);
             var t2 = DateTime.Now;
             Console.WriteLine("Done. Elapsed time: {0}", t2 - t1);
         });
     })
     .AddTranslation("-pf", "-print-files")
     .AddTranslation("-pfs", "-print-file-stats")
     .AddTranslation("-i", "-index-files")
     .AddTranslation("-index", "-index-files")
     .AddUsageHeader("Usage <file>:")
     .AddUsage("{0} -print-files [GROUPNAME]", Program.Name)
     .AddUsage("{0} -print-file-stats [GROUPNAME]", Program.Name)
     .AddUsage("{0} -print-file GROUPNAME PATH", Program.Name)
     .AddUsage("{0} -index-files [-force] [-concurrency NUMBER] [-immediate] GROUPNAME PATH...", Program.Name)
     .AddUsage("{0} -delete-files [GROUPNAME]", Program.Name)
     .AddUsage("{0} -delete-stale-files [GROUPNAME]", Program.Name);
 }
コード例 #3
0
ファイル: SearchCommand.cs プロジェクト: hnrt/TextSearch
        private void FormatQueryResultsInHtml(IEnumerable <HitFile> results)
        {
            var sb = new StringBuilder();

            Console.WriteLine("<!doctype html>");
            Console.WriteLine("<html>");
            Console.WriteLine("<head>");
            Console.WriteLine("<style>");
            Console.WriteLine("table { border-collapse:collapse; border-width:thin; border-style:solid; width:100%; }");
            Console.WriteLine("tr td { border-width:thin; border-style:solid; }");
            Console.WriteLine("td.lineno { width:6em; text-align:right; background-color:lightgray; }");
            Console.WriteLine("font.path { color:darkgreen; }");
            Console.WriteLine("font.match { color:red; }");
            Console.WriteLine("</style>");
            Console.WriteLine("</head>");
            Console.WriteLine("<body>");
            foreach (var prc in results)
            {
                var contents = FileContents.Find(prc.Fid);
                if (contents == null)
                {
                    contents = file.DownloadFile(prc.Fid);
                }
                Console.WriteLine("<p>");
                Console.WriteLine("<font class=\"path\">{0}</font>", contents.Path);
                Console.WriteLine("<table>");
                foreach (var entry in prc.Rows)
                {
                    var line = contents.Lines[entry.Row];
                    sb.Length = 0;
                    int u = 0;
                    int v = 0;
                    for (int i = 0; i < line.Length; i++)
                    {
                        if (u < entry.Matches.Count && i == entry.Matches[u].StartCol)
                        {
                            if (u++ == v)
                            {
                                sb.Append("<font class=\"match\">");
                            }
                        }
                        if (v < entry.Matches.Count && i == entry.Matches[v].EndCol)
                        {
                            if (++v == u)
                            {
                                sb.Append("</font>");
                            }
                        }
                        char c = line[i];
                        if (c == '&')
                        {
                            sb.Append("&amp;");
                        }
                        else if (c == '<')
                        {
                            sb.Append("&lt;");
                        }
                        else if (c == '>')
                        {
                            sb.Append("&gt;");
                        }
                        else
                        {
                            sb.Append(c);
                        }
                    }
                    if (v < u)
                    {
                        sb.Append("</font>");
                    }
                    Console.WriteLine("<tr><td class=\"lineno\">{0}</td><td>{1}</td></tr>", entry.Row + 1, sb.ToString());
                }
                Console.WriteLine("</table>");
                Console.WriteLine("</p>");
            }
            Console.WriteLine("</body>");
            Console.WriteLine("</html>");
        }