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
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
- .bg(colour, string) ⇒ Object
- .bright(string) ⇒ Object
- .colour(fg, bg, string) ⇒ Object
- .config ⇒ Object
- .decorate(string, fg, bg, bright, underline) ⇒ Object
- .display_palette ⇒ Object
- .fg(colour, string) ⇒ Object
- .get_style(style_name) ⇒ Object
- .parse(string, default_style) ⇒ Object
- .reconfigure(config) ⇒ Object
- .style(style_name, string) ⇒ Object
- .underline(string) ⇒ Object
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 |
.config ⇒ Object
88 89 90 |
# File 'lib/tco.rb', line 88 def self.config @config 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_palette ⇒ Object
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 |
# File 'lib/tco.rb', line 97 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 black = Tco::Colour.new([0,0,0]) white = Tco::Colour.new([255,255,255]) font_colour = if (colours[c] - black).abs > (colours[c] - white).abs black else white end square_styles.push [c, font_colour, 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 |
.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] style.bg = seg.params[:bg] style.bright = seg.params[:bright] style.underline = seg.params[:underline] output << decorate(seg.to_s, style) end output end |
.reconfigure(config) ⇒ Object
92 93 94 95 |
# File 'lib/tco.rb', line 92 def self.reconfigure(config) @config = config @colouring = Colouring.new config 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 |