Add clipboard support
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
1c8404e029
commit
cc54900498
2 changed files with 593 additions and 497 deletions
1022
LICENSE.md
1022
LICENSE.md
File diff suppressed because it is too large
Load diff
36
SSnR.py
36
SSnR.py
|
@ -27,24 +27,29 @@ import os.path
|
|||
from os import walk
|
||||
import argparse
|
||||
import regex
|
||||
import pyperclip
|
||||
|
||||
def main():
|
||||
"""
|
||||
Main function
|
||||
"""
|
||||
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
||||
parser.add_argument('-e', '--regex', help='Regex', required=True)
|
||||
parser.add_argument('-s', '--string', help='String', required=False)
|
||||
parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+')
|
||||
parser.add_argument('-p', '--regex_input', help='Regex input file', required=False)
|
||||
parser.add_argument('-l', '--regex_output', help='Regex output file', required=False)
|
||||
parser.add_argument('-o', '--output', help='Output file', required=False)
|
||||
parser.add_argument('-r', '--replace', help='Replace', required=False)
|
||||
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
|
||||
parser.add_argument('-ex', '--regex', help='Regex', required=True)
|
||||
parser.add_argument('-str', '--string', help='Input string', required=False)
|
||||
parser.add_argument('-if', '--input', help='Input file', required=False, nargs='+')
|
||||
parser.add_argument('-iex', '--regex_input', help='Regex input file', required=False)
|
||||
parser.add_argument('-oex', '--regex_output', help='Regex output file', required=False)
|
||||
parser.add_argument('-of', '--output', help='Output file', required=False)
|
||||
parser.add_argument('-rex', '--replace', help='Replace', required=False)
|
||||
parser.add_argument('-pm', '--print_nb_match', help='Print the number of match in replace',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
|
||||
parser.add_argument('-igc', '--ignore_case', help='Ignore the case',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-u', '--recursive', help='Use the regex input recrusivly in the folders',
|
||||
parser.add_argument('-r', '--recursive', help='Use the regex input recrusivly in the folders',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-ic', '--input_clipboard', help='Use the clipboard as input',
|
||||
required=False, action='store_true')
|
||||
parser.add_argument('-oc', '--output_clipboard', help='Use the clipboard as output',
|
||||
required=False, action='store_true')
|
||||
|
||||
args = vars(parser.parse_args())
|
||||
|
@ -93,6 +98,9 @@ def main():
|
|||
elif args["string"] is not None:
|
||||
string = args["string"]
|
||||
is_file = False
|
||||
elif args["input_clipboard"]:
|
||||
string = pyperclip.paste()
|
||||
is_file = False
|
||||
else:
|
||||
string = sys.stdin.read()
|
||||
is_file = False
|
||||
|
@ -109,8 +117,12 @@ def main():
|
|||
elif args["output"] is not None:
|
||||
output_filenames.append(args["output"])
|
||||
use_output_file = True
|
||||
elif args["output_clipboard"]:
|
||||
use_output_file = False
|
||||
use_output_clipboard = True
|
||||
else:
|
||||
use_output_file = False
|
||||
use_output_clipboard = False
|
||||
|
||||
# Search or replace
|
||||
file_index = 0
|
||||
|
@ -139,6 +151,8 @@ def main():
|
|||
print(" - Number of replace: " + str(nb_replace))
|
||||
if len(output_filenames) > 1:
|
||||
file_index += 1
|
||||
elif use_output_clipboard:
|
||||
pyperclip.copy(replace_string)
|
||||
else:
|
||||
print(replace_string)
|
||||
if args["print_nb_match"]:
|
||||
|
@ -161,6 +175,8 @@ def main():
|
|||
print("Error: file not found: " + str(exception))
|
||||
return -1
|
||||
output_file.write(replace_string)
|
||||
elif use_output_clipboard:
|
||||
pyperclip.copy(replace_string)
|
||||
else:
|
||||
print(replace_string)
|
||||
if args["print_nb_match"]:
|
||||
|
|
Loading…
Reference in a new issue