Module: SeccompTools::Util

Defined in:
lib/seccomp-tools/util.rb

Overview

Define utility methods.

Constant Summary collapse

LIGHT_YELLOW =

color code of light yellow

"\e[38;5;230m"
COLOR_CODE =

Color codes for pretty print.

{
  esc_m: "\e[0m",
  syscall: "\e[38;5;120m", # light green
  arch: LIGHT_YELLOW,
  args: LIGHT_YELLOW,
  gray: "\e[2m",
  error: "\e[38;5;196m" # heavy red
}.freeze

Class Method Summary collapse

Class Method Details

.colorize(s, t: nil) ⇒ String

Wrapper color codes.

Parameters:

  • s (String)

    Contents to wrapper.

  • t (Symbol?) (defaults to: nil)

    Specific which kind of color to use, valid symbols are defined in COLOR_CODE.

Returns:

  • (String)

    Wrapper with color codes.



65
66
67
68
69
70
71
72
# File 'lib/seccomp-tools/util.rb', line 65

def colorize(s, t: nil)
  s = s.to_s
  return s unless colorize_enabled?

  cc = COLOR_CODE
  color = cc[t]
  "#{color}#{s.sub(cc[:esc_m], cc[:esc_m] + color)}#{cc[:esc_m]}"
end

.colorize_enabled?Boolean

Is colorize enabled?

Returns:

  • (Boolean)


43
44
45
# File 'lib/seccomp-tools/util.rb', line 43

def colorize_enabled?
  !@disable_color && $stdout.tty?
end

.disable_color!void

This method returns an undefined value.

Disable colorize.



37
38
39
# File 'lib/seccomp-tools/util.rb', line 37

def disable_color!
  @disable_color = true
end

.enable_color!void

This method returns an undefined value.

Enable colorize.



31
32
33
# File 'lib/seccomp-tools/util.rb', line 31

def enable_color!
  @disable_color = false
end

.supported_archsArray<Symbol>

Get currently supported architectures.

Returns:

  • (Array<Symbol>)

    Architectures.



11
12
13
14
15
# File 'lib/seccomp-tools/util.rb', line 11

def supported_archs
  @supported_archs ||= Dir.glob(File.join(__dir__, 'consts', 'sys_nr', '*.rb'))
                          .map { |f| File.basename(f, '.rb').to_sym }
                          .sort
end

.system_archSymbol

Detect system architecture.

Returns:

  • (Symbol)


19
20
21
22
23
24
25
26
27
# File 'lib/seccomp-tools/util.rb', line 19

def system_arch
  case RbConfig::CONFIG['host_cpu']
  when /x86_64/ then :amd64
  when /i386/ then :i386
  when /aarch64/ then :aarch64
  when /s390x/ then :s390x
  else :unknown
  end
end

.template(filename) ⇒ String

Get content of filename under directory templates/.

Parameters:

  • filename (String)

    The filename.

Returns:

  • (String)

    Content of the file.



81
82
83
# File 'lib/seccomp-tools/util.rb', line 81

def template(filename)
  File.binread(File.join(__dir__, 'templates', filename))
end