diff --git a/vlib/time/time.v b/vlib/time/time.v index d10bca4161..ed2c787a34 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -189,6 +189,32 @@ fn since(t Time) int { // relative returns a string representation of difference between time // and current time. pub fn (t Time) relative() string { + now := time.now() + secs := now.unix - t.unix + if secs <= 30 { + // right now or in the future + // TODO handle time in the future + return 'now' + } + if secs < 60 { + return '1m' + } + if secs < 3600 { + return '${secs/60} minutes ago' + } + if secs < 3600 * 24 { + return '${secs/3600} hours ago' + } + if secs < 3600 * 24 * 5 { + return '${secs/3600/24} days ago' + } + if secs > 3600 * 24 * 10000 { + return '' + } + return t.md() +} + +pub fn (t Time) relative_short() string { now := time.now() secs := now.unix - t.unix if secs <= 30 { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 0f1c02740c..f0877a7f02 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -571,7 +571,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) { ast.CharLiteral { f.write('`$it.val`') } - ast.ComptimeCall {} + ast.ComptimeCall { + if it.is_vweb { + f.write('$' + 'vweb.html()') + } + } ast.ConcatExpr { for i, val in it.vals { if i != 0 { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 9a00845982..3fe61e1b6a 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1369,7 +1369,7 @@ fn (mut p Parser) return_stmt() ast.Return { } } -const( +const ( // modules which allow globals by default global_enabled_mods = ['rand'] ) @@ -1377,8 +1377,8 @@ const( // left hand side of `=` or `:=` in `a,b,c := 1,2,3` fn (mut p Parser) global_decl() ast.GlobalDecl { if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v && - p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && - !p.pref.enable_globals && p.mod !in global_enabled_mods { + p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals && + !p.pref.is_fmt && p.mod !in global_enabled_mods { p.error('use `v --enable-globals ...` to enable globals') } start_pos := p.tok.position() diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index b746d09125..5c0d4ccd94 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -28,15 +28,15 @@ enum State { pub fn compile_template(html_, fn_name string) string { // lines := os.read_lines(path) - mut html := html_ + mut html := html_.trim_space() mut header := '' -if os.exists('templates/header.html') && html.contains('@header') { - h := os.read_file('templates/header.html') or { - panic('reading file templates/header.html failed') - } - header = h.replace("\'", '"') - html = header + html -} + if os.exists('templates/header.html') && html.contains('@header') { + h := os.read_file('templates/header.html') or { + panic('reading file templates/header.html failed') + } + header = h.trim_space().replace("\'", '"') + html = header + html + } mut lines := html.split_into_lines() mut s := strings.new_builder(1000)