Module: NA::Theme

Defined in:
lib/na/theme.rb

Overview

Provides theme and color template helpers for todo CLI output.

Examples:

Get theme help text

NA::Theme.template_help

Class Method Summary collapse

Class Method Details

.load_theme(template: {}) ⇒ Hash

Loads the theme configuration, merging defaults with any custom theme file and the provided template. Writes the help text and theme YAML to the theme file.

Examples:

NA::Theme.load_theme(template: { action: '{r}' })

Parameters:

  • template (Hash) (defaults to: {})

    Additional theme settings to merge

Returns:

  • (Hash)

    The merged theme configuration



39
40
41
42
43
44
45
46
47
# File 'lib/na/theme.rb', line 39

def load_theme(template: {})
  if defined?(NA::Benchmark) && NA::Benchmark
    NA::Benchmark.measure('Theme.load_theme') do
      load_theme_internal(template: template)
    end
  else
    load_theme_internal(template: template)
  end
end

.load_theme_internal(template: {}) ⇒ Object



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
# File 'lib/na/theme.rb', line 49

def load_theme_internal(template: {})
  # Default colorization, can be overridden with full or partial template variable
  default_template = {
    parent: '{c}',
    bracket: '{dc}',
    parent_divider: '{xw}/',
    action: '{bg}',
    project: '{xbk}',
    tags: '{m}',
    value_parens: '{m}',
    values: '{c}',
    duration: '{y}',
    search_highlight: '{y}',
    note: '{dw}',
    dirname: '{xdw}',
    filename: '{xb}{#eccc87}',
    line: '{dw}',
    prompt: '{m}',
    success: '{bg}',
    error: '{b}{#b61d2a}',
    warning: '{by}',
    debug: '{dw}',
    templates: {
      output: '%filename%line%parents| %action',
      default: '%parents %line %action',
      single_file: '%parents %line %action',
      multi_file: '%filename%line%parents %action',
      no_file: '%parents %line %action'
    }
  }

  # Load custom theme
  theme_file = NA.database_path(file: 'theme.yaml')
  theme = if File.exist?(theme_file)
            YAML.load(File.read(theme_file)) || {}
          else
            {}
          end
  theme = default_template.deep_merge(theme)

  File.open(theme_file, 'w') do |f|
    f.puts template_help.comment
    f.puts YAML.dump(theme)
  end

  theme.merge(template)
end

.template_helpString

Returns a help string describing available color placeholders for themes.

Examples:

NA::Theme.template_help

Returns:

  • (String)

    Help text for theme placeholders



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/na/theme.rb', line 14

def template_help
  <<~EOHELP
    Use {X} placeholders to apply colors. Available colors are:

    w: white, k: black, g: green, l: blue,
    y: yellow, c: cyan, m: magenta, r: red,
    W: bgwhite, K: bgblack, G: bggreen, L: bgblue,
    Y: bgyellow, C: bgcyan, M: bgmagenta, R: bgred,
    d: dark, b: bold, u: underline, i: italic, x: reset

    Multiple placeholders can be combined in a single {} pair.

    You can also use {#RGB} and {#RRGGBB} to specify hex colors.
    Add a b before the # to make the hex a background color ({b#fa0}).


  EOHELP
end