Class: Daigaku::Window

Inherits:
Curses::Window
  • Object
show all
Defined in:
lib/daigaku/window.rb

Constant Summary collapse

COLOR_TEXT =
Curses::COLOR_YELLOW
COLOR_TEXT_EMPHASIZE =
Curses::COLOR_CYAN
COLOR_HEADING =
Curses::COLOR_WHITE
COLOR_RED =
Curses::COLOR_BLUE
COLOR_GREEN =
Curses::COLOR_MAGENTA
COLOR_YELLOW =
Curses::COLOR_RED
BACKGROUND =
Curses::COLOR_WHITE
FONT =
Curses::COLOR_BLACK
FONT_HEADING =
Curses::COLOR_MAGENTA
FONT_EMPHASIZE =
Curses::COLOR_BLUE
RED =
Curses::COLOR_RED
GREEN =
Curses::COLOR_GREEN
YELLOW =
Curses::COLOR_YELLOW

Instance Method Summary collapse

Constructor Details

#initialize(height = Curses.lines, width = Curses.cols, top = 0, left = 0) ⇒ Window

Returns a new instance of Window.



22
23
24
25
# File 'lib/daigaku/window.rb', line 22

def initialize(height = Curses.lines, width = Curses.cols, top = 0, left = 0)
  super(height, width, top, left)
  init_colors
end

Instance Method Details

#clear_line(options = {}) ⇒ Object

clear_line(options = {}) options: [:color, :text_decoration, :start_pos, :end_pos]



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/daigaku/window.rb', line 68

def clear_line(options = {})
  color = options[:color] || COLOR_TEXT
  text_decoration = options[:text_decoration] || Curses::A_NORMAL
  start = options[:start_pos] || 0
  stop = options[:end_pos] || maxx

  x = curx
  setpos(cury, start)
  write((options[:text] || ' ') * (stop - 1), color, text_decoration)
  setpos(cury, x)
  refresh
end

#colored(text, color, text_decoration = Curses::A_NORMAL, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/daigaku/window.rb', line 51

def colored(text, color, text_decoration = Curses::A_NORMAL, options = {})
  if options[:full_line]
    clear_line(
      color: color,
      text_decoration: Curses::A_STANDOUT,
      start_pos: 1,
      end_pos: maxx - 2
    )

    prefix = ' '
  end

  write("#{prefix}#{text}", color, text_decoration)
end

#emphasize(text, text_decoration = Curses::A_NORMAL) ⇒ Object



31
32
33
# File 'lib/daigaku/window.rb', line 31

def emphasize(text, text_decoration = Curses::A_NORMAL)
  write(text, Window::COLOR_TEXT_EMPHASIZE, text_decoration)
end

#green(text, text_decoration = Curses::A_NORMAL, options = {}) ⇒ Object



47
48
49
# File 'lib/daigaku/window.rb', line 47

def green(text, text_decoration = Curses::A_NORMAL, options = {})
  colored(text, Window::COLOR_GREEN, text_decoration, options)
end

#heading(text, text_decoration = Curses::A_UNDERLINE) ⇒ Object



35
36
37
# File 'lib/daigaku/window.rb', line 35

def heading(text, text_decoration = Curses::A_UNDERLINE)
  write(text, Window::COLOR_HEADING, text_decoration)
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/daigaku/window.rb', line 81

def print_indicator(object, text = ' ', text_decoration = Curses::A_STANDOUT)
  if object.respond_to?(:mastered?) && object.respond_to?(:started?)
    if object.mastered?
      green(text, text_decoration)
      write ' '
    elsif object.started?
      yellow(text, text_decoration)
      write ' '
    else
      red(text, text_decoration)
      write ' '
    end
  elsif object.respond_to?(:mastered?)
    if object.mastered?
      green(text, text_decoration)
      write ' '
    else
      red(text, text_decoration)
      write ' '
    end
  end
end


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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/daigaku/window.rb', line 104

def print_markdown(text)
  clear_line

  h1 = /^\#{1}[^#]+/    # '# heading'
  h2 = /^\#{2}[^#]+/    # '## sub heading'
  bold = /(\*[^*]*\*)/  # '*text*'
  line = /^-{3,}/       # '---' vertical line
  code = /(\`*\`)/      # '`code line`'
  ruby_doc_core = /(\(ruby-doc core:.*\))/ # '(ruby-doc core: Kernel#print)'
  ruby_doc_stdlib = /(\(ruby-doc stdlib:.*\))/ # '(ruby-doc stdlib: CSV#Row::<<)'

  case text
    when h1
      heading(text.gsub(/^#\s?/, ''))
    when h2
      text_decoration = Curses::A_UNDERLINE | Curses::A_NORMAL
      emphasize(text.gsub(/^##\s?/, ''), text_decoration)
    when (code || bold)
      emphasized = false
      highlighted = false

      text.chars.each do |char|
        if char == '*'
          emphasized = !emphasized
          next
        end

        if char == '`'
          highlighted = !highlighted
          next
        end

        if highlighted
          red(char)
        elsif emphasized
          emphasize(char)
        else
          write(char)
        end
      end
    when bold
      text.chars.each do |char|
        if char == '*'
          emphasized = !emphasized
          next
        end

        emphasized ? emphasize(char) : write(char)
      end
    when line
      write('-' * (Curses.cols - 2))
    when ruby_doc_core
      capture = text.match(/\(ruby-doc core:\s?(.*)\)/).captures.first
      write text.gsub(ruby_doc_core, ruby_doc_core_link(capture))
    when ruby_doc_stdlib
      capture = text.match(/\(ruby-doc stdlib:\s?(.*)\)/).captures.first
      write text.gsub(ruby_doc_stdlib, ruby_doc_stdlib_link(capture))
    else
      write(text)
  end
end

#red(text, text_decoration = Curses::A_NORMAL, options = {}) ⇒ Object



39
40
41
# File 'lib/daigaku/window.rb', line 39

def red(text, text_decoration = Curses::A_NORMAL, options = {})
  colored(text, Window::COLOR_RED, text_decoration, options)
end

#write(text, color = COLOR_TEXT, text_decoration = Curses::A_NORMAL) ⇒ Object



27
28
29
# File 'lib/daigaku/window.rb', line 27

def write(text, color = COLOR_TEXT, text_decoration = Curses::A_NORMAL )
  self.attron(Curses.color_pair(color) | text_decoration) { self << text.to_s }
end

#yellow(text, text_decoration = Curses::A_NORMAL, options = {}) ⇒ Object



43
44
45
# File 'lib/daigaku/window.rb', line 43

def yellow(text, text_decoration = Curses::A_NORMAL, options = {})
  colored(text, Window::COLOR_YELLOW, text_decoration, options)
end