tty-file 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c14d1cde8381d3b5e40e0b73139df56495bf4247abc1e5b28cfa5cfc17ed94a
4
- data.tar.gz: 752eebdc5b63125700de9ed06aa8abd700ab1518a595e26b2bbaa767dadc31c4
3
+ metadata.gz: e1d9e2c1d0c438b74128707623835e44da4ec23dcdebfe0ba88afb5284c730a7
4
+ data.tar.gz: 63c7b68f347b2199502240cbdbfdf20264f90a95ece1ea50f60a78d2c3a6c73f
5
5
  SHA512:
6
- metadata.gz: 2625754b9f281800439fbc63eea1b978bf3711ec38eb5040e8279f3ce68de5880c8a58003f2f42b7717f86660f987aa321db128f095cef9a81360b19a020da3f
7
- data.tar.gz: e87cd920b9f868ed0774d986b997e697a1351cc83916b05fd148e439b0e9fc5c24ace82d41add7c287dced567ba5ce43010cd58667f04409c3a49d1988fbf38d
6
+ metadata.gz: 0ab6abf98fa2bcdb58f7f0eb2d3518e1184607863fedfeb2a1de34b21eeb876d3e7c9ce2eb0c48198b98dfaae7d281c49447a7dfe872a48e19d07493fdb0c020
7
+ data.tar.gz: 9925c5f71b7a0c8f9afb369abb6b83df9c6700d87884a3b9e4ce2dd77c02c2e0d9848803979ac599e431c0f282419ba2be72ea241de10479930989bc7073ed57
@@ -1,5 +1,19 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.10.0] - 2020-07-27
4
+
5
+ ### Added
6
+ * Add diff option to file conflict menu to show file contents differences
7
+ * Add :header option to #diff to show two-line header for compared files
8
+ * Add git-like hunk coloring when displaying diff lines in unified format
9
+
10
+ ### Changed
11
+ * Change to only use keyword arguments in all methods
12
+ * Change #tail_file to accept :lines as keyword parameter
13
+ * Change to update tty-prompt & pastel dependencies
14
+ * Change prompt to be quiet and provide no output after selection
15
+ * Change #diff_files to stop raising on binary or large files
16
+
3
17
  ## [v0.9.0] - 2020-04-28
4
18
 
5
19
  ### Changed
@@ -106,6 +120,7 @@
106
120
 
107
121
  * Initial implementation and release
108
122
 
123
+ [v0.10.0]: https://github.com/piotrmurach/tty-file/compare/v0.9.0...v0.10.0
109
124
  [v0.9.0]: https://github.com/piotrmurach/tty-file/compare/v0.8.0...v0.9.0
110
125
  [v0.8.0]: https://github.com/piotrmurach/tty-file/compare/v0.7.1...v0.8.0
111
126
  [v0.7.1]: https://github.com/piotrmurach/tty-file/compare/v0.7.0...v0.7.1
data/README.md CHANGED
@@ -30,7 +30,7 @@ Though Ruby's `File` and `FileUtils` libraries provide very robust apis for deal
30
30
  Add this line to your application's Gemfile:
31
31
 
32
32
  ```ruby
33
- gem 'tty-file'
33
+ gem "tty-file"
34
34
  ```
35
35
 
36
36
  And then execute:
@@ -64,7 +64,7 @@ Or install it yourself as:
64
64
  ## 1. Usage
65
65
 
66
66
  ```ruby
67
- TTY::File.replace_in_file('Gemfile', /gem 'rails'/, "gem 'hanami'")
67
+ TTY::File.replace_in_file("Gemfile", /gem 'rails'/, "gem 'hanami'")
68
68
  ```
69
69
 
70
70
  ## 2. Interface
@@ -78,7 +78,7 @@ If you wish to silence verbose output use `verbose: false`. Similarly if you wis
78
78
  To check whether a file is a binary file, i.e. image, executable etc. do:
79
79
 
80
80
  ```ruby
81
- TTY::File.binary?('image.png') # => true
81
+ TTY::File.binary?("image.png") # => true
82
82
  ```
83
83
 
84
84
  ### 2.2. checksum_file
@@ -93,7 +93,7 @@ Among the supported message digest algorithms are:
93
93
  For example, to create a digest for a string using `SHA1` do:
94
94
 
95
95
  ```ruby
96
- TTY::File.checksum_file("Some content\nThe end", 'sha1')
96
+ TTY::File.checksum_file("Some content\nThe end", "sha1")
97
97
  # => "289388f187404135e6c15b21460442cf867180dd"
98
98
  ```
99
99
 
@@ -102,19 +102,19 @@ TTY::File.checksum_file("Some content\nThe end", 'sha1')
102
102
  To change file modes use `chmod`, like so:
103
103
 
104
104
  ```ruby
105
- TTY::File.chmod('filename.rb', 0777)
105
+ TTY::File.chmod("filename.rb", 0777)
106
106
  ```
107
107
 
108
108
  There are a number of constants available to represent common mode bits such as `TTY::File::U_R` and `TTY::File::O_X`, and they can be used as follows:
109
109
 
110
110
  ```ruby
111
- TTY::File.chmod('filename.rb', TTY::File::U_R | TTY::File::O_X)
111
+ TTY::File.chmod("filename.rb", TTY::File::U_R | TTY::File::O_X)
112
112
  ```
113
113
 
114
114
  Apart from traditional octal number definition for file permissions, you can use the more convenient permission notation used by the Unix `chmod` command:
115
115
 
116
116
  ```ruby
117
- TTY::File.chmod('filename.rb', 'u=wrx,g+x')
117
+ TTY::File.chmod("filename.rb", "u=wrx,g+x")
118
118
  ```
119
119
 
120
120
  The `u`, `g`, and `o` specify the user, group, and other parts of the mode bits. The `a` symbol is equivalent to `ugo`.
@@ -124,13 +124,13 @@ The `u`, `g`, and `o` specify the user, group, and other parts of the mode bits.
124
124
  Copies a file's contents from a relative source to a relative destination.
125
125
 
126
126
  ```ruby
127
- TTY::File.copy_file 'Gemfile', 'Gemfile.bak'
127
+ TTY::File.copy_file "Gemfile", "Gemfile.bak"
128
128
  ```
129
129
 
130
130
  If you provide a block then the file content is yielded:
131
131
 
132
132
  ```ruby
133
- TTY::File.copy_file('Gemfile', 'app/Gemfile') do |content|
133
+ TTY::File.copy_file("Gemfile", "app/Gemfile") do |content|
134
134
  "https://rubygems.org\n" + content
135
135
  end
136
136
  ```
@@ -139,25 +139,25 @@ If the source file is an `ERB` template then you can provide a `:context` in whi
139
139
 
140
140
  ```ruby
141
141
  variables = OpenStruct.new
142
- variables[:foo] = 'bar'
142
+ variables[:foo] = "bar"
143
143
 
144
- TTY::File.copy_file('templates/application.html.erb', context: variables)
144
+ TTY::File.copy_file("templates/application.html.erb", context: variables)
145
145
  ```
146
146
 
147
147
  You can also specify the template name surrounding any dynamic variables with `%` to be evaluated:
148
148
 
149
149
  ```ruby
150
150
  variables = OpenStruct.new
151
- variables[:file_name] = 'foo'
151
+ variables[:file_name] = "foo"
152
152
 
153
- TTY::File.copy_file('templates/%file_name%.rb', context: variables)
153
+ TTY::File.copy_file("templates/%file_name%.rb", context: variables)
154
154
  # => Creates templates/foo.rb
155
155
  ```
156
156
 
157
157
  If the destination is a directory, then copies source inside that directory.
158
158
 
159
159
  ```ruby
160
- TTY::File.copy_file 'docs/README.md', 'app'
160
+ TTY::File.copy_file "docs/README.md", "app"
161
161
  ```
162
162
 
163
163
  If the destination file already exists, a prompt menu will be displayed to enquire about action:
@@ -165,24 +165,50 @@ If the destination file already exists, a prompt menu will be displayed to enqui
165
165
  If you wish to preserve original owner, group, permission and modified time use `:preserve` option:
166
166
 
167
167
  ```ruby
168
- TTY::File.copy_file 'docs/README.md', 'app', preserve: true
168
+ TTY::File.copy_file "docs/README.md", "app", preserve: true
169
169
  ```
170
170
 
171
171
  ### 2.5. create_file
172
172
 
173
- To create a file at a given destination with the given content use `create_file`:
173
+ To create a file at a given destination with some content use `create_file`:
174
174
 
175
175
  ```ruby
176
- TTY::File.create_file 'docs/README.md', '## Title header'
176
+ TTY::File.create_file "file-a/README.md", content
177
177
  ```
178
178
 
179
- On collision with already existing file, a menu is displayed:
179
+ On collision with already existing file, a menu gets displayed:
180
+
181
+ ```
182
+ collision examples/file-a
183
+ Overwrite examples/file-a? (enter "h" for help) [y,d,n,q,h]
184
+ ```
185
+
186
+ The `d` option allows to compare the changes:
187
+
188
+ ```
189
+ --- a/examples/file-a
190
+ +++ b/examples/file-a
191
+ @@ -1,8 +1,9 @@
192
+ aaaaa
193
+ bbbbb
194
+ -ccccc
195
+ +xxxxx
196
+ +
197
+ ddddd
198
+ eeeee
199
+ fffff
200
+ -ggggg
201
+ +yyyyy
202
+ Overwrite examples/file-a? (enter "h" for help) [y,d,n,q,h]
203
+ ````
180
204
 
181
205
  You can force to always overwrite file with `:force` option or always skip by providing `:skip`.
182
206
 
207
+ There is [examples/overwrite.rb](examples/overwrite.rb) that demonstrates diffing file with new content.
208
+
183
209
  ### 2.6. copy_dir
184
210
 
185
- To recursively copy a directory of files from source to destination location use `copy_directory` or its alias 'copy_dir'.
211
+ To recursively copy a directory of files from source to destination location use `copy_directory` or its alias `copy_dir`.
186
212
 
187
213
  Assuming you have the following directory structure:
188
214
 
@@ -197,7 +223,7 @@ Assuming you have the following directory structure:
197
223
  You can copy `doc` folder to `docs` by invoking:
198
224
 
199
225
  ```ruby
200
- TTY::File.copy_directory('doc', 'docs', context: ...)
226
+ TTY::File.copy_directory("doc", "docs", context: ...)
201
227
  ```
202
228
 
203
229
  The `context` needs to respond to `name` message and given it returns `foo` value the following directory gets created:
@@ -213,13 +239,13 @@ The `context` needs to respond to `name` message and given it returns `foo` valu
213
239
  If you only need to copy top level files use option `recursive: false`:
214
240
 
215
241
  ```ruby
216
- TTY::File.copy_directory('doc', 'docs', recursive: false)
242
+ TTY::File.copy_directory("doc", "docs", recursive: false)
217
243
  ```
218
244
 
219
245
  By passing `:exclude` option you can instruct the method to ignore any files including the given pattern:
220
246
 
221
247
  ```ruby
222
- TTY::File.copy_directory('doc', 'docs', exclude: 'subcommands')
248
+ TTY::File.copy_directory("doc", "docs", exclude: "subcommands")
223
249
  ```
224
250
 
225
251
  ### 2.7. create_dir
@@ -227,21 +253,21 @@ TTY::File.copy_directory('doc', 'docs', exclude: 'subcommands')
227
253
  To create directory use `create_directory` or its alias `create_dir` passing as a first argument file path:
228
254
 
229
255
  ```ruby
230
- TTY::File.create_dir('/path/to/directory')
256
+ TTY::File.create_dir("/path/to/directory")
231
257
  ```
232
258
 
233
259
  Or a data structure describing the directory tree including any files with or without content:
234
260
 
235
261
  ```ruby
236
262
  tree =
237
- 'app' => [
238
- 'README.md',
239
- ['Gemfile', "gem 'tty-file'"],
240
- 'lib' => [
241
- 'cli.rb',
242
- ['file_utils.rb', "require 'tty-file'"]
263
+ "app" => [
264
+ "README.md",
265
+ ["Gemfile", "gem 'tty-file'"],
266
+ "lib" => [
267
+ "cli.rb",
268
+ ["file_utils.rb", "require 'tty-file'"]
243
269
  ]
244
- 'spec' => []
270
+ "spec" => []
245
271
  ]
246
272
  ```
247
273
 
@@ -260,7 +286,7 @@ TTY::File.create_dir(tree)
260
286
  As a second argument you can provide a parent directory, otherwise current directory will be assumed:
261
287
 
262
288
  ```ruby
263
- TTY::File.create_dir(tree, '/path/to/parent/dir')
289
+ TTY::File.create_dir(tree, "/path/to/parent/dir")
264
290
  ```
265
291
 
266
292
  ### 2.8. diff_files
@@ -268,42 +294,85 @@ TTY::File.create_dir(tree, '/path/to/parent/dir')
268
294
  To compare files line by line in a system independent way use `diff`, or `diff_files`:
269
295
 
270
296
  ```ruby
271
- TTY::File.diff_files('file_a', 'file_b')
272
- # =>
273
- # @@ -1,4 +1,4 @@
274
- # aaa
275
- # -bbb
276
- # +xxx
277
- # ccc
297
+ print TTY::File.diff_files("file-a", "file-b")
278
298
  ```
279
299
 
280
- You can also pass additional arguments such as `:format`, `:context_lines` and `:threshold`.
300
+ Printing output to console would result in:
301
+
302
+ ```
303
+ diff examples/file-a and examples/file-b
304
+ --- examples/file-a
305
+ +++ examples/file-b
306
+ @@ -1,8 +1,9 @@
307
+ aaaaa
308
+ bbbbb
309
+ -ccccc
310
+ +xxxxx
311
+ +
312
+ ddddd
313
+ eeeee
314
+ fffff
315
+ -ggggg
316
+ +yyyyy
317
+ ```
281
318
 
282
- Accepted formats are `:old`, `:unified`, `:context`, `:ed`, `:reverse_ed`, by default the `:unified` format is used.
319
+ You can also pass additional parameters such as:
283
320
 
284
- The `:context_lines` specifies how many extra lines around the differing lines to include in the output. By default its 3 lines.
321
+ * `:format` - accepted values are `:unified`, `:old`, `:context` and `:ed`. Defaults to `:unified` as seen in the output above - similar to git tool.
322
+ * `:lines` - how many extra lines to include in the output around the compared lines. Defaults to `3` lines.
323
+ * `:threshold` - set maximum file size in bytes. By default files larger than `10Mb` are no processed.
324
+ * `:header` - controls display of two-line files comparison. By default `true`.
285
325
 
286
- The `:threshold` sets maximum file size in bytes, by default files larger than `10Mb` are not processed.
326
+ Changing format to `:old`, removing context lines and skipping log output:
287
327
 
288
328
  ```ruby
289
- TTY::File.diff_files('file_a', 'file_b', format: :old)
290
- # =>
291
- # 1,4c1,4
292
- # < aaa
293
- # < bbb
294
- # < ccc
295
- # ---
296
- # > aaa
297
- # > xxx
298
- # > ccc
329
+ TTY::File.diff_files("file_a", "file_b", format: :old, lines: 0, verbose: false)
330
+ ```
331
+
332
+ Results in the following output:
333
+
334
+ ```
335
+ <<< examples/file-a
336
+ >>> examples/file-b
337
+ 3c3,4
338
+ < ccccc
339
+ ---
340
+ > xxxxx
341
+ >
342
+
343
+ 7c8
344
+ < ggggg
345
+ ---
346
+ > yyyyy
299
347
  ```
300
348
 
301
- Equally, you can perform a comparison between a file content and a string content like so:
349
+ In addition, you can perform a comparison between a file and a string or between two strings. For example, comparing file with content:
302
350
 
303
351
  ```ruby
304
- TTY::File.diff_files('/path/to/file', 'some long text')
352
+ TTY::File.diff_files("file-a", "new\nlong\ntext")
305
353
  ```
306
354
 
355
+ Will output:
356
+
357
+ ```
358
+ diff a/examples/file-a and b/examples/file-a
359
+ --- a/examples/file-a
360
+ +++ b/examples/file-a
361
+ @@ -1,8 +1,4 @@
362
+ -aaaaa
363
+ -bbbbb
364
+ -ccccc
365
+ -ddddd
366
+ -eeeee
367
+ -fffff
368
+ -ggggg
369
+ +new
370
+ +long
371
+ +text
372
+ ````
373
+
374
+ Please run [examples/diff.rb](examples/diff.rb) to see how output works.
375
+
307
376
  ### 2.9. download_file
308
377
 
309
378
  To download a content from a given address and to save at a given relative location do:
@@ -332,13 +401,13 @@ TTY::File.download_file("https://gist.github.com/4701967", "doc/README.md", limi
332
401
  Inject content into a file at a given location and return `true` when performed successfully, `false` otherwise.
333
402
 
334
403
  ```ruby
335
- TTY::File.inject_into_file 'filename.rb', "text to add", after: "Code below this line\n"
404
+ TTY::File.inject_into_file "filename.rb", "text to add", after: "Code below this line\n"
336
405
  ```
337
406
 
338
407
  Or using a block:
339
408
 
340
409
  ```ruby
341
- TTY::File.inject_into_file 'filename.rb', after: "Code below this line\n" do
410
+ TTY::File.inject_into_file "filename.rb", after: "Code below this line\n" do
342
411
  "text to add"
343
412
  end
344
413
  ```
@@ -348,13 +417,13 @@ You can also use Regular Expressions in `:after` or `:before` to match file loca
348
417
  By default, this method will always inject content into file, regardless whether it is already present or not. To change this pass `:force` set to `false` to perform check before actually inserting text:
349
418
 
350
419
  ```ruby
351
- TTY::File.inject_into_file('filename.rb', "text to add", after: "Code below this line\n"
420
+ TTY::File.inject_into_file("filename.rb", "text to add", after: "Code below this line\n"
352
421
  ```
353
422
 
354
423
  Alternatively, use `safe_inject_into_file` to check if the text can be safely inserted.
355
424
 
356
425
  ```ruby
357
- TTY::File.safe_inject_into_file('Gemfile', "gem 'tty'")
426
+ TTY::File.safe_inject_into_file("Gemfile", "gem 'tty'")
358
427
  ```
359
428
 
360
429
  The [append_to_file](#212-append_to_file) and [prepend_to_file](#213-prepend_to_file) allow you to add content at the end and the begging of a file.
@@ -364,14 +433,14 @@ The [append_to_file](#212-append_to_file) and [prepend_to_file](#213-prepend_to_
364
433
  Replace content of a file matching condition by calling `replace_in_file` or `gsub_file`, which returns `true` when substitutions are performed successfully, `false` otherwise.
365
434
 
366
435
  ```ruby
367
- TTY::File.replace_in_file 'filename.rb', /matching condition/, 'replacement'
436
+ TTY::File.replace_in_file "filename.rb", /matching condition/, "replacement"
368
437
  ```
369
438
 
370
439
  The replacement content can be provided in a block
371
440
 
372
441
  ```ruby
373
- TTY::File.gsub_file 'filename.rb', /matching condition/ do
374
- 'replacement'
442
+ TTY::File.gsub_file "filename.rb", /matching condition/ do
443
+ "replacement"
375
444
  end
376
445
  ```
377
446
 
@@ -380,13 +449,13 @@ end
380
449
  Appends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
381
450
 
382
451
  ```ruby
383
- TTY::File.append_to_file('Gemfile', "gem 'tty'")
452
+ TTY::File.append_to_file("Gemfile", "gem 'tty'")
384
453
  ```
385
454
 
386
455
  Or inside a block:
387
456
 
388
457
  ```ruby
389
- TTY::File.append_to_file('Gemfile') do
458
+ TTY::File.append_to_file("Gemfile") do
390
459
  "gem 'tty'"
391
460
  end
392
461
  ```
@@ -394,13 +463,13 @@ end
394
463
  By default, this method will always append content regardless whether it is already present or not. To change this pass `:force` set to `false` to perform check before actually appending:
395
464
 
396
465
  ```ruby
397
- TTY::File.append_to_file('Gemfile', "gem 'tty'", force: false)
466
+ TTY::File.append_to_file("Gemfile", "gem 'tty'", force: false)
398
467
  ```
399
468
 
400
469
  Alternatively, use `safe_append_to_file` to check if the text can be safely appended.
401
470
 
402
471
  ```ruby
403
- TTY::File.safe_append_to_file('Gemfile', "gem 'tty'")
472
+ TTY::File.safe_append_to_file("Gemfile", "gem 'tty'")
404
473
  ```
405
474
 
406
475
  ### 2.13. prepend_to_file
@@ -408,13 +477,13 @@ TTY::File.safe_append_to_file('Gemfile', "gem 'tty'")
408
477
  Prepends text to a file and returns `true` when performed successfully, `false` otherwise. You can provide the text as a second argument:
409
478
 
410
479
  ```ruby
411
- TTY::File.prepend_to_file('Gemfile', "gem 'tty'")
480
+ TTY::File.prepend_to_file("Gemfile", "gem 'tty'")
412
481
  ```
413
482
 
414
483
  Or inside a block:
415
484
 
416
485
  ```ruby
417
- TTY::File.prepend_to_file('Gemfile') do
486
+ TTY::File.prepend_to_file("Gemfile") do
418
487
  "gem 'tty'"
419
488
  end
420
489
  ```
@@ -422,13 +491,13 @@ end
422
491
  By default, this method will always prepend content regardless whether it is already present or not. To change this pass `:force` set to `false` to perform check before actually prepending:
423
492
 
424
493
  ```ruby
425
- TTY::File.prepend_to_file('Gemfile', "gem 'tty'", force: false)
494
+ TTY::File.prepend_to_file("Gemfile", "gem 'tty'", force: false)
426
495
  ```
427
496
 
428
497
  Alternatively, use `safe_prepend_to_file` to check if the text can be safely appended.
429
498
 
430
499
  ```ruby
431
- TTY::File.safe_prepend_to_file('Gemfile', "gem 'tty'")
500
+ TTY::File.safe_prepend_to_file("Gemfile", "gem 'tty'")
432
501
  ```
433
502
 
434
503
  ### 2.14. remove_file
@@ -436,13 +505,13 @@ TTY::File.safe_prepend_to_file('Gemfile', "gem 'tty'")
436
505
  To remove a file do:
437
506
 
438
507
  ```ruby
439
- TTY::File.remove_file 'doc/README.md'
508
+ TTY::File.remove_file "doc/README.md"
440
509
  ```
441
510
 
442
511
  You can also pass in `:force` to remove file ignoring any errors:
443
512
 
444
513
  ```ruby
445
- TTY::File.remove_file 'doc/README.md', force: true
514
+ TTY::File.remove_file "doc/README.md", force: true
446
515
  ```
447
516
 
448
517
  ### 2.15. tail_file
@@ -450,14 +519,14 @@ TTY::File.remove_file 'doc/README.md', force: true
450
519
  To read the last 10 lines from a file do:
451
520
 
452
521
  ```ruby
453
- TTY::File.tail_file 'doc/README.md'
454
- # => ['## Copyright', 'Copyright (c) 2016-2017', ...]
522
+ TTY::File.tail_file "doc/README.md"
523
+ # => ["## Copyright", "Copyright (c) 2016-2017", ...]
455
524
  ```
456
525
 
457
526
  You can also pass a block:
458
527
 
459
528
  ```ruby
460
- TTY::File.tail_file('doc/README.md') do |line|
529
+ TTY::File.tail_file("doc/README.md") do |line|
461
530
  puts line
462
531
  end
463
532
  ```
@@ -465,7 +534,7 @@ end
465
534
  To change how many lines are read pass a second argument:
466
535
 
467
536
  ```ruby
468
- TTY::File.tail_file('doc/README.md', 15)
537
+ TTY::File.tail_file("doc/README.md", 15)
469
538
  ```
470
539
 
471
540
  ## Development