annotate .zfun/zsh-autosuggestions/spec/terminal_session.rb @ 513:ad9ca178e094

hg: add crevert alias for curses-revert
author Augie Fackler <raf@durin42.com>
date Thu, 03 Jun 2021 14:25:21 -0400
parents e1ce8897030d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
467
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
1 require 'securerandom'
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
2
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
3 class TerminalSession
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
4 ZSH_BIN = ENV['TEST_ZSH_BIN'] || 'zsh'
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
5
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
6 def initialize(opts = {})
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
7 opts = {
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
8 width: 80,
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
9 height: 24,
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
10 prompt: '',
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
11 term: 'xterm-256color',
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
12 zsh_bin: ZSH_BIN
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
13 }.merge(opts)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
14
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
15 @opts = opts
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
16
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
17 cmd="PS1=\"#{opts[:prompt]}\" TERM=#{opts[:term]} #{ZSH_BIN} -f"
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
18 tmux_command("new-session -d -x #{opts[:width]} -y #{opts[:height]} '#{cmd}'")
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
19 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
20
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
21 def zsh_version
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
22 @zsh_version ||= Gem::Version.new(`#{ZSH_BIN} -c 'echo -n $ZSH_VERSION'`)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
23 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
24
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
25 def tmux_socket_name
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
26 @tmux_socket_name ||= SecureRandom.hex(6)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
27 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
28
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
29 def run_command(command)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
30 send_string(command)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
31 send_keys('enter')
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
32
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
33 self
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
34 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
35
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
36 def send_string(str)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
37 tmux_command("send-keys -t 0 -l -- '#{str.gsub("'", "\\'")}'")
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
38
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
39 self
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
40 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
41
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
42 def send_keys(*keys)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
43 tmux_command("send-keys -t 0 #{keys.join(' ')}")
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
44
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
45 self
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
46 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
47
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
48 def paste_string(str)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
49 tmux_command("set-buffer -- '#{str}'")
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
50 tmux_command("paste-buffer -dpr -t 0")
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
51
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
52 self
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
53 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
54
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
55 def content(esc_seqs: false)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
56 cmd = 'capture-pane -p -t 0'
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
57 cmd += ' -e' if esc_seqs
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
58 tmux_command(cmd).strip
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
59 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
60
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
61 def clear_screen
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
62 send_keys('C-l')
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
63
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
64 i = 0
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
65 until content == opts[:prompt] || i > 20 do
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
66 sleep(0.1)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
67 i = i + 1
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
68 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
69
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
70 self
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
71 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
72
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
73 def destroy
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
74 tmux_command('kill-session')
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
75 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
76
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
77 def cursor
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
78 tmux_command("display-message -t 0 -p '\#{cursor_x},\#{cursor_y}'").
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
79 strip.
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
80 split(',').
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
81 map(&:to_i)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
82 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
83
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
84 def attach!
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
85 tmux_command('attach-session')
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
86 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
87
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
88 private
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
89
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
90 attr_reader :opts
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
91
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
92 def tmux_command(cmd)
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
93 out = `tmux -u -L #{tmux_socket_name} #{cmd}`
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
94
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
95 raise("tmux error running: '#{cmd}'") unless $?.success?
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
96
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
97 out
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
98 end
e1ce8897030d zsh: import df6f6f9ff41 of zsh-autosuggestions
Augie Fackler <raf@durin42.com>
parents:
diff changeset
99 end