class Daigaku::Views::TaskView

Constants

TOP_BAR_TEXT

Public Class Methods

new() click to toggle source
# File lib/daigaku/views/task_view.rb, line 18
def initialize
  @lines = []
  @top   = nil
end

Public Instance Methods

enter(course, chapter, unit) click to toggle source
# File lib/daigaku/views/task_view.rb, line 23
def enter(course, chapter, unit)
  @course  = course
  @chapter = chapter
  @unit    = unit

  @test_result_lines = nil
  @lines             = @unit.task.markdown.lines
  @top_bar_height    = 4
  @head_height       = 2

  initialize_window(@lines.count + @top_bar_height + @head_height)
end

Private Instance Methods

code_error_lines(code) click to toggle source
# File lib/daigaku/views/task_view.rb, line 222
def code_error_lines(code)
  begin
    eval(code)
  rescue StandardError, ScriptError => error
    return error.inspect.gsub(/(\A.*#<|>.*$)/, '').lines.map(&:rstrip)
  end

  []
end
draw(window) click to toggle source
# File lib/daigaku/views/task_view.rb, line 55
def draw(window)
  @top = 0
  window.attrset(A_NORMAL)
  print_head(window)

  @lines.each_with_index do |line, index|
    window.setpos(index + 2, 1)
    print_line(window, line, index)
  end

  window.setpos(0, 1)
  window.refresh
end
example_index(heights, index) click to toggle source
# File lib/daigaku/views/task_view.rb, line 248
def example_index(heights, index)
  heights.values.index { |range| range.include?(index) }.to_i
end
initialize_window(height) click to toggle source
# File lib/daigaku/views/task_view.rb, line 241
def initialize_window(height)
  @window = default_window(height)

  top_bar = TopBar.new(@window, TOP_BAR_TEXT)
  show sub_window_below_top_bar(@window, top_bar)
end
interact_with(window) click to toggle source
# File lib/daigaku/views/task_view.rb, line 122
def interact_with(window)
  while char = window.getch
    scrollable = true

    case char
    when 'v' # verify
      print_test_results
      return
    when 'c' # clear
      reset_screen
      return
    when 'o' # open solution file
      open_editor(@unit.solution.path)
    when Curses::KEY_DOWN, Curses::KEY_CTRL_N
      scrollable = scroll_down(window)
    when Curses::KEY_UP, Curses::KEY_CTRL_P
      scrollable = scroll_up(window)
    when Curses::KEY_NPAGE, '?\s' # white space
      0.upto(window.maxy - 2) do |n|
        scrolled = scroll_down(window)

        unless scrolled
          scrollable = false if n == 0
          break
        end
      end
    when Curses::KEY_PPAGE
      0.upto(window.maxy - 2) do |n|
        scrolled = scroll_up(window)

        unless scrolled
          scrollable = false if n == 0
          break
        end
      end
    when Curses::KEY_LEFT, Curses::KEY_CTRL_T
      while scroll_up(window)
      end
    when Curses::KEY_RIGHT, Curses::KEY_CTRL_B
      while scroll_down(window)
      end
    when Curses::KEY_BACKSPACE
      broadcast(:reenter, @course, @chapter, @unit)
      return
    when 27 # ESC
      exit
    end

    Curses.beep unless scrollable
    window.setpos(0, 1)
    window.refresh
  end
end
open_editor(file_path) click to toggle source
# File lib/daigaku/views/task_view.rb, line 176
def open_editor(file_path)
  if OS.windows?
    system "start '' '#{file_path}'"
  elsif OS.mac?
    system "open '#{file_path}'"
  elsif OS.linux?
    system "xdg-open '#{file_path}'"
  end
end
print_head(window) click to toggle source
print_line(window, line, index) click to toggle source
print_test_results() click to toggle source
reset_screen() click to toggle source
# File lib/daigaku/views/task_view.rb, line 232
def reset_screen
  @test_result_lines = nil
  @lines             = @unit.task.markdown.lines
  lines_count        = @lines.count + @top_bar_height + @head_height
  height             = [lines_count, Curses.lines].max

  initialize_window(height)
end
scroll_down(window) click to toggle source
# File lib/daigaku/views/task_view.rb, line 83
def scroll_down(window)
  if @top + Curses.lines <= @lines.count + @top_bar_height + @head_height
    window.scrl(1)
    print_head(window)

    @top += 1
    line = @lines[@top + window.maxy - 1]
    window.setpos(window.maxy - 1, 1)

    if line
      print_line(window, line, @top)
    else
      window.clear_line
    end

    return true
  else
    return false
  end
end
scroll_up(window) click to toggle source
# File lib/daigaku/views/task_view.rb, line 69
def scroll_up(window)
  return unless @top > 0

  window.scrl(-1)
  print_head(window)

  @top -= 1
  line = @lines[@top]

  return unless line
  window.setpos(2, 1)
  print_line(window, line, @top)
end
show(window) click to toggle source
# File lib/daigaku/views/task_view.rb, line 49
def show(window)
  window.scrollok(true)
  draw(window)
  interact_with(window)
end
test_result_lines(result) click to toggle source
# File lib/daigaku/views/task_view.rb, line 216
def test_result_lines(result)
  lines  = result.summary_lines
  lines += code_error_lines(@unit.solution.code) unless result.passed?
  lines
end