dmitri.shuralyov.com/website/gido/...

Factor out table rendering code into renderTable.

The code to render a table of packages will be called more often in the
following commit. This refactor will make the upcoming diff smaller.

Rename isStandard parameter to give it a more readable name.
dmitshur committed 2 years ago commit ca977c66e3dcde9dcd1032ae704b6dbacb6b89a7
main.go
@@ -206,14 +206,10 @@ func (h *handler) ServeIndex(w http.ResponseWriter, req *http.Request) error {
 
 	// Find some popular packages to display.
 	h.s.PackageIssuesMu.RLock()
 	pis := h.s.PackageIssues
 	h.s.PackageIssuesMu.RUnlock()
-	type pkg struct {
-		Path string
-		Open int
-	}
 	var popular []pkg
 	for _, p := range h.s.Packages {
 		popular = append(popular, pkg{
 			Path: p,
 			Open: len(pis[p].Open),
@@ -221,36 +217,46 @@ func (h *handler) ServeIndex(w http.ResponseWriter, req *http.Request) error {
 	}
 	sort.SliceStable(popular, func(i, j int) bool { return popular[i].Open > popular[j].Open })
 	popular = popular[:15]
 
 	// Render the table.
-	_, err = io.WriteString(w, `<table class="table table-sm">
+	err = renderTable(w, popular)
+	if err != nil {
+		return err
+	}
+
+	_, err = io.WriteString(w, htmlPart3)
+	return err
+}
+
+type pkg struct {
+	Path string
+	Open int
+}
+
+func renderTable(w io.Writer, pkgs []pkg) error {
+	_, err := io.WriteString(w, `<table class="table table-sm">
 		<thead>
 			<tr>
 				<th>Path</th>
 				<th>Open Issues</th>
 			</tr>
 		</thead>
 		<tbody>`)
 	if err != nil {
 		return err
 	}
-	for _, p := range popular {
+	for _, p := range pkgs {
 		err := html.Render(w, htmlg.TR(
 			htmlg.TD(htmlg.A(p.Path, "/"+p.Path)),
 			htmlg.TD(htmlg.Text(fmt.Sprint(p.Open))),
 		))
 		if err != nil {
 			return err
 		}
 	}
 	_, err = io.WriteString(w, `</tbody></table>`)
-	if err != nil {
-		return err
-	}
-
-	_, err = io.WriteString(w, htmlPart3)
 	return err
 }
 
 // ServeIssues serves a list of issues for the package with import path pkg.
 func (h *handler) ServeIssues(w http.ResponseWriter, req *http.Request, pkg string) error {
service.go
@@ -84,17 +84,17 @@ func category(importPath string) int {
 	default:
 		panic("unreachable")
 	}
 }
 
-// isStandard reports whether import path p is in standard library.
+// isStandard reports whether import path importPath is in standard library.
 // It's determined by whether the first '/'-separated element contains a dot.
-func isStandard(p string) bool {
-	if i := strings.IndexByte(p, '/'); i != -1 {
-		p = p[:i]
+func isStandard(importPath string) bool {
+	if i := strings.IndexByte(importPath, '/'); i != -1 {
+		importPath = importPath[:i]
 	}
-	return !strings.Contains(p, ".")
+	return !strings.Contains(importPath, ".")
 }
 
 func (s *service) poll(ctx context.Context) {
 	corpus, repo, err := initCorpus(ctx)
 	if err != nil {