tty-draft 0.1.1 → 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 +6 -2
- data/lib/tty-draft.rb +71 -35
- 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,14 +19,18 @@ 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
32
|
- `enter`: new line
|
|
29
|
-
- arrow keys: move cursor around
|
|
33
|
+
- arrow keys: move cursor around and retrieve history
|
|
30
34
|
- `ctrl-d`/`ctrl-z`: submit
|
|
31
35
|
|
|
32
36
|
## License
|
data/lib/tty-draft.rb
CHANGED
|
@@ -3,41 +3,39 @@ 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
|
|
40
37
|
when :ctrl_d, :ctrl_z
|
|
38
|
+
history << @chars
|
|
41
39
|
@done = true
|
|
42
40
|
when :backspace
|
|
43
41
|
if @col > 0
|
|
@@ -63,11 +61,25 @@ module TTY
|
|
|
63
61
|
@row += 1
|
|
64
62
|
@col = 0
|
|
65
63
|
when :up
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
68
71
|
when :down
|
|
69
|
-
|
|
70
|
-
|
|
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
|
|
71
83
|
when :return
|
|
72
84
|
insert_row
|
|
73
85
|
else
|
|
@@ -77,19 +89,19 @@ module TTY
|
|
|
77
89
|
end
|
|
78
90
|
end
|
|
79
91
|
|
|
92
|
+
private
|
|
93
|
+
|
|
80
94
|
def edited
|
|
81
|
-
to_string(@chars)
|
|
95
|
+
render to_string(@chars)
|
|
82
96
|
end
|
|
83
97
|
|
|
84
98
|
def editing
|
|
85
99
|
copy = @chars.dup
|
|
86
100
|
copy[@row] = current_row.dup
|
|
87
101
|
copy[@row][@col] = "\e[7m#{copy[@row][@col] || ' '}\e[27m"
|
|
88
|
-
to_string(copy)
|
|
102
|
+
render to_string(copy)
|
|
89
103
|
end
|
|
90
104
|
|
|
91
|
-
private
|
|
92
|
-
|
|
93
105
|
def delete_char
|
|
94
106
|
current_row.delete_at(@col)
|
|
95
107
|
end
|
|
@@ -109,12 +121,36 @@ module TTY
|
|
|
109
121
|
@col = 0
|
|
110
122
|
end
|
|
111
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
|
+
|
|
112
141
|
def fix_col
|
|
113
142
|
@col = [@col, current_row.size].min
|
|
114
143
|
end
|
|
115
144
|
|
|
116
145
|
def to_string(chars)
|
|
117
|
-
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
|
|
118
154
|
end
|
|
119
155
|
end
|
|
120
156
|
end
|