| 1 | module main |
| 2 | |
| 3 | import veb |
| 4 | import git |
| 5 | |
| 6 | fn pick_plural_form(count int, forms []string) string { |
| 7 | if forms.len == 1 { |
| 8 | return forms[0] |
| 9 | } |
| 10 | if forms.len == 2 { |
| 11 | return if count == 1 { forms[0] } else { forms[1] } |
| 12 | } |
| 13 | n := if count < 0 { -count } else { count } |
| 14 | last_two := n % 100 |
| 15 | last := n % 10 |
| 16 | if last_two >= 11 && last_two <= 14 { |
| 17 | return forms[2] |
| 18 | } |
| 19 | if last == 1 { |
| 20 | return forms[0] |
| 21 | } |
| 22 | if last >= 2 && last <= 4 { |
| 23 | return forms[1] |
| 24 | } |
| 25 | return forms[2] |
| 26 | } |
| 27 | |
| 28 | fn format_thousands(n int) string { |
| 29 | s := n.str() |
| 30 | mut neg := false |
| 31 | mut digits := s |
| 32 | if s.starts_with('-') { |
| 33 | neg = true |
| 34 | digits = s[1..] |
| 35 | } |
| 36 | if digits.len <= 3 { |
| 37 | return s |
| 38 | } |
| 39 | mut out := '' |
| 40 | first := digits.len % 3 |
| 41 | if first > 0 { |
| 42 | out = digits[..first] |
| 43 | } |
| 44 | for i := first; i < digits.len; i += 3 { |
| 45 | if out.len > 0 { |
| 46 | out += ' ' |
| 47 | } |
| 48 | out += digits[i..i + 3] |
| 49 | } |
| 50 | return if neg { '-' + out } else { out } |
| 51 | } |
| 52 | |
| 53 | fn format_count(count int, key string, lang Lang) veb.RawHtml { |
| 54 | s := veb.tr(lang.str(), key).trim_space() |
| 55 | forms := s.split('|') |
| 56 | form := pick_plural_form(count, forms) |
| 57 | return veb.RawHtml('<b>${format_thousands(count)}</b> ${form}') |
| 58 | } |
| 59 | |
| 60 | fn (mut app App) format_commits_count(repo Repo, branch_name string, lang Lang) veb.RawHtml { |
| 61 | branch := app.find_repo_branch_by_name(repo.id, branch_name) |
| 62 | nr_commits := app.get_repo_commit_count(repo.id, branch.id) |
| 63 | |
| 64 | return format_count(nr_commits, 'commits_count', lang) |
| 65 | } |
| 66 | |
| 67 | fn (r &Repo) format_nr_branches(lang Lang) veb.RawHtml { |
| 68 | return format_count(r.nr_branches, 'branches_count', lang) |
| 69 | } |
| 70 | |
| 71 | fn (r &Repo) format_nr_tags(lang Lang) veb.RawHtml { |
| 72 | return format_count(r.nr_tags, 'tags_count', lang) |
| 73 | } |
| 74 | |
| 75 | fn (r &Repo) format_nr_open_prs(lang Lang) veb.RawHtml { |
| 76 | return format_count(r.nr_open_prs, 'prs_count', lang) |
| 77 | } |
| 78 | |
| 79 | fn (r &Repo) format_nr_open_issues(lang Lang) veb.RawHtml { |
| 80 | return format_count(r.nr_open_issues, 'issues_count', lang) |
| 81 | } |
| 82 | |
| 83 | fn (r &Repo) format_nr_contributors(lang Lang) veb.RawHtml { |
| 84 | return format_count(r.nr_contributors, 'contributors_count', lang) |
| 85 | } |
| 86 | |
| 87 | fn (r &Repo) format_nr_topics(lang Lang) veb.RawHtml { |
| 88 | return format_count(r.nr_topics, 'topics_count', lang) |
| 89 | } |
| 90 | |
| 91 | fn (r &Repo) format_nr_releases(lang Lang) veb.RawHtml { |
| 92 | return format_count(r.nr_releases, 'releases_count', lang) |
| 93 | } |
| 94 | |
| 95 | fn (r &Repo) format_nr_stars(lang Lang) veb.RawHtml { |
| 96 | return format_count(r.nr_stars, 'stars_count', lang) |
| 97 | } |
| 98 | |
| 99 | fn (mut app App) format_nr_watchers(repo_id int, lang Lang) veb.RawHtml { |
| 100 | return format_count(app.get_count_repo_watchers(repo_id), 'watchers_count', lang) |
| 101 | } |
| 102 | |
| 103 | fn (r &Repo) format_size() veb.RawHtml { |
| 104 | bytes := r.disk_size_bytes() |
| 105 | if bytes <= 0 { |
| 106 | return veb.RawHtml('') |
| 107 | } |
| 108 | num, unit := format_repo_size_parts(bytes) |
| 109 | return veb.RawHtml('<b>${num}</b> ${unit}') |
| 110 | } |
| 111 | |
| 112 | fn (r &Repo) disk_size_bytes() i64 { |
| 113 | if r.git_dir == '' { |
| 114 | return 0 |
| 115 | } |
| 116 | result := git.Git.exec_in_dir(r.git_dir, ['count-objects', '-v']) |
| 117 | if result.exit_code != 0 { |
| 118 | return 0 |
| 119 | } |
| 120 | mut total_kib := i64(0) |
| 121 | for line in result.output.split_into_lines() { |
| 122 | idx := line.index(':') or { continue } |
| 123 | key := line[..idx].trim_space() |
| 124 | if key != 'size' && key != 'size-pack' && key != 'size-garbage' { |
| 125 | continue |
| 126 | } |
| 127 | total_kib += line[idx + 1..].trim_space().i64() |
| 128 | } |
| 129 | return total_kib * 1024 |
| 130 | } |
| 131 | |
| 132 | fn format_repo_size_parts(bytes i64) (string, string) { |
| 133 | if bytes < 1024 { |
| 134 | return bytes.str(), 'B' |
| 135 | } |
| 136 | if bytes < i64(1024) * 1024 { |
| 137 | return (bytes / 1024).str(), 'KiB' |
| 138 | } |
| 139 | if bytes < i64(1024) * 1024 * 1024 { |
| 140 | return (bytes / (i64(1024) * 1024)).str(), 'MiB' |
| 141 | } |
| 142 | tenths := (bytes * 10) / (i64(1024) * 1024 * 1024) |
| 143 | return '${tenths / 10}.${tenths % 10}', 'GiB' |
| 144 | } |
| 145 | |