Class: Keisanjaku::Modes::Demo

Inherits:
Object
  • Object
show all
Defined in:
lib/keisanjaku/modes/demo.rb

Constant Summary collapse

REDRAW_INTERVAL =
0.1

Instance Method Summary collapse

Constructor Details

#initialize(expression, width: nil, color: true, animate: true, output: $stdout) ⇒ Demo

Returns a new instance of Demo.



11
12
13
14
15
16
17
18
# File 'lib/keisanjaku/modes/demo.rb', line 11

def initialize(expression, width: nil, color: true, animate: true, output: $stdout)
  @expression = expression
  @width = width
  @viewport = TerminalViewport.new(width: width)
  @renderer = Renderer.new(color: color)
  @animate = animate
  @output = output
end

Instance Method Details

#planObject



20
21
22
# File 'lib/keisanjaku/modes/demo.rb', line 20

def plan
  @plan ||= Planner.compile(Parser.parse(@expression), expression: @expression)
end

#run_interactive(input: $stdin) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/keisanjaku/modes/demo.rb', line 35

def run_interactive(input: $stdin)
  engine = Engine.new(plan, animate: @animate)
  reader = Input.new(input)
  auto = false
  speed = 0.35
  next_auto_at = Time.now + speed
  needs_draw = true
  @viewport = TerminalViewport.new(width: @width, right_margin: 1, clamp_fixed_width: true)
  @viewport.install
  Input.with_raw(input: input, output: @output) do
    loop do
      if needs_draw || @viewport.refresh_needed?
        draw(engine)
        needs_draw = false
      end

      key = reader.read_key(timeout: auto ? [speed, REDRAW_INTERVAL].min : REDRAW_INTERVAL)
      if key.nil?
        if auto && Time.now >= next_auto_at
          auto = false unless advance(engine)
          next_auto_at = Time.now + speed
          needs_draw = true
        end
        next
      end

      case key
      when :enter, :space
        advance(engine)
        needs_draw = true
      when "b"
        engine.step_back
        needs_draw = true
      when "a"
        auto = !auto
        next_auto_at = Time.now + speed
        needs_draw = true
      when "1"
        speed = 0.7
        next_auto_at = Time.now + speed
      when "2"
        speed = 0.35
        next_auto_at = Time.now + speed
      when "3"
        speed = 0.15
        next_auto_at = Time.now + speed
      when "q", :ctrl_c
        break
      end
    end
  end
ensure
  @viewport.restore if @viewport
end

#run_noninteractiveObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/keisanjaku/modes/demo.rb', line 24

def run_noninteractive
  engine = Engine.new(plan, animate: false)
  @output.puts "Expression: #{@expression}"
  plan.steps.each_with_index do |step, index|
    @output.puts "#{index + 1}. #{step.description}"
    engine.step_forward
  end
  @renderer.render(engine.state, width: @viewport.current_width, highlight_scale: plan.read_scale).each { |line| @output.puts line }
  print_summary(engine.summary)
end