2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
23 fn shell_expand_prompt(w : world, ro : acmd_ro, p : bytes, d : bytes) : (world, string);
27 fn date_d(implicit w : world, implicit ro : acmd_ro) : (world, string)
29 var time := get_real_time();
30 var c := time_to_calendar(ro.tz, time);
31 return weekdays[c.wday] + ` ` + months[c.month] + ` ` + locale_to_string(ro.loc, ntos(c.day + 1));
34 fn time_t(implicit w : world, implicit ro : acmd_ro, h24 sec : bool) : (world, string)
36 var time := get_real_time();
37 var c := time_to_calendar(ro.tz, time);
40 result += list_left_pad(locale_to_string(ro.loc, ntos(c.hour)), 2, '0');
42 result += list_left_pad(locale_to_string(ro.loc, ntos((c.hour + 11) mod 12 + 1)), 2, '0');
43 result += `:` + list_left_pad(locale_to_string(ro.loc, ntos(c.min)), 2, '0');
45 result += `:` + list_left_pad(locale_to_string(ro.loc, ntos(c.sec)), 2, '0');
50 fn shell_expand_prompt(implicit w : world, implicit ro : acmd_ro, p : bytes, d : bytes) : (world, string)
53 var ps := locale_to_string(ro.loc, p);
54 for i := 0 to len(ps) do [
55 if ps[i] = '\', i < len(ps) - 1 then [
58 ] else if c = 'd' then [
61 ] else if c = 'e' then [
62 ] else if c = 'h' or c = 'H' then [
63 var h := get_host_name();
65 var e := treemap_search(ro.env, "HOSTNAME");
70 var l := list_search(h, '.');
74 result += locale_to_string(ro.loc, h);
75 ] else if c = 't' then [
76 var t := time_t(true, true);
78 ] else if c = 'T' then [
79 var t := time_t(false, true);
81 ] else if c = '@' then [
82 var t := time_t(false, false);
84 ] else if c = 'A' then [
85 var t := time_t(true, false);
87 ] else if c = 'u' then [
88 var e := treemap_search(ro.env, "HOSTNAME");
90 result += locale_to_string(ro.loc, e.j);
92 ] else if c = 'w' or c = 'W' then [
93 var w := path_shortcut_home(ro.home, d);
95 var p := list_search_backwards_fn(w, path_is_separator);
99 result += locale_to_string(ro.loc, w);
100 ] else if c = '$' then [
101 var uid_s := treemap_search(ro.env, "UID");
102 var uid := ston(uid_s.j);
103 if not is_exception uid, uid = 0 then
107 ] else if c = '\' then [
109 ] else if c = '[' then [
110 while i < len(ps) - 1, not(ps[i] = '\' and ps[i + 1] = ']') do
112 ] else if c >= '0', c <= '7' then [
114 if i + 1 < len(ps), ps[i + 1] >= '0', ps[i + 1] <= '7' then [
115 oct := 8 * oct + (ps[i + 1] - '0');
118 if i + 1 < len(ps), ps[i + 1] >= '0', ps[i + 1] <= '7' then [
119 oct := 8 * oct + (ps[i + 1] - '0');