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