Add clipboard support

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
Rémi BERTHO 2017-11-03 21:32:19 +01:00
parent 1c8404e029
commit cc54900498
Signed by: dalan
GPG Key ID: EE3B917931C07B64
2 changed files with 593 additions and 497 deletions

1048
LICENSE.md

File diff suppressed because it is too large Load Diff

42
SSnR.py
View File

@ -27,25 +27,30 @@ import os.path
from os import walk from os import walk
import argparse import argparse
import regex import regex
import pyperclip
def main(): def main():
""" """
Main function Main function
""" """
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR') parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
parser.add_argument('-e', '--regex', help='Regex', required=True) parser.add_argument('-ex', '--regex', help='Regex', required=True)
parser.add_argument('-s', '--string', help='String', required=False) parser.add_argument('-str', '--string', help='Input string', required=False)
parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+') parser.add_argument('-if', '--input', help='Input file', required=False, nargs='+')
parser.add_argument('-p', '--regex_input', help='Regex input file', required=False) parser.add_argument('-iex', '--regex_input', help='Regex input file', required=False)
parser.add_argument('-l', '--regex_output', help='Regex output file', required=False) parser.add_argument('-oex', '--regex_output', help='Regex output file', required=False)
parser.add_argument('-o', '--output', help='Output file', required=False) parser.add_argument('-of', '--output', help='Output file', required=False)
parser.add_argument('-r', '--replace', help='Replace', required=False) parser.add_argument('-rex', '--replace', help='Replace', required=False)
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace', parser.add_argument('-pm', '--print_nb_match', help='Print the number of match in replace',
required=False, action='store_true') 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') 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') 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()) args = vars(parser.parse_args())
@ -93,6 +98,9 @@ def main():
elif args["string"] is not None: elif args["string"] is not None:
string = args["string"] string = args["string"]
is_file = False is_file = False
elif args["input_clipboard"]:
string = pyperclip.paste()
is_file = False
else: else:
string = sys.stdin.read() string = sys.stdin.read()
is_file = False is_file = False
@ -109,8 +117,12 @@ def main():
elif args["output"] is not None: elif args["output"] is not None:
output_filenames.append(args["output"]) output_filenames.append(args["output"])
use_output_file = True use_output_file = True
elif args["output_clipboard"]:
use_output_file = False
use_output_clipboard = True
else: else:
use_output_file = False use_output_file = False
use_output_clipboard = False
# Search or replace # Search or replace
file_index = 0 file_index = 0
@ -139,6 +151,8 @@ def main():
print(" - Number of replace: " + str(nb_replace)) print(" - Number of replace: " + str(nb_replace))
if len(output_filenames) > 1: if len(output_filenames) > 1:
file_index += 1 file_index += 1
elif use_output_clipboard:
pyperclip.copy(replace_string)
else: else:
print(replace_string) print(replace_string)
if args["print_nb_match"]: if args["print_nb_match"]:
@ -161,6 +175,8 @@ def main():
print("Error: file not found: " + str(exception)) print("Error: file not found: " + str(exception))
return -1 return -1
output_file.write(replace_string) output_file.write(replace_string)
elif use_output_clipboard:
pyperclip.copy(replace_string)
else: else:
print(replace_string) print(replace_string)
if args["print_nb_match"]: if args["print_nb_match"]: