package liquidsoap-lang

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file console.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
(*****************************************************************************

  Liquidsoap, a programmable stream generator.
  Copyright 2003-2024 Savonet team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details, fully stated in the COPYING
  file at the root of the liquidsoap distribution.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

 *****************************************************************************)

(* Some of the code below was borrowed from opam. *)

let dumb_term =
  lazy (try Sys.getenv "TERM" = "dumb" with Not_found -> Sys.win32)

type color_conf = [ `Always | `Never | `Auto ]

let color_conf : color_conf ref = ref `Auto

let color =
  let auto =
    lazy
      (try Unix.isatty Unix.stdout && not (Lazy.force dumb_term)
       with _ -> false)
  in
  fun () ->
    match !color_conf with
      | `Always -> true
      | `Never -> false
      | `Auto -> Lazy.force auto

type text_style =
  [ `bold
  | `underline
  | `crossed
  | `black
  | `red
  | `green
  | `yellow
  | `blue
  | `magenta
  | `cyan
  | `white ]

let style_code (c : text_style) =
  match c with
    | `bold -> "01"
    | `underline -> "04"
    | `crossed -> "09"
    | `black -> "30"
    | `red -> "31"
    | `green -> "32"
    | `yellow -> "33"
    | `blue -> "1;34" (* most terminals make blue unreadable unless bold *)
    | `magenta -> "35"
    | `cyan -> "36"
    | `white -> "37"

let colorize styles s =
  if not (color ()) then s
  else
    Printf.sprintf "\027[%sm%s\027[0m"
      (String.concat ";" (List.map style_code styles))
      s

let start_color styles =
  if not (color ()) then ""
  else
    Printf.sprintf "\027[%sm" (String.concat ";" (List.map style_code styles))

let stop_color () = if not (color ()) then "" else "\027[0m"
OCaml

Innovation. Community. Security.