utils 0.67.0 → 0.69.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +251 -18
  3. data/Rakefile +1 -0
  4. data/bin/ascii7 +28 -0
  5. data/bin/blameline +17 -0
  6. data/bin/changes +69 -5
  7. data/bin/classify +128 -7
  8. data/bin/code_comment +102 -104
  9. data/bin/commit_message +26 -2
  10. data/bin/create_cstags +18 -0
  11. data/bin/create_tags +10 -0
  12. data/bin/discover +38 -1
  13. data/bin/edit +14 -1
  14. data/bin/edit_wait +14 -0
  15. data/bin/enum +139 -15
  16. data/bin/git-empty +50 -0
  17. data/bin/git-versions +20 -0
  18. data/bin/json_check +15 -1
  19. data/bin/long_lines +11 -2
  20. data/bin/myex +38 -0
  21. data/bin/on_change +22 -0
  22. data/bin/path +21 -0
  23. data/bin/print_method +29 -1
  24. data/bin/probe +52 -4
  25. data/bin/rainbow +102 -0
  26. data/bin/rd2md +15 -0
  27. data/bin/search +83 -1
  28. data/bin/sedit +6 -0
  29. data/bin/serve +18 -3
  30. data/bin/ssh-tunnel +14 -2
  31. data/bin/strip_spaces +17 -9
  32. data/bin/sync_dir +48 -1
  33. data/bin/untest +19 -1
  34. data/bin/utils-utilsrc +42 -6
  35. data/bin/vcf2alias +33 -0
  36. data/bin/yaml_check +24 -2
  37. data/lib/utils/config_dir.rb +127 -0
  38. data/lib/utils/config_file.rb +445 -1
  39. data/lib/utils/editor.rb +215 -3
  40. data/lib/utils/finder.rb +127 -16
  41. data/lib/utils/grepper.rb +90 -1
  42. data/lib/utils/irb.rb +387 -39
  43. data/lib/utils/line_blamer.rb +28 -0
  44. data/lib/utils/line_formatter.rb +198 -0
  45. data/lib/utils/md5.rb +14 -0
  46. data/lib/utils/patterns.rb +77 -3
  47. data/lib/utils/probe_server.rb +302 -23
  48. data/lib/utils/ssh_tunnel_specification.rb +58 -0
  49. data/lib/utils/version.rb +1 -1
  50. data/lib/utils/xt/source_location_extension.rb +18 -6
  51. data/lib/utils.rb +3 -1
  52. data/tests/utils_test.rb +7 -1
  53. data/utils.gemspec +7 -6
  54. metadata +22 -8
  55. data/bin/number_files +0 -26
  56. data/lib/utils/xdg_config.rb +0 -10
  57. /data/{COPYING → LICENSE} +0 -0
@@ -0,0 +1,127 @@
1
+ require 'pathname'
2
+ require 'stringio'
3
+
4
+ module Utils
5
+ class ConfigDir
6
+ # Initializes a new ConfigDir instance with the specified name and optional
7
+ # root path or environment variable.
8
+ #
9
+ # @param name [ String ] the name of the directory to be used
10
+ # @param root_path [ String, nil ] the root path to use; if nil, the
11
+ # default root path is used
12
+ # @param env_var [ String, nil ] the name of the environment variable to
13
+ # check for the root path
14
+ def initialize(name, root_path: nil, env_var: nil)
15
+ root_path ||= env_var_path(env_var)
16
+ @directory_path = derive_directory_path(name, root_path)
17
+ end
18
+
19
+ # Memoizes the foobar method's return value and returns the result of the computation.
20
+ # Initializes a new ConfigDir instance with the specified name and optional
21
+ # root path or environment variable.
22
+ #
23
+ # @param name [ String ] the name of the directory to be used
24
+ # @param root_path [ String, nil ] the root path to use; if nil, the
25
+ # default root path is used
26
+ # @param env_var [ String, nil ] the name of the environment variable to
27
+ # check for the root path
28
+ def initialize(name, root_path: nil, env_var: nil)
29
+ root_path ||= env_var_path(env_var)
30
+ @directory_path = derive_directory_path(name, root_path)
31
+ end
32
+
33
+ # Returns the string representation of the configuration directory path.
34
+ #
35
+ # @return [ String ] the path of the configuration directory as a string
36
+ def to_s
37
+ @directory_path.to_s
38
+ end
39
+
40
+ # Joins the directory path with the given path and returns the combined
41
+ # result.
42
+ #
43
+ # @param path [ String ] the path to be joined with the directory path
44
+ #
45
+ # @return [ Pathname ] the combined path as a Pathname object
46
+ def join(path)
47
+ @directory_path + path
48
+ end
49
+ alias + join
50
+
51
+ # Reads the content of a file at the given path within the configuration
52
+ # directory.
53
+ #
54
+ # If the file exists, it returns the file's content as a string encoded in
55
+ # UTF-8. If a block is given and the file exists, it opens the file and
56
+ # yields to the block.
57
+ # If the file does not exist and a default value is provided, it returns
58
+ # the default. If a block is given and the file does not exist, it yields a
59
+ # StringIO object containing
60
+ # the default value to the block.
61
+ #
62
+ # @param path [ String ] the path to the file relative to the configuration
63
+ # directory
64
+ # @param default [ String, nil ] the default value to return if the file
65
+ # does not exist
66
+ #
67
+ # @yield [ io ]
68
+ #
69
+ # @return [ String, nil ] the content of the file or the default value if
70
+ # the file does not exist
71
+ def read(path, default: nil, &block)
72
+ full_path = join(path)
73
+ if File.exist?(full_path)
74
+ if block
75
+ File.new(full_path, &block)
76
+ else
77
+ File.read(full_path, encoding: 'UTF-8')
78
+ end
79
+ else
80
+ if default && block
81
+ block.(StringIO.new(default))
82
+ else
83
+ default
84
+ end
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ # Derives the full directory path by combining the root path and the given
91
+ # name.
92
+ #
93
+ # @param name [ String ] the name of the directory to be appended to the root path
94
+ # @param root_path [ String, nil ] the root path to use; if nil, the default root path is used
95
+ #
96
+ # @return [ Pathname ] the combined directory path as a Pathname object
97
+ def derive_directory_path(name, root_path)
98
+ root = if path = root_path
99
+ Pathname.new(path)
100
+ else
101
+ Pathname.new(default_root_path)
102
+ end
103
+ root + name
104
+ end
105
+
106
+ # Returns the environment variable path if it is set and not empty.
107
+ #
108
+ # @param env_var [ String ] the name of the environment variable to check
109
+ # @return [ String, nil ] the value of the environment variable if it
110
+ # exists and is not empty, otherwise nil
111
+ def env_var_path(env_var)
112
+ env_var.full? { ENV[it].full? }
113
+ end
114
+
115
+ # Returns the default configuration directory path based on the HOME
116
+ # environment variable.
117
+ #
118
+ # This method constructs and returns a Pathname object pointing to the
119
+ # standard configuration directory location, which is typically
120
+ # $HOME/.config.
121
+ #
122
+ # @return [ Pathname ] the default configuration directory path
123
+ def default_root_path
124
+ Pathname.new(ENV.fetch('HOME') + '.config')
125
+ end
126
+ end
127
+ end