Class: Fatty::KeyTestSession

Inherits:
Session
  • Object
show all
Defined in:
lib/fatty/session/keytest_session.rb

Instance Attribute Summary collapse

Attributes inherited from Session

#counter, #id, #keymap, #terminal

Instance Method Summary collapse

Methods inherited from Session

#handle_resize, #persist!, #renderer, #screen, #tick

Methods included from Actionable

included

Constructor Details

#initialize(output_dir: Dir.tmpdir) ⇒ KeyTestSession

Returns a new instance of KeyTestSession.



9
10
11
12
13
14
# File 'lib/fatty/session/keytest_session.rb', line 9

def initialize(output_dir: Dir.tmpdir)
  super(id: :keytest, keymap: nil)
  @output_session = OutputSession.new
  @path = nil
  @suggestions = []
end

Instance Attribute Details

#output_sessionObject (readonly)

Returns the value of attribute output_session.



7
8
9
# File 'lib/fatty/session/keytest_session.rb', line 7

def output_session
  @output_session
end

Instance Method Details

#closeObject



92
93
94
95
# File 'lib/fatty/session/keytest_session.rb', line 92

def close
  output_session.close
  nil
end

#init(terminal:) ⇒ Object

Session Protocol



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fatty/session/keytest_session.rb', line 20

def init(terminal:)
  commands = super
  @suggestions = []

  commands.concat(output_session.init(terminal: terminal))
  commands << Command.terminal(:register_session, session: output_session)
  commands << Command.session(output_session.id, :resize)
  commands << Command.session(output_session.id, :set_mode, mode: :scrolling)
  commands << Command.session(output_session.id, :begin_command)
  commands << Command.session(
    output_session.id,
    :append,
    text: <<~TEXT,
      Key test mode

      Press keys to inspect Fatty's decoding and binding resolution.
      Press q, ESC, or C-g to leave key test mode.

    TEXT
    follow: true,
  )
  commands << show_quit_status_command
  commands
end

#update(command) ⇒ Object



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
# File 'lib/fatty/session/keytest_session.rb', line 45

def update(command)
  log_update(command)
  commands =
    case command.action
    when :key
      ev = command.payload.fetch(:event)
      if quit_key?(ev)
        write_suggestions_commands + [
          Command.session(output_session.id, :append, text: "Leaving key test mode.\n\n", follow: true),
          Command.session(:status, :clear),
          Command.terminal(:pop_modal),
          Command.session(
            :status,
            :show,
            text: "KeyTest suggestions written to #{@path}",
            role: :good,
          ),
        ]
      else
        snippet = ev.suggested_snippet(terminal_name)
        @suggestions << snippet unless snippet.empty?
        text = [
          ev.report,
          "\nSuggested keybindings:\n",
          snippet,
          "\n",
        ].join
        [
          Command.session(
            output_session.id,
            :append,
            text: text,
            follow: true,
            mode: :scrolling),
          show_quit_status_command,
        ]
      end
    else
      []
    end
  Array(commands)
end

#viewObject



88
89
90
# File 'lib/fatty/session/keytest_session.rb', line 88

def view
  output_session.view
end