Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # This Script is a wrapper for the mate command.
  4. # IT'S NOT A SUBSTITUTE!
  5. # If you want to pipe to Textmate from the terminal,
  6. # use the mate command instead.
  7. # Also, its slower than the mate command, so if you don't
  8. # mind typing a couple extra characters, but do mind one extra
  9. # second of waiting at most, then use the mate command.
  10.  
  11. # Make sure that the mate command is installed!
  12. system("which mate > /dev/null 2>/dev/null")
  13. unless $?.exitstatus == 0
  14. puts "The mate command was not found!"
  15. exit
  16. end
  17.  
  18. HOME = `echo $HOME`.chomp
  19. CWD = Dir.pwd
  20.  
  21. # Notes about this array:
  22. # Add a trailling slash only if you don't want the dir itself to be included
  23. # Example:
  24. # authorized_dirs = [ "#{HOME}/Sites/", "#{HOME}/Sandbox/", "#{HOME}/Sources/", "#{HOME}/bin" ]
  25. #
  26. # Here only the bin dir is included. The Sites, Sandbox and Sources dirs are not included,
  27. # but everything below them are.
  28. #
  29. # That means that if you use the m command with no argument under the Sources dir, it will open a new file in Textmate
  30. # but if you use the m command with no argument under the Sources/ruby dir or the bin dir, it will open the dir itself.
  31. AUTHORIZED_DIRS = [ "#{HOME}/Sites/", "#{HOME}/Sandbox/", "#{HOME}/Sources/", "#{HOME}/bin", "#{HOME}/.oh-my-zsh" ]
  32. VALID_OPTIONS = ["a","-async","w","-wait","l","-line","r","-recent","d","-change-dir","n","-no-reactivation","h","-help","v","-version"]
  33.  
  34.  
  35. def validate_dir(dir)
  36.  
  37. AUTHORIZED_DIRS.each do |d|
  38. return dir != d.chop if dir.include?(d)
  39. end
  40. false
  41. end
  42.  
  43. def validate_arg(arg)
  44. if arg[0] == "-"
  45. if VALID_OPTIONS.include? arg[1..-1]
  46. return true
  47. end
  48.  
  49. invalid_options = []
  50. arg[1..-1].split(//).each do |char|
  51. if !VALID_OPTIONS.include? char
  52. invalid_options << char
  53. end
  54. end
  55.  
  56. return true if invalid_options.empty?
  57.  
  58. if !File.exist? arg
  59. options_label = invalid_options.size > 1 ? "options" : "option"
  60. puts invalid_options.size.to_s + " unknown #{options_label}:"
  61. invalid_options.map { |option| puts "\t-" + option }
  62. exit
  63. end
  64. end
  65. end
  66.  
  67. if ARGV.empty?
  68. if validate_dir(CWD)
  69. exec 'mate .'
  70. else
  71. exec 'mate'
  72. end
  73. else
  74. # Remove the - argument(s) if it's not the only one passed. Otherwize use it.
  75. # The - argument can be use to read from stdin
  76. if ARGV.include? "-"
  77. ARGV.size > 1 ? ARGV.delete("-") : exec("mate -")
  78. end
  79.  
  80. # The -h and -v options have the highest precedence
  81. includes_h = ARGV.include? "h"
  82. includes_v = ARGV.include? "v"
  83. if includes_h && includes_v
  84. ARGV.index("h") > ARGV.index("v") ? exec("mate -v") : exec("mate -h")
  85. elsif includes_h
  86. exec("mate -h")
  87. elsif includes_v
  88. exec("mate -v")
  89. end
  90.  
  91. options = []
  92. files = []
  93. ARGV.each do |arg|
  94. if validate_arg arg
  95. options << arg
  96. else
  97. files << arg
  98. end
  99. end
  100. if files.empty?
  101. if validate_dir(CWD)
  102. exec "mate #{options.join(" ")} ."
  103. else
  104. exec "mate #{options.join(" ")}"
  105. end
  106. else
  107. exec "mate #{options.join(" ")} #{files.join(" ")}"
  108. end
  109. end
Add Comment
Please, Sign In to add comment