ggdgsdbsdbbb / utils.v
68 lines · 53 sloc · 1.54 KB · bed64285555e6c9bf5ebf34b7538e1da67e926b2
Raw
1module main
2
3import time
4import math
5import os
6import veb
7
8pub fn (mut app App) running_since() string {
9 duration := time.now().unix() - app.started_at
10
11 seconds_in_hour := 60 * 60
12
13 days := int(math.floor(f64(duration) / f64(seconds_in_hour * 24)))
14 hours := int(math.fmod(math.floor(f64(duration) / f64(seconds_in_hour)), 24))
15 minutes := int(math.floor(f64(duration) / 60.0)) % 60
16 seconds := duration % 60
17
18 return '${days} days ${hours} hours ${minutes} minutes and ${seconds} seconds'
19}
20
21pub fn (mut ctx Context) make_path(branch_name string, i int) string {
22 if i == 0 {
23 return ctx.path_split[..i + 1].join('/')
24 }
25 mut s := ctx.path_split[0]
26 s += '/tree/${branch_name}/'
27 s += ctx.path_split[1..i + 1].join('/')
28 return s
29}
30
31fn create_directory_if_not_exists(path string) {
32 if !os.exists(path) {
33 os.mkdir(path) or { panic('cannot create ${path} directory') }
34 }
35}
36
37fn calculate_pages(count int, per_page int) int {
38 if count == 0 {
39 return 0
40 }
41
42 return int(math.ceil(f32(count) / f32(per_page))) - 1
43}
44
45fn generate_prev_next_pages(page int) (int, int) {
46 prev_page := if page > 0 { page - 1 } else { 0 }
47 next_page := page + 1
48
49 return prev_page, next_page
50}
51
52fn check_first_page(page int) bool {
53 return page == 0
54}
55
56fn check_last_page(total int, offset int, per_page int) bool {
57 return (total - offset) < per_page
58}
59
60const is_dev = true
61
62fn css2(s string) veb.RawHtml {
63 if is_dev {
64 return '<link href="http://localhost:8000/${s}" rel="stylesheet" type="text/css">'
65 } else {
66 return '<link href="/static/${s}" rel="stylesheet" type="text/css">'
67 }
68}
69