tty-draft 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -4
- data/lib/tty-draft.rb +74 -37
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 96bf89073a5a4f36eaf4f201aea23179363d51e71634f19b3606920081ac10b9
|
|
4
|
+
data.tar.gz: 552c765b4e9ef313810cd612dcd36361ca258514aa02218575961bd229e75917
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0a34cdf25cdadb7e9439af8d2fc671fe88bb7d39d2fcc60572019dba79c6d7a7459813c16476649bbb25430d8e1b69aba8b08f4ea91acac609b9cfcbb846eb7d
|
|
7
|
+
data.tar.gz: 6ae19fe5ad236885fc4483cdced1c8d6ccbe503c0d67dba14a344cd46892e133ea46690b3678ec32cdc7ce9f73abbb14d9d1f145c4920b72c5dc7ea6acb4de4b
|
data/README.md
CHANGED
|
@@ -19,15 +19,19 @@ gem install tty-draft
|
|
|
19
19
|
```ruby
|
|
20
20
|
require 'tty-draft'
|
|
21
21
|
|
|
22
|
+
draft = TTY::Draft.new(
|
|
23
|
+
prompt: ->(n){n==0 ? "\033[32m> \033[0m" : ' '}
|
|
24
|
+
)
|
|
25
|
+
|
|
22
26
|
loop do
|
|
23
|
-
|
|
27
|
+
draft.gets
|
|
24
28
|
puts "I see"
|
|
25
29
|
end
|
|
26
30
|
```
|
|
27
31
|
|
|
28
|
-
- `enter`:
|
|
29
|
-
-
|
|
30
|
-
-
|
|
32
|
+
- `enter`: new line
|
|
33
|
+
- arrow keys: move cursor around and retrieve history
|
|
34
|
+
- `ctrl-d`/`ctrl-z`: submit
|
|
31
35
|
|
|
32
36
|
## License
|
|
33
37
|
|
data/lib/tty-draft.rb
CHANGED
|
@@ -3,40 +3,40 @@ require 'tty-live'
|
|
|
3
3
|
|
|
4
4
|
module TTY
|
|
5
5
|
class Draft
|
|
6
|
-
VERSION = '0.1.
|
|
7
|
-
|
|
8
|
-
class << self
|
|
9
|
-
def gets
|
|
10
|
-
draft = new
|
|
11
|
-
reader = Reader.new
|
|
12
|
-
reader.subscribe(draft)
|
|
13
|
-
live = Live.new
|
|
14
|
-
|
|
15
|
-
loop do
|
|
16
|
-
reader.read_char
|
|
17
|
-
if draft.done
|
|
18
|
-
live.update('')
|
|
19
|
-
reader.unsubscribe(draft)
|
|
20
|
-
print live.show
|
|
21
|
-
return draft.edited
|
|
22
|
-
end
|
|
23
|
-
live.update(draft.editing)
|
|
24
|
-
print live.hide
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
6
|
+
VERSION = '0.1.2'
|
|
28
7
|
|
|
29
8
|
attr_reader :done
|
|
30
9
|
|
|
31
|
-
def initialize
|
|
32
|
-
@
|
|
33
|
-
|
|
34
|
-
|
|
10
|
+
def initialize(prompt: ->(n){''})
|
|
11
|
+
@prompt = prompt
|
|
12
|
+
new_draft
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def gets
|
|
16
|
+
reader = Reader.new
|
|
17
|
+
reader.subscribe(self)
|
|
18
|
+
live = Live.new
|
|
19
|
+
|
|
20
|
+
live.update(edited)
|
|
21
|
+
loop do
|
|
22
|
+
reader.read_char
|
|
23
|
+
if done
|
|
24
|
+
live.update(edited)
|
|
25
|
+
reader.unsubscribe(self)
|
|
26
|
+
puts live.show
|
|
27
|
+
return to_string(@chars).tap{new_draft}
|
|
28
|
+
end
|
|
29
|
+
live.update(editing)
|
|
30
|
+
print live.hide
|
|
31
|
+
end
|
|
35
32
|
end
|
|
36
33
|
|
|
37
34
|
def keypress(event)
|
|
38
35
|
@done = false
|
|
39
36
|
case event.key.name
|
|
37
|
+
when :ctrl_d, :ctrl_z
|
|
38
|
+
history << @chars
|
|
39
|
+
@done = true
|
|
40
40
|
when :backspace
|
|
41
41
|
if @col > 0
|
|
42
42
|
@col -= 1
|
|
@@ -61,14 +61,27 @@ module TTY
|
|
|
61
61
|
@row += 1
|
|
62
62
|
@col = 0
|
|
63
63
|
when :up
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
if @row > 0
|
|
65
|
+
@row -= 1
|
|
66
|
+
return fix_col
|
|
67
|
+
end
|
|
68
|
+
if @work > 0
|
|
69
|
+
return load_history(@work -= 1)
|
|
70
|
+
end
|
|
66
71
|
when :down
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
if (@row + 1) < @chars.size
|
|
73
|
+
@row += 1
|
|
74
|
+
return fix_col
|
|
75
|
+
end
|
|
76
|
+
if @work + 1 < @workspaces.size
|
|
77
|
+
return load_history(@work += 1)
|
|
78
|
+
end
|
|
79
|
+
when :home, :ctrl_a
|
|
80
|
+
@col = 0
|
|
81
|
+
when :end, :ctrl_e
|
|
82
|
+
@col = current_row.size
|
|
69
83
|
when :return
|
|
70
|
-
|
|
71
|
-
@done = true
|
|
84
|
+
insert_row
|
|
72
85
|
else
|
|
73
86
|
return insert_row if event.value == "\n"
|
|
74
87
|
current_row.insert(@col, event.value)
|
|
@@ -76,19 +89,19 @@ module TTY
|
|
|
76
89
|
end
|
|
77
90
|
end
|
|
78
91
|
|
|
92
|
+
private
|
|
93
|
+
|
|
79
94
|
def edited
|
|
80
|
-
to_string(@chars)
|
|
95
|
+
render to_string(@chars)
|
|
81
96
|
end
|
|
82
97
|
|
|
83
98
|
def editing
|
|
84
99
|
copy = @chars.dup
|
|
85
100
|
copy[@row] = current_row.dup
|
|
86
101
|
copy[@row][@col] = "\e[7m#{copy[@row][@col] || ' '}\e[27m"
|
|
87
|
-
to_string(copy)
|
|
102
|
+
render to_string(copy)
|
|
88
103
|
end
|
|
89
104
|
|
|
90
|
-
private
|
|
91
|
-
|
|
92
105
|
def delete_char
|
|
93
106
|
current_row.delete_at(@col)
|
|
94
107
|
end
|
|
@@ -108,12 +121,36 @@ module TTY
|
|
|
108
121
|
@col = 0
|
|
109
122
|
end
|
|
110
123
|
|
|
124
|
+
def new_draft
|
|
125
|
+
@chars = [[]]
|
|
126
|
+
@row = 0
|
|
127
|
+
@col = 0
|
|
128
|
+
(@workspaces = [])[(@work = history.size)] = @chars
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def history
|
|
132
|
+
@history ||= []
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def load_history(i)
|
|
136
|
+
@chars = (@workspaces[i] ||= @history[i].map(&:dup))
|
|
137
|
+
@row = @chars.size - 1
|
|
138
|
+
@col = current_row.size
|
|
139
|
+
end
|
|
140
|
+
|
|
111
141
|
def fix_col
|
|
112
142
|
@col = [@col, current_row.size].min
|
|
113
143
|
end
|
|
114
144
|
|
|
115
145
|
def to_string(chars)
|
|
116
|
-
chars.map
|
|
146
|
+
chars.map(&:join).join("\n")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def render(str)
|
|
150
|
+
lines = str.empty? ? [''] : str.each_line
|
|
151
|
+
lines.each_with_index.map do |line, i|
|
|
152
|
+
@prompt[i] + line
|
|
153
|
+
end.join
|
|
117
154
|
end
|
|
118
155
|
end
|
|
119
156
|
end
|