Skip to content

Commit

Permalink
Replace newline characters with semicolons
Browse files Browse the repository at this point in the history
'PowerShell' can take semicolon separated commands as input, so we can
use them to improve readability.
  • Loading branch information
thanhph111 committed Oct 1, 2021
1 parent 3809c22 commit de5031f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions imagepaste/clipboard/windows/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def push(cls, save_directory: str) -> WindowsClipboard:
filepath = join(save_directory, filename)

image_script = (
"$image = Get-Clipboard -Format Image\n"
"$image = Get-Clipboard -Format Image; "
f"if ($image) {{ $image.Save('{filepath}'); Write-Output 0 }}"
)
process = Process.execute(cls.get_powershell_args(image_script), split=False)
Expand All @@ -48,7 +48,7 @@ def push(cls, save_directory: str) -> WindowsClipboard:
return cls(Report(6, f"Saved and pasted 1 image: {image}"), [image])

file_script = (
"$files = Get-Clipboard -Format FileDropList\n"
"$files = Get-Clipboard -Format FileDropList; "
"if ($files) { $files.fullname }"
)
process = Process.execute(cls.get_powershell_args(file_script))
Expand All @@ -70,9 +70,9 @@ def pull(cls, image_path: str) -> WindowsClipboard:
information of the pulled image we put its path to the input.
"""
script = (
"Add-Type -Assembly System.Windows.Forms\n"
"Add-Type -Assembly System.Drawing\n"
f'$image = [Drawing.Image]::FromFile("{image_path}")\n'
"Add-Type -Assembly System.Windows.Forms; "
"Add-Type -Assembly System.Drawing; "
f"$image = [Drawing.Image]::FromFile('{image_path}'); "
"[Windows.Forms.Clipboard]::SetImage($image)"
)
process = Process.execute(cls.get_powershell_args(script))
Expand Down Expand Up @@ -112,8 +112,9 @@ def get_powershell_args(script: str) -> list[str]:
"$OutputEncoding = "
"[System.Console]::OutputEncoding = "
"[System.Console]::InputEncoding = "
"[System.Text.Encoding]::UTF8\n"
"$PSDefaultParameterValues['*:Encoding'] = 'utf8'\n" + script
"[System.Text.Encoding]::UTF8; "
+ "$PSDefaultParameterValues['*:Encoding'] = 'utf8'; "
+ script
)
args = powershell_args + ["& { " + script + " }"]
return args

0 comments on commit de5031f

Please sign in to comment.