Last active
September 5, 2020 20:16
-
-
Save aurelienbottazini/52a2b5b22dc460b9144097ce3769cd62 to your computer and use it in GitHub Desktop.
Sublime text command to search with rg (ripgrep) in wsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import sys | |
import os | |
import re | |
import subprocess | |
class WslSearchCommand(sublime_plugin.WindowCommand): | |
def run(self, type="search"): | |
view = self.window.active_view() | |
selection_text = view.substr(view.sel()[0]) | |
self.window.show_input_panel( | |
"Search in project:", | |
selection_text, | |
self.input_panel_done, | |
None, | |
None) | |
def input_panel_done(self, search_input): | |
project_dir_path = self.window.folders()[0][2:].replace("\\", "/") | |
subprocess_args = ['wsl.exe', 'rg', '-n', '--max-columns', '100', '--max-columns-preview', '--column', | |
search_input, project_dir_path] | |
output = subprocess.check_output(subprocess_args, startupinfo = self.no_startup_window()).decode('utf-8').strip() | |
output = re.sub('^', 'Z:', output, flags=re.M) | |
self.result_view(output) | |
def no_startup_window(self): | |
startupinfo = None | |
if os.name == 'nt': | |
startupinfo = subprocess.STARTUPINFO() | |
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW | |
return startupinfo | |
def result_view(self, results): | |
panel = self.window.create_output_panel('*WSL_SEARCH_RESULTS*') | |
panel.settings().set("result_file_regex", r"(.*):(\d+):(\d+):(.*)") | |
self.window.run_command('show_panel', {"panel":"output.*WSL_SEARCH_RESULTS*"}) | |
panel.run_command("append", {"characters": results}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment