Module: Tco

Defined in:
lib/tco/style.rb,
lib/tco.rb,
lib/tco/config.rb,
lib/tco/parser.rb,
lib/tco/palette.rb,
lib/tco/version.rb,
lib/tco/colouring.rb

Overview

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Classes: Colour, Colouring, Config, Palette, Parser, Segment, Style, Token, Unknown

Constant Summary collapse

VERSION =
"0.1.8"

Class Method Summary collapse

Class Method Details

.bg(colour, string) ⇒ Object



42
43
44
# File 'lib/tco.rb', line 42

def self.bg(colour, string)
  decorate string, Style.new(nil, colour)
end

.bright(string) ⇒ Object



46
47
48
# File 'lib/tco.rb', line 46

def self.bright(string)
  decorate string, Style.new(nil, nil, true, false)
end

.colour(fg, bg, string) ⇒ Object



34
35
36
# File 'lib/tco.rb', line 34

def self.colour(fg, bg, string)
  decorate string, Style.new(fg, bg)
end

.configObject



88
89
90
# File 'lib/tco.rb', line 88

def self.config
  @config
end

.configure {|c| ... } ⇒ Object

Yields:

  • (c)


92
93
94
95
96
97
# File 'lib/tco.rb', line 92

def self.configure
  c = config
  yield(c)
  reconfigure(c)
  c
end

.decorate(string, fg, bg, bright, underline) ⇒ Object



84
85
86
# File 'lib/tco.rb', line 84

def self.decorate(string, (fg, bg, bright, underline))
  @colouring.decorate string, [fg, bg, bright, underline]
end

.display_paletteObject



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
# File 'lib/tco.rb', line 122

def self.display_palette
  # TODO: Might be worth sorting, so the pallete is easier to navigate
  colours = @colouring.palette.colours
  colours_per_line = (`tput cols`.to_i / 9 - 0.5).ceil

  c = 0
  while c < colours.length do
    if (c + colours_per_line) < colours.length
      squares = colours_per_line
    else
      squares = colours.length - c
    end

    # Prepare the squares for the line
    square_styles = []
    squares.times do
      square_styles.push [c, @colouring.get_best_font_colour(colours[c]), colours[c]]
      c += 1
    end

    # The first empty line
    square_styles.each { |c, fg, bg| print Tco::colour fg, bg, " "*9 }
    puts

    # Colour index
    square_styles.each do |c, fg, bg|
      print Tco::colour fg, bg, c.to_s.center(9)
    end
    puts

    # Colour value
    square_styles.each do |c, fg, bg|
      print Tco::colour fg, bg, bg.to_s.center(9)
    end
    puts

    # Final empty line
    square_styles.each { |c, fg, bg| print Tco::colour fg, bg, " "*9 }
    puts
  end
end

.fg(colour, string) ⇒ Object



38
39
40
# File 'lib/tco.rb', line 38

def self.fg(colour, string)
  decorate string, Style.new(colour)
end

.get_style(style_name) ⇒ Object



58
59
60
# File 'lib/tco.rb', line 58

def self.get_style(style_name)
  @colouring.get_style style_name
end

.match_colour(colour_spec) ⇒ Object



104
105
106
107
# File 'lib/tco.rb', line 104

def self.match_colour(colour_spec)
  colour = @colouring.get_colour_instance colour_spec
  @colouring.palette.match_colour colour
end

.parse(string, default_style) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tco.rb', line 62

def self.parse(string, default_style)
  p = Parser.new default_style
  segments = p.parse string

  output = ""
  segments.each do |seg|
    style = if seg.params[:base_style]
              @colouring.get_style seg.params[:base_style]
            else
              Style.new
            end

    style.fg = seg.params[:fg] if seg.params[:fg]
    style.bg = seg.params[:bg] if seg.params[:bg]
    style.bright = seg.params[:bright] if seg.params[:bright]
    style.underline = seg.params[:underline] if seg.params[:underline]

    output << decorate(seg.to_s, style)
  end
  output
end

.reconfigure(config) ⇒ Object



99
100
101
102
# File 'lib/tco.rb', line 99

def self.reconfigure(config)
  @config = config
  @colouring = Colouring.new config
end

.show_matching_colour(colour_spec) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/tco.rb', line 109

def self.show_matching_colour(colour_spec)
  colour = @colouring.get_colour_instance colour_spec
  colour_index = @colouring.palette.match_colour colour

  fg = @colouring.get_best_font_colour colour
  bg = @colouring.palette.colours[colour_index]

  puts Tco::colour fg, bg, " "*9
  puts Tco::colour fg, bg, colour_index.to_s.center(9)
  puts Tco::colour fg, bg, bg.to_s.center(9)
  puts Tco::colour fg, bg, " "*9
end

.style(style_name, string) ⇒ Object



54
55
56
# File 'lib/tco.rb', line 54

def self.style(style_name, string)
  @colouring.decorate(string, @colouring.get_style(style_name))
end

.underline(string) ⇒ Object



50
51
52
# File 'lib/tco.rb', line 50

def self.underline(string)
  decorate string, Style.new(nil, nil, false, true)
end