cmdline-sub 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README +3 -3
  3. data/bin/sub +27 -8
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8333cc1f6aa8e4eb3b47299c6038d0c04d26b9b0b8e4b2292e54906fc9e766d
4
- data.tar.gz: 2b6fa1a2ef48f3ff9fec5aa017c7e4ef94e977085f424e64a4c9378822dc0a4b
3
+ metadata.gz: c389766a40ec1f0894ff39ae9156d8ab1f737e32da5e6a83ac82e758fd420e06
4
+ data.tar.gz: cbe56894e3d43b63ec14da75db54b820c9347a197e32c9ae6a5cc649ddabdb1c
5
5
  SHA512:
6
- metadata.gz: 3b162147e9a995230fdb3744963af0fa237ec96fe9f6088b561ce82de7a7a6d338096715dda296f2ad5c6060d2d3e826017aa346c283cb79662723884acd64c6
7
- data.tar.gz: 2bb7d2a20e123d67ddb0ee09e8c2de7b7e0e46d6a39d31b3364df381b9f9481d15020df0096a0edc0b140b5a3d8ebae97926717f8cb2609fa5f7ea9861f40a41
6
+ metadata.gz: 71806a610d4b0a4e3e14e6ed75ab31f0dc4d1c34ac72767b3b8e063ed139322685f1c0585929f58f447a25e07f717171892d7ed35ace82f4a8d67ad9dd00b394
7
+ data.tar.gz: d08f7dd174f7546d4b451a5ee6a0639eea308ca894db10c861dbd3556efbe6965770c2529a822da2a64821de52a68444d3771fa253a9e6f46ba17923e65b77fb
data/README CHANGED
@@ -1,7 +1,7 @@
1
1
  Sub
2
2
  ===
3
3
 
4
- sub v0.1.2
4
+ sub v0.1.3
5
5
  Usage: sub COMMANDLINE -- (PATTERN/SUBSTITUTION[/SUB_FLAGS])+ (/GLOBAL_FLAGS)?
6
6
 
7
7
  sub substitutes the matching pattern in every word in the command line with the
@@ -53,8 +53,8 @@ Substitution flags:
53
53
  ex: sub cp here.txt there.txt -- CP/mv/i #=> mv here.txt there.txt
54
54
  -g: General substitution: substitute all matches in the word, not just the first
55
55
  ex: sub cp here.txt there.txt -- ./_/g #=> __ ________ _________
56
- -e: Expand * in commandline after replacements are made
57
- ex: sub ls mydir -- mydir/*/e #=> ls bin README
56
+ -r: Don't escape substituted value, allow shell to expand and split it
57
+ ex: sub ls mydir -- 'mydir/*/r' #=> ls bin README
58
58
  Global flags:
59
59
  -p: Print the command instead of executing it (adds newline)
60
60
  -P: Print the command instead of executing it (doesn't add newline)
data/bin/sub CHANGED
@@ -5,8 +5,9 @@
5
5
  if ENV["TESTING_SUB_CMD"] == "1" && ENV["DEBUGGING_SUB_CMD"] == "1"
6
6
  require "debug"
7
7
  end
8
+ require "shellwords"
8
9
 
9
- VERSION = "0.1.2"
10
+ VERSION = "0.1.3"
10
11
 
11
12
  USAGE = <<EOF
12
13
  Usage: sub COMMANDLINE -- (PATTERN/SUBSTITUTION[/SUB_FLAGS])+ (/GLOBAL_FLAGS)?
@@ -89,8 +90,8 @@ SUB_FLAGS = [
89
90
  { flag: 'g', name: :general, desc: "General substitution: substitute all matches in the word, not just the first",
90
91
  example: "sub cp here.txt there.txt -- ./_/g #=> __ ________ _________",
91
92
  },
92
- { flag: 'e', name: :expand_star, desc: "Expand * in commandline after replacements are made",
93
- example: [ "sub ls mydir -- mydir/*/e #=> ls bin README" ]
93
+ { flag: 'r', name: :raw_mode, desc: "Don't escape substituted value, allow shell to expand and split it",
94
+ example: [ "sub ls mydir -- 'mydir/*/r' #=> ls bin README" ]
94
95
  },
95
96
  ]
96
97
  GLOBAL_FLAGS = [
@@ -234,11 +235,14 @@ def parse_sub_strs(sub_strs)
234
235
  flags_hash = {
235
236
  first_match: parse_simple_flag!(flags, flag_by_name(:first_match)),
236
237
  last_match: parse_simple_flag!(flags, flag_by_name(:last_match)),
237
- expand_star: parse_simple_flag!(flags, flag_by_name(:expand_star)),
238
+ raw_mode: parse_simple_flag!(flags, flag_by_name(:raw_mode)),
238
239
  literal: parse_simple_flag!(flags, flag_by_name(:literal)),
239
240
  ignorecase: parse_simple_flag!(flags, flag_by_name(:ignorecase)),
240
241
  general: parse_simple_flag!(flags, flag_by_name(:general)),
241
242
  }
243
+ if flags_hash[:raw_mode]
244
+ flags_hash[:raw_mode] = 0 # bitfield
245
+ end
242
246
  pats << pat
243
247
  subs << sub
244
248
  match_flags << flags_hash
@@ -303,6 +307,9 @@ def argv_replace!(argv, regexps, subs, flags)
303
307
  scan_size = arg.scan(regexp).size
304
308
  scan_size = 1 if scan_size > 1
305
309
  end
310
+ if flags[i].fetch(:raw_mode)
311
+ flags[i][:raw_mode] |= i+1
312
+ end
306
313
  if new_arg != arg || new_arg == subs[i]
307
314
  num_replacements += scan_size
308
315
  end
@@ -359,7 +366,7 @@ def copy!(cmd_line, global_flags)
359
366
  end
360
367
  end
361
368
 
362
- def exec_cmd(argv, regexps, subs, raw_flags, num_replacements, global_flags)
369
+ def exec_cmd(argv, regexps, subs, flags_hashes, raw_flags, global_flags, num_replacements)
363
370
  if global_flags.fetch(:debug)
364
371
  puts "Patterns: #{regexps.inspect}"
365
372
  puts "Substitutions: #{subs.inspect}"
@@ -382,7 +389,19 @@ def exec_cmd(argv, regexps, subs, raw_flags, num_replacements, global_flags)
382
389
  end
383
390
 
384
391
  cmd = argv.shift
385
- cmd_line = "#{cmd} #{argv.join(' ')}"
392
+ argv_string = String.new
393
+ argv.each_with_index do |arg, i|
394
+ raw_mode = flags_hashes.any? { |flags_hash| flags_hash[:raw_mode] && (flags_hash[:raw_mode] & i+1) != 0 }
395
+ if raw_mode
396
+ argv_string << arg
397
+ else
398
+ argv_string << Shellwords.escape(arg)
399
+ end
400
+ unless argv[i+1].nil?
401
+ argv_string << " "
402
+ end
403
+ end
404
+ cmd_line = "#{cmd} #{argv_string}"
386
405
  action = global_flags.fetch(:copy_to_clipboard) ? "copy" : "execute"
387
406
  if global_flags.fetch(:interactive) && !global_flags.fetch(:print_only)
388
407
  $stdout.puts "Would you like to #{action} the following command? [y(es),n(o)]"
@@ -401,7 +420,7 @@ def exec_cmd(argv, regexps, subs, raw_flags, num_replacements, global_flags)
401
420
  copy!(cmd_line, global_flags) if global_flags.fetch(:copy_to_clipboard)
402
421
  exit 0 if global_flags.fetch(:print_only) || global_flags.fetch(:print_only_no_newline) || global_flags.fetch(:copy_to_clipboard)
403
422
  begin
404
- exec cmd, *argv
423
+ exec cmd_line
405
424
  rescue SystemCallError => e
406
425
  if e.class == Errno::ENOENT
407
426
  # act like a shell
@@ -413,4 +432,4 @@ def exec_cmd(argv, regexps, subs, raw_flags, num_replacements, global_flags)
413
432
  end
414
433
  end
415
434
 
416
- exec_cmd(argv, regexps, subs, raw_flags, num_replacements, global_flags)
435
+ exec_cmd(argv, regexps, subs, flags_hashes, raw_flags, global_flags, num_replacements)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdline-sub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Gruber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-26 00:00:00.000000000 Z
11
+ date: 2024-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debug