Class: Daigaku::Window

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Window.



9
10
11
12
# File 'lib/daigaku/window.rb', line 9

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]



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/daigaku/window.rb', line 55

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/daigaku/window.rb', line 38

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



18
19
20
# File 'lib/daigaku/window.rb', line 18

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

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



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

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

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



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

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


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/daigaku/window.rb', line 68

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


91
92
93
94
95
96
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
147
148
149
150
# File 'lib/daigaku/window.rb', line 91

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::<<)'

  text = Markdown::RubyDoc.parse(text)

  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_with_index do |char, index|
      if char == '*' && text[index - 1] != '\\'
        emphasized = !emphasized
        next
      end

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

      character = "#{text[index..(index + 1)]}" == '\\*' ? '' : char

      if highlighted
        red(character)
      elsif emphasized
        emphasize(character)
      else
        write(character)
      end
    end
  when bold
    text.chars.each_with_index do |char, index|
      if char == '*' && text[index - 1] != '\\'
        emphasized = !emphasized
        next
      end

      character = "#{text[index..(index + 1)]}" == '\\*' ? '' : char
      emphasized ? emphasize(character) : write(character)
    end
  when line
    write('-' * (Curses.cols - 2))
  else
    write(text.gsub(/(\\#)/, '#'))
  end
end

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



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

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

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



14
15
16
# File 'lib/daigaku/window.rb', line 14

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



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

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