pikuri-workspace 0.0.7 → 0.2.0
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 +23 -16
- data/lib/pikuri/workspace/confirmer/terminal.rb +252 -0
- data/lib/pikuri/workspace/confirmer.rb +184 -123
- data/lib/pikuri/workspace/edit.rb +261 -101
- data/lib/pikuri/workspace/extension.rb +163 -0
- data/lib/pikuri/workspace/file_list.rb +125 -0
- data/lib/pikuri/workspace/filesystem.rb +160 -446
- data/lib/pikuri/workspace/listing.rb +204 -0
- data/lib/pikuri/workspace/read.rb +139 -225
- data/lib/pikuri/workspace/search/glob.rb +310 -0
- data/lib/pikuri/workspace/search/grep.rb +353 -0
- data/lib/pikuri/workspace/search/utils.rb +173 -0
- data/lib/pikuri/workspace/workspace.rb +105 -0
- data/lib/pikuri/workspace/write.rb +129 -78
- data/lib/pikuri/workspace/write_gate.rb +74 -0
- data/lib/pikuri-workspace.rb +8 -10
- data/prompts/pikuri-files.txt +6 -0
- metadata +32 -11
- data/lib/pikuri/workspace/glob.rb +0 -322
- data/lib/pikuri/workspace/grep.rb +0 -350
- data/lib/pikuri/workspace/project_root.rb +0 -102
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pikuri
|
|
4
|
+
module Workspace
|
|
5
|
+
# Renders a directory as rows a model can orient from — name-sorted,
|
|
6
|
+
# directories first, sizes +ls -h+ style:
|
|
7
|
+
#
|
|
8
|
+
# Listing.render(project_root, filesystem: fs, depth: 2) # =>
|
|
9
|
+
# lib/
|
|
10
|
+
# pikuri/ [3 dirs, 1 file]
|
|
11
|
+
# version.rb 48B
|
|
12
|
+
# spec/ [empty]
|
|
13
|
+
# README.md 4.2K
|
|
14
|
+
# broken.md [Errno::ENOENT]
|
|
15
|
+
#
|
|
16
|
+
# A directory shows +[3 dirs, 1 file]+ exactly where its contents *aren't*
|
|
17
|
+
# on screen — the deepest rendered level — so +lib/+ above, expanded, never
|
|
18
|
+
# restates children that are already rows. Counts are immediate children,
|
|
19
|
+
# not recursive.
|
|
20
|
+
#
|
|
21
|
+
# A row that can't be measured prints its errno class rather than a prose
|
|
22
|
+
# word or a fake zero: +ENOENT+ is a dangling symlink or an entry that
|
|
23
|
+
# vanished mid-listing, +EACCES+ a directory you can't open, +ELOOP+ a
|
|
24
|
+
# symlink cycle. Each implies a different follow-up, and a model reads
|
|
25
|
+
# +EACCES+ unaided. Only the *root* failing raises.
|
|
26
|
+
#
|
|
27
|
+
# A credential path is +[denied]+ and is never descended into or +stat+ed
|
|
28
|
+
# (+.ssh/ [denied]+). Shown rather than omitted because this listing
|
|
29
|
+
# answers *"what is here"*, where a silent omission is a lie the model
|
|
30
|
+
# would burn turns re-probing — and +~/.ssh+ existing is not the secret;
|
|
31
|
+
# its filenames and bytes are. That is why {Filesystem#denied?} is a
|
|
32
|
+
# *required* constructor-level input here: a default of "nothing is
|
|
33
|
+
# denied" would let a new caller leak by forgetting.
|
|
34
|
+
#
|
|
35
|
+
# == Implementation details
|
|
36
|
+
#
|
|
37
|
+
# Walks with +Dir.children+, not the +rg --files+ prefix derivation
|
|
38
|
+
# {Search::Glob} uses, so symlinked / gitignored-only / empty directories
|
|
39
|
+
# can't silently vanish from the answer. The price is a caller-visible one:
|
|
40
|
+
# nothing evaluates +.gitignore+ here, so everything shows except +.git/+ —
|
|
41
|
+
# +ls+ behaviour, and the one place the file tools diverge on gitignore.
|
|
42
|
+
# Full argument in +design/decisions.md+ +D_directory_listing+.
|
|
43
|
+
module Listing
|
|
44
|
+
# @return [Integer] hard byte cap on rendered output. Same value as
|
|
45
|
+
# {Search::Glob::MAX_BYTES}; re-declared rather than cross-referenced
|
|
46
|
+
# because Zeitwerk's eager-load order between siblings isn't guaranteed.
|
|
47
|
+
MAX_BYTES = 50 * 1024
|
|
48
|
+
|
|
49
|
+
# @return [String] human-readable {MAX_BYTES} for truncation messages.
|
|
50
|
+
MAX_BYTES_LABEL = "#{MAX_BYTES / 1024} KB"
|
|
51
|
+
|
|
52
|
+
# @return [Integer] default +depth+ — immediate children only.
|
|
53
|
+
DEFAULT_DEPTH = 1
|
|
54
|
+
|
|
55
|
+
# @return [Integer] deepest +depth+ honoured; larger values clamp down.
|
|
56
|
+
# Past three levels a listing stops being orientation and becomes the
|
|
57
|
+
# whole-tree dump {MAX_BYTES} would eat anyway.
|
|
58
|
+
MAX_DEPTH = 3
|
|
59
|
+
|
|
60
|
+
# @return [String] per-level indent for nested rows.
|
|
61
|
+
INDENT = ' '
|
|
62
|
+
|
|
63
|
+
# @return [String] separator between an entry's name and its size /
|
|
64
|
+
# summary column.
|
|
65
|
+
GAP = ' '
|
|
66
|
+
|
|
67
|
+
# @return [String] the one entry never listed, matching
|
|
68
|
+
# {Search::Glob}'s +--glob '!.git/*'+.
|
|
69
|
+
GIT_DIR = '.git'
|
|
70
|
+
|
|
71
|
+
# @return [Array<String>] size suffixes, +ls -h+ style.
|
|
72
|
+
SIZE_UNITS = %w[B K M G T].freeze
|
|
73
|
+
|
|
74
|
+
# @return [String] stands in for the size / summary column on a path
|
|
75
|
+
# under {Filesystem#denied?}.
|
|
76
|
+
DENIED = '[denied]'
|
|
77
|
+
|
|
78
|
+
# Render +dir+'s entries.
|
|
79
|
+
#
|
|
80
|
+
# @param dir [String, Pathname] absolute directory path
|
|
81
|
+
# @param filesystem [Filesystem] consulted per row via
|
|
82
|
+
# {Filesystem#denied?}; required, never defaulted (see the class doc)
|
|
83
|
+
# @param depth [Integer] levels to descend, clamped to +1..{MAX_DEPTH}+
|
|
84
|
+
# @return [String] newline-joined rows, head-truncated at {MAX_BYTES};
|
|
85
|
+
# +""+ when +dir+ has no entries
|
|
86
|
+
# @raise [SystemCallError] if +dir+ itself can't be opened
|
|
87
|
+
def self.render(dir, filesystem:, depth: DEFAULT_DEPTH)
|
|
88
|
+
render_with_count(dir, filesystem: filesystem, depth: depth).first
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# {render} plus how many rows it walked — what a caller needs for a
|
|
92
|
+
# display line, and not recoverable from the rendered text (nested rows
|
|
93
|
+
# and a truncation marker are lines too).
|
|
94
|
+
#
|
|
95
|
+
# listing, entries = Listing.render_with_count(root, filesystem: fs)
|
|
96
|
+
#
|
|
97
|
+
# @param dir [String, Pathname] directory to list
|
|
98
|
+
# @param filesystem [Filesystem] consulted per row via +denied?+
|
|
99
|
+
# @param depth [Integer] levels to descend, clamped to {MAX_DEPTH}
|
|
100
|
+
# @return [Array(String, Integer)] the listing (+""+ when empty) and its
|
|
101
|
+
# row count
|
|
102
|
+
# @raise [SystemCallError] if +dir+ itself can't be opened
|
|
103
|
+
def self.render_with_count(dir, filesystem:, depth: DEFAULT_DEPTH)
|
|
104
|
+
rows = child_rows(dir.to_s, filesystem: filesystem,
|
|
105
|
+
depth: depth.clamp(1, MAX_DEPTH), indent: '')
|
|
106
|
+
return ['', 0] if rows.empty?
|
|
107
|
+
|
|
108
|
+
content, marker = Search::Utils.head_truncate(
|
|
109
|
+
rows.join("\n"), max_bytes: MAX_BYTES, hint: 'list a subdirectory instead'
|
|
110
|
+
)
|
|
111
|
+
[content + marker, rows.size]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Rows for +dir+'s immediate children, recursing while +depth+ allows.
|
|
115
|
+
#
|
|
116
|
+
# @return [Array<String>]
|
|
117
|
+
# @raise [SystemCallError] if +dir+ can't be opened
|
|
118
|
+
def self.child_rows(dir, filesystem:, depth:, indent:)
|
|
119
|
+
dirs, files = partition_children(dir)
|
|
120
|
+
dirs.flat_map { |name|
|
|
121
|
+
dir_rows(File.join(dir, name), name: name, filesystem: filesystem,
|
|
122
|
+
depth: depth, indent: indent)
|
|
123
|
+
} + files.map { |name|
|
|
124
|
+
file_row(File.join(dir, name), name: name, filesystem: filesystem, indent: indent)
|
|
125
|
+
}
|
|
126
|
+
end
|
|
127
|
+
private_class_method :child_rows
|
|
128
|
+
|
|
129
|
+
# One directory's row, plus its children's rows when +depth+ allows. An
|
|
130
|
+
# expanded directory that turns out to have no children keeps +[empty]+:
|
|
131
|
+
# a bare +foo/+ with nothing under it reads as truncation.
|
|
132
|
+
#
|
|
133
|
+
# @return [Array<String>]
|
|
134
|
+
def self.dir_rows(absolute, name:, filesystem:, depth:, indent:)
|
|
135
|
+
label = "#{indent}#{name}/"
|
|
136
|
+
return ["#{label}#{GAP}#{DENIED}"] if filesystem.denied?(absolute)
|
|
137
|
+
return ["#{label}#{GAP}#{summary(absolute)}"] if depth <= 1
|
|
138
|
+
|
|
139
|
+
sub = child_rows(absolute, filesystem: filesystem, depth: depth - 1,
|
|
140
|
+
indent: indent + INDENT)
|
|
141
|
+
sub.empty? ? ["#{label}#{GAP}[empty]"] : [label] + sub
|
|
142
|
+
rescue SystemCallError => e
|
|
143
|
+
["#{label}#{GAP}[#{e.class}]"]
|
|
144
|
+
end
|
|
145
|
+
private_class_method :dir_rows
|
|
146
|
+
|
|
147
|
+
# @return [String] the +[N dirs, M files]+ / +[empty]+ column
|
|
148
|
+
# @raise [SystemCallError] if the directory can't be opened
|
|
149
|
+
def self.summary(absolute)
|
|
150
|
+
dirs, files = partition_children(absolute)
|
|
151
|
+
return '[empty]' if dirs.empty? && files.empty?
|
|
152
|
+
|
|
153
|
+
"[#{Pikuri::Tool.pluralize(dirs.size, 'dir')}, #{Pikuri::Tool.pluralize(files.size, 'file')}]"
|
|
154
|
+
end
|
|
155
|
+
private_class_method :summary
|
|
156
|
+
|
|
157
|
+
# @return [String] one file's row, e.g. +"version.rb 48B"+
|
|
158
|
+
def self.file_row(absolute, name:, filesystem:, indent:)
|
|
159
|
+
return "#{indent}#{name}#{GAP}#{DENIED}" if filesystem.denied?(absolute)
|
|
160
|
+
|
|
161
|
+
"#{indent}#{name}#{GAP}#{human_size(File.size(absolute))}"
|
|
162
|
+
rescue SystemCallError => e
|
|
163
|
+
"#{indent}#{name}#{GAP}[#{e.class}]"
|
|
164
|
+
end
|
|
165
|
+
private_class_method :file_row
|
|
166
|
+
|
|
167
|
+
# Immediate children split into directories and files, each sorted,
|
|
168
|
+
# dotfiles kept (as +ls -a+). A symlink is classified by its target, so a
|
|
169
|
+
# dangling one lands in +files+ and degrades in the size column.
|
|
170
|
+
#
|
|
171
|
+
# Don't "simplify" this into one flat sort of full paths: +-+ (0x2D)
|
|
172
|
+
# sorts before +/+ (0x2F), so +pikuri/lib+ would land after
|
|
173
|
+
# +pikuri-workspace/spec+ and render under the wrong parent. Sorting
|
|
174
|
+
# per-directory is what makes that unrepresentable.
|
|
175
|
+
#
|
|
176
|
+
# @return [Array(Array<String>, Array<String>)]
|
|
177
|
+
# @raise [SystemCallError] if the directory can't be opened
|
|
178
|
+
def self.partition_children(dir)
|
|
179
|
+
entries = Dir.children(dir).reject { |e| e == GIT_DIR }
|
|
180
|
+
dirs, files = entries.partition { |e| File.directory?(File.join(dir, e)) }
|
|
181
|
+
[dirs.sort, files.sort]
|
|
182
|
+
end
|
|
183
|
+
private_class_method :partition_children
|
|
184
|
+
|
|
185
|
+
# @param bytes [Integer]
|
|
186
|
+
# @return [String] +ls -h+ style: a decimal below 10 (+4.2K+), whole
|
|
187
|
+
# units above (+50M+), raw bytes under 1 K (+812B+)
|
|
188
|
+
def self.human_size(bytes)
|
|
189
|
+
unit = SIZE_UNITS.first
|
|
190
|
+
value = bytes.to_f
|
|
191
|
+
SIZE_UNITS.drop(1).each do |bigger|
|
|
192
|
+
break if value < 1024
|
|
193
|
+
|
|
194
|
+
value /= 1024
|
|
195
|
+
unit = bigger
|
|
196
|
+
end
|
|
197
|
+
return "#{value.round}#{unit}" if unit == SIZE_UNITS.first || value >= 10
|
|
198
|
+
|
|
199
|
+
format('%.1f%s', value, unit)
|
|
200
|
+
end
|
|
201
|
+
private_class_method :human_size
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|