{{ define "title"}}{{ range .BreadCrumbs }}{{ pathUnescape (index . 0)}}/{{ end }} at {{ .Ref }} &middot; {{ .RepoInfo.FullName }} &middot; Tangled{{ end }}


{{ define "extrameta" }}

    {{ $path := "" }}
    {{ range .BreadCrumbs }}
    {{ $path = printf "%s/%s" $path (index . 0) }}
    {{ end }}

    {{ template "repo/fragments/meta" . }}
    {{ $title := printf "%s at %s &middot; %s" $path .Ref .RepoInfo.FullName }}
    {{ $url := printf "https://tangled.org/%s/tree/%s%s" .RepoInfo.FullName .Ref $path }}

    {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }}
{{ end }}


{{define "repoContent"}}
<main>
  <div class="tree">
    {{ $linkstyle := "no-underline hover:underline" }}

    <div class="mb-3 text-sm flex justify-between gap-2 items-center">
      <div class="flex gap-3 items-center">
        <div class="bg-gray-100 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded p-2 flex items-center gap-1 overflow-x-auto whitespace-nowrap w-fit">
          {{ $last := sub (len .BreadCrumbs) 1 }}
          {{ range $i, $b := .BreadCrumbs }}
            {{ if (ne $i $last) }}
              <a href="{{ index $b 1 }}" class="text-gray-500 dark:text-gray-400 {{ $linkstyle }}">{{ pathUnescape (index $b 0) }}</a>
              <span class="text-gray-500 dark:text-gray-400">/</span>
            {{ else }}
              <span class="text-black dark:text-white">{{ pathUnescape (index $b 0) }}</span>
            {{ end }}
          {{ end }}
        </div>
        <div class="text-sm text-gray-500 dark:text-gray-400 flex items-center gap-2">
          {{ $stats := .TreeStats }}
          {{ if eq $stats.NumFolders 1 }}
            <span>{{ $stats.NumFolders }} folder</span>
          {{ else if gt $stats.NumFolders 1 }}
            <span>{{ $stats.NumFolders }} folders</span>
          {{ end }}

          {{ if and (gt $stats.NumFolders 0) (gt $stats.NumFiles 0) }}
            <span class="select-none [&:before]:content-['·']"></span>
          {{ end }}

          {{ if eq $stats.NumFiles 1 }}
            <span>{{ $stats.NumFiles }} file</span>
          {{ else if gt $stats.NumFiles 1 }}
            <span>{{ $stats.NumFiles }} files</span>
          {{ end }}
        </div>
      </div>
      <div class="bg-gray-100 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded p-2 hidden md:flex items-center gap-2 whitespace-nowrap">
        <span class="text-gray-500 dark:text-gray-400">at <a href="/{{ $.RepoInfo.FullName }}/tree/{{ pathEscape $.Ref }}" class="{{ $linkstyle }}">{{ $.Ref }}</a></span>
      </div>
    </div>

    {{ if .LastCommitInfo }}
      {{ template "repo/fragments/lastCommitPanel" $ }}
    {{ end }}

    {{ range .Files }}
      <div class="grid grid-cols-12 gap-4 items-center py-1">
        <div class="col-span-8 md:col-span-4">
          {{ $link := printf "/%s/%s/%s/%s/%s" $.RepoInfo.FullName "tree" (pathEscape $.Ref) $.Path .Name }}
          {{ $icon := "folder" }}
          {{ $iconStyle := "size-4 fill-current" }}

          {{ if .IsSubmodule }}
            {{ $link = printf "/%s/%s/%s/%s/%s" $.RepoInfo.FullName "blob" (urlquery $.Ref) $.Path .Name }}
            {{ $icon = "folder-input" }}
            {{ $iconStyle = "size-4" }}
          {{ end }}

          {{ if .IsFile }}
            {{ $link = printf "/%s/%s/%s/%s/%s" $.RepoInfo.FullName "blob" (urlquery $.Ref) $.Path .Name }}
            {{ $icon = "file" }}
            {{ $iconStyle = "size-4" }}
          {{ end }}

          <a href="{{ $link }}" class="{{ $linkstyle }}">
            <div class="flex items-center gap-2">
              {{ i $icon $iconStyle "flex-shrink-0" }}
              <span class="truncate">{{ .Name }}</span>
            </div>
          </a>
        </div>

        <div class="col-span-0 md:col-span-6 hidden md:block overflow-hidden">
          {{ with .LastCommit }}
            <a href="/{{ $.RepoInfo.FullName }}/commit/{{ .Hash }}" class="text-gray-500 dark:text-gray-400 block truncate">{{ .Message }}</a>
          {{ end }}
        </div>

        <div class="col-span-4 md:col-span-2 text-sm text-right">
          {{ with .LastCommit }}
            <a href="/{{ $.RepoInfo.FullName }}/commit/{{ .Hash }}" class="text-gray-500 dark:text-gray-400">{{ template "repo/fragments/time" .When }}</a>
          {{ end }}
        </div>
      </div>
    {{ end }}

  </div>
</main>
{{ if .LastCommitInfo }}
  {{ template "repo/fragments/permalinkShortcutScript" (printf "/%s/tree/%s/%s" .RepoInfo.RepoIdentifier .LastCommitInfo.Hash .Path) }}
{{ end }}
{{end}}

{{ define "repoAfter" }}
    {{- if or .HTMLReadme .Readme -}}
       {{ template "repo/fragments/readme" . }}
    {{- end -}}
{{ end }}
