Source file b_theme.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
open B_utils
module Utf8 = B_utf8
let this_version = "20250815"
let default_vars = [
"DEBUG", "false";
"DEBUG_CODE", "1023";
"LOG_TO_FILE", "false";
"DIR", "";
"THEME", "default";
"BACKGROUND", "color:white";
"BG_COLOR", "aliceblue";
"BOX_BG_COLOR", "lightsteelblue";
"BUTTON_COLOR_ON", "darkturquoise";
"BUTTON_COLOR_OFF", "#8bc0d1";
"CHECK_ON", "fa:check-square-o";
"CHECK_OFF", "fa:square-o";
"CURSOR_COLOR", "#2a7da2";
"DISABLED_BG", "lightgray";
"DISABLED_FG", "gray";
"FAINT_COLOR", "gainsboro";
"TEXT_COLOR", "black";
"SEL_BG_COLOR", "slategray";
"SEL_FG_COLOR", "white";
"LABEL_COLOR", "black";
"MENU_HL_COLOR", "#8099a2";
"MENU_BG_COLOR", "#BEBEBE";
"LABEL_FONT_SIZE", "14";
"LABEL_FONT", "Ubuntu-R.ttf";
"TEXT_FONT", "Ubuntu-R.ttf";
"TEXT_FONT_SIZE", "14";
"SMALL_FONT_SIZE", "10";
"MONO_FONT", "UbuntuMono-R.ttf";
"ROOM_MARGIN", "10";
"FA_DIR", "font-awesome-4.6.3";
"INT_SCALE", "false";
"SCALE", "0";
"OPENGL_MULTISAMPLE", "false";
"NO_VSYNC", "false";
"NATURAL_SCROLLING", "auto";
"USE_FSWATCH", "true"
]
let id x = x
let (//) = Filename.concat
let xdg = Xdg.create ~env:Sys.getenv_opt ()
let home = Xdg.home_dir xdg
let conf =
let application = "bogue" in
let config_dir = Xdg.config_dir xdg in
config_dir // application
let buffer =
let rec loop () =
let _ = Scanf.bscanf buffer " #%s@\n" id in
loop () in
try loop () with
| End_of_file
| Scanf.Scan_failure _ -> ()
| e -> print_endline "SCAN ERROR"; raise e
let load_vars config_file =
let buffer = Scanf.Scanning.from_file config_file in
let version = try
Scanf.bscanf buffer "## BOGUE version %s " id
with e -> raise e in
printd debug_io "Reading config file [%s]. BOGUE Version [%s]"
config_file version;
if version > this_version
then printd debug_warning
"The version indicated in the config file (%s) is more recent than your \
version of Bogue (%s)" version this_version;
let rec loop list =
skip_comment buffer;
try
let varname, value = Scanf.bscanf buffer " %[^=] = %[^\n]"
(fun a b -> String.trim a, String.trim b) in
printd debug_io "Reading %s=%s" varname value;
if varname = "DEBUG"
then debug := (String.lowercase_ascii value = "true");
loop ((varname, value) :: list)
with
| End_of_file -> Scanf.Scanning.close_in buffer; list
| e -> raise e
in
loop []
let load_vars config_file =
if Sys.file_exists config_file then load_vars config_file
else begin
printd (debug_error + debug_io) "Config file [%s] does not exists." config_file;
[]
end
let conf_file = "bogue.conf"
let all_conf_files () = [
conf_file;
home // ("." ^ conf_file);
conf // conf_file
]
let user_vars =
List.map (fun file ->
if Sys.file_exists file then
try load_vars file with
| _ ->
printd (debug_error + debug_io) "Error loading config file %s." file; []
else (printd debug_io "Config file %s not found." file; []))
(all_conf_files ())
|> List.flatten
let user_vars = ref user_vars
let get_var s =
let v = try Sys.getenv ("BOGUE_" ^ s) with
| Not_found ->
match List.assoc_opt s !user_vars with
| Some v -> v
| None -> begin
printd debug_warning
"User variable '%s' not found in config." s;
match List.assoc_opt s default_vars with
| Some v -> v
| None ->
printd debug_error "Variable '%s' not found. Prepare for a crash." s;
""
end in
printd debug_warning "Using %s=%s" s v;
v
let get_int ?(default = 0) s =
let v = get_var s in
try int_of_string v with
| Failure _ ->
printd debug_error
"Expected an integer for '%s', got '%s' instead. Using default=%d."
s v default;
default
| e -> raise e
let get_float ?(default = 0.) s =
let v = get_var s in
try float_of_string v with
| Failure _ ->
printd debug_error "Expected a float for '%s', got '%s' instead. Using default=%f." s v default;
default
| e -> raise e
let get_bool s =
let b = get_var s in
String.lowercase_ascii b = "true" || b = "1"
let dir_exists d = Sys.file_exists d && Sys.is_directory d
let rev_insert_theme_vars theme_path vars rest =
let theme_vars = load_vars theme_path in
List.rev_append vars (List.append theme_vars rest)
let chk_theme_dir th_dir =
dir_exists th_dir || begin
printd (debug_error + debug_io) "Theme directory [%s] does not exist." th_dir;
false
end
let load_theme_vars dir vars =
let rec loop newv = function
| [] ->
let value0 = get_var "THEME" in
let value = if chk_theme_dir (dir // value0) then value0
else if value0 = "default" then failwith "Cannot find default theme."
else "default" in
printd debug_io "No theme specified, using default=%s." value;
if value <> value0 then begin
print "Bogue Warning: Using theme [%s] because [%s] could not be loaded."
value value0;
Unix.putenv "BOGUE_THEME" value
end;
loop newv ["THEME", value]
| (name, value)::rest ->
if name = "THEME"
then
if chk_theme_dir (dir // value)
then let theme_path = dir // value // conf_file in
(name, value) :: (rev_insert_theme_vars theme_path newv rest)
else loop newv rest
else loop ((name, value)::newv) rest
in
loop [] vars
let () =
debug := get_bool "DEBUG";
debug_code := get_int "DEBUG_CODE";
if get_bool "LOG_TO_FILE"
then begin
let log_file = Filename.temp_file "bogue" ".log" in
Printf.printf "Bogue - Logging to file %s\n" log_file;
log_channel := open_out log_file
end
let download_conf () =
let open Printf in
let rescue = "bogue_conf.tgz" in
let cwd = Sys.getcwd () in
let tmpdir = Filename.temp_file "bogue" "" in
Sys.remove tmpdir;
Unix.mkdir tmpdir 0o777;
Sys.chdir tmpdir;
if Sys.command (sprintf "wget https://raw.githubusercontent.com/sanette/bogue/master/%s" rescue) <> 0
then failwith "Cannot download rescue config. Aborting."
else if Sys.command (sprintf "tar xvfz %s" rescue) <> 0
then failwith "Cannot extract rescue config. Aborting."
else if Sys.command (sprintf "mkdir -p %s" conf) <> 0
then failwith "Cannot create config dir. Aborting."
else if Sys.command (sprintf "cp -r bogue/* %s/" conf) <> 0
then failwith "Cannot copy config files. Aborting"
else print "Minimal config downloaded to %s." conf;
Sys.chdir cwd;
sprintf "%s/themes" conf
let find_share prog file =
let cwd = Sys.getcwd () in
let queue = Queue.create () in
let bin_dir = Filename.dirname Sys.executable_name in
let prefix_dir = Sys.chdir bin_dir; Sys.chdir ".."; Sys.getcwd () in
let b = Filename.basename bin_dir in
if b = "bin"
then Queue.add (prefix_dir // "share" // prog) queue;
if Filename.basename b = "bin"
then Queue.add (prefix_dir // "share") queue;
let () =
let opam = "opam var share" in
try
printd debug_io "Invoking opam";
let system = Unix.open_process_in opam in
let res = input_line system in
Unix.close_process_in system |> ignore;
Queue.add (res // prog) queue;
Queue.add (res // "bogue") queue;
with _ -> printd debug_io "Cannot execute '%s'" opam in
Sys.chdir cwd;
match Queue.fold (fun list path ->
if Sys.file_exists (path // file)
then List.cons path list else list) [] queue with
| [] -> printd debug_error "Cannot find share directory for %s/%s! bin_dir=%s, prefix_dir=%s"
prog file bin_dir prefix_dir; None
| path :: _ -> Some path
let app_share_dir = match Filename.basename Sys.executable_name with
| "ocaml" | "utop" | "utop.exe" -> None
| app -> printd debug_io "Executable=%s" app; find_share app "."
let dir =
let dir = get_var "DIR" in
if dir_exists dir then dir
else let config = conf // "themes" in
if dir_exists config
then if dir = ""
then begin
printd debug_warning "Using [%s] as bogue themes directory" config;
config
end else begin
printd debug_error
"Bogue themes directory [%s] does not exist. Using [%s] instead" dir config;
config
end
else try
let system = Unix.open_process_in "ocamlfind query bogue" in
let res = input_line system in
match Unix.close_process_in system with
| Unix.WEXITED 0 ->
let res = Filename.(dirname @@ dirname res) in
let dir = Printf.sprintf "%s/share/bogue/themes" res in
if dir_exists dir then dir else begin
printd debug_error "Cannot find themes directory";
raise Not_found
end
| _ ->
printd debug_error
"Cannot find a usable bogue configuration directory";
raise Not_found
with
| Not_found
| End_of_file ->
printd debug_error
"(FATAL) Bogue configuration directory [%s] does not exist, and \
system-wide config cannot be found." dir;
print_endline "Cannot find bogue lib directory";
print_endline "Trying to download a minimal Bogue config...";
download_conf ()
| e -> raise e
let () = user_vars := load_theme_vars dir !user_vars
let current = dir // (get_var "THEME")
let () = print "Loading Bogue %s with config dir %s" this_version current
let common = dir // "common"
let fonts_dir = common // "fonts"
let load_path = List.fold_left (fun l -> function
| Some p -> p::l
| None -> l) [] [app_share_dir; Some ""; Some current; Some common]
let get_path ?(path = load_path) file =
if file = "" then invalid_arg "[Theme.get_path]: filename empty";
if file.[0] = '/' then file
else if file.[0] = '%'
then let file = String.sub file 1 (String.length file - 1) in
((Filename.dirname dir) // file)
else let rec loop = function
| [] ->
printd (debug_error + debug_io)
"File %S not found in path: %s (current dir=%s)"
file (String.concat ":" path) (Sys.getcwd ());
file
| dir::rest ->
let p = dir // file in
if Sys.file_exists p then p else loop rest in
loop path
let get_fa_or_path s =
if startswith s "fa:" then s else get_path s
let get_font_path name =
if name = "" then failwith "Empty fontname";
if name.[0] = '/' then name
else let check_file file fail =
if Sys.file_exists file then file else fail () in
check_file name (fun () ->
check_file (fonts_dir // name) (fun () ->
let fclist = Unix.open_process_in
(Printf.sprintf "fc-list : file | grep %s" name) in
try
let res = input_line fclist in
match Unix.close_process_in fclist with
| Unix.WEXITED 0 ->
String.sub res 0 (String.rindex res ':')
| _ -> printd debug_error "Cannot find font %s" name; name
with End_of_file -> printd debug_error "Cannot find font %s" name; name
)
)
let get_font_path_opt name =
let file = get_font_path name in
if Sys.file_exists file then Some file else None
let background = get_var "BACKGROUND"
let bg_color = get_var "BG_COLOR"
let box_bg_color = get_var "BOX_BG_COLOR"
let button_color_off = get_var "BUTTON_COLOR_OFF"
let button_color_on = get_var "BUTTON_COLOR_ON"
let check_on = get_fa_or_path (get_var "CHECK_ON")
let check_off = get_fa_or_path (get_var "CHECK_OFF")
let cursor_color = get_var "CURSOR_COLOR"
let disabled_fg = get_var "DISABLED_FG"
let disabled_bg = get_var "DISABLED_BG"
let faint_color = get_var "FAINT_COLOR"
let text_color = get_var "TEXT_COLOR"
let sel_bg_color = get_var "SEL_BG_COLOR"
let sel_fg_color = get_var "SEL_FG_COLOR"
let label_color = get_var "LABEL_COLOR"
let = get_var "MENU_HL_COLOR"
let = get_var "MENU_BG_COLOR"
let label_font_size = get_int ~default:14 "LABEL_FONT_SIZE"
let label_font = ref (get_font_path (get_var "LABEL_FONT"))
let text_font = ref (get_font_path (get_var "TEXT_FONT"))
let text_font_size = get_int ~default:14 "TEXT_FONT_SIZE"
let small_font_size = get_int ~default:10 "SMALL_FONT_SIZE"
let mono_font = get_font_path (get_var "MONO_FONT")
let room_margin = get_int ~default:10 "ROOM_MARGIN"
let fa_dir = common // (get_var "FA_DIR")
let fa_font = fa_dir // "fonts/fontawesome-webfont.ttf"
let int_scale = ref (get_bool "INT_SCALE")
let scale = ref (get_float ~default:0. "SCALE")
let opengl_multisample = get_bool "OPENGL_MULTISAMPLE"
let fa_font_size = 18
let no_vsync = get_bool "NO_VSYNC"
let natural_scrolling =
if String.lowercase_ascii (get_var "NATURAL_SCROLLING") = "auto"
then os_type () = "Darwin" else get_bool "NATURAL_SCROLLING"
let mouse_wheel_scale = ref (if natural_scrolling then -1. else 1.)
let use_fswatch = get_bool "USE_FSWATCH"
(** some standard (?) UTF8 symbols *)
let symbols = [
"check_empty", "\239\130\150";
"check", "\239\129\134";
]
let set var x = var := x
let set_text_font f = text_font := get_font_path f
let set_label_font f = label_font := get_font_path f
let set_scale s =
let s = if !int_scale then Float.floor s else s in
scale := s
let set_int_scale b =
int_scale := b;
if b then set_scale !scale
let set_integer_scale = set_int_scale
let scale_was_init () = !scale <> 0.
let scale_int i =
round (!scale *. float i)
let unscale_int i =
round (float i /. !scale)
let unscale_f x =
x /. !scale
let scale_from_float x =
round (!scale *. x)
let unscale_to_float i =
!scale *. (float i)
(** Font Awesome variables *)
let load_fa_variables () =
let file = fa_dir // "less" // "variables.less" in
let buffer = Scanf.Scanning.from_file file in
let rec loop list =
try
let key = Scanf.bscanf buffer "@fa-var-%s@: " (fun x -> x) in
let value = Scanf.bscanf buffer "\"\\%s@\"" (fun x -> x) in
let int_value = int_of_string ("0x" ^ value) in
let ucode = Bytes.to_string (Utf8.uencode int_value) in
loop ((key, ucode) :: list)
with
| End_of_file ->
Scanf.Scanning.close_in buffer;
list
| Scanf.Scan_failure _ ->
ignore (Scanf.bscanf buffer "%s@\n" (fun x -> x));
loop list
| e -> raise e
in
loop []
let fa_symbols = load_fa_variables ()
let fa_symbol name =
match List.assoc_opt name fa_symbols with
| Some s -> s
| None ->
printd debug_error "FA symbol '%s' was not found. Using 'question' instead" name;
List.assoc "question" fa_symbols
let load_colors () =
let file = common // "colors" // "liste.txt" in
printd debug_io "Reading color names from [%s]." file;
let buffer = Scanf.Scanning.from_file file in
let rec loop list =
try
let name,_,r,g,b = Scanf.bscanf buffer "%s #%x rgb(%u,%u,%u)\n" (fun n h r g b -> n,h,r,g,b) in
loop ( (name,(r,g,b))::list )
with
| End_of_file ->
Scanf.Scanning.close_in buffer;
list
| Scanf.Scan_failure _ ->
let fail = Scanf.bscanf buffer "%s@\n" (fun x -> x) in
printd (debug_error+debug_io) "Fail to read [%s]" fail;
loop list
| e -> raise e
in
loop []
let color_names = load_colors ()
let print_bin c =
let rec loop code list =
if code = 0 then list
else loop (code lsr 1) ((string_of_int (code land 1)) :: list)
in
if c = 0 then "0" else String.concat "" (loop c [])
let () = printd debug_custom "Detected os type: [%s]" (os_type ())