mirror of
https://github.com/zhigang1992/mitmproxy.git
synced 2026-04-30 13:11:47 +08:00
Merge pull request #2293 from cortesi/clip
commands: cut.clip copies cuts to system clipboard
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import io
|
||||||
import csv
|
import csv
|
||||||
import typing
|
import typing
|
||||||
from mitmproxy import command
|
from mitmproxy import command
|
||||||
@@ -7,6 +8,8 @@ from mitmproxy import ctx
|
|||||||
from mitmproxy import certs
|
from mitmproxy import certs
|
||||||
from mitmproxy.utils import strutils
|
from mitmproxy.utils import strutils
|
||||||
|
|
||||||
|
import pyperclip
|
||||||
|
|
||||||
|
|
||||||
def headername(spec: str):
|
def headername(spec: str):
|
||||||
if not (spec.startswith("header[") and spec.endswith("]")):
|
if not (spec.startswith("header[") and spec.endswith("]")):
|
||||||
@@ -124,3 +127,25 @@ class Cut:
|
|||||||
[strutils.always_str(c) or "" for c in r] # type: ignore
|
[strutils.always_str(c) or "" for c in r] # type: ignore
|
||||||
)
|
)
|
||||||
ctx.log.alert("Saved %s cuts as CSV." % len(cuts))
|
ctx.log.alert("Saved %s cuts as CSV." % len(cuts))
|
||||||
|
|
||||||
|
@command.command("cut.clip")
|
||||||
|
def clip(self, cuts: command.Cuts) -> None:
|
||||||
|
"""
|
||||||
|
Send cuts to the system clipboard.
|
||||||
|
"""
|
||||||
|
fp = io.StringIO(newline="")
|
||||||
|
if len(cuts) == 1 and len(cuts[0]) == 1:
|
||||||
|
v = cuts[0][0]
|
||||||
|
if isinstance(v, bytes):
|
||||||
|
fp.write(strutils.always_str(v))
|
||||||
|
else:
|
||||||
|
fp.write("utf8")
|
||||||
|
ctx.log.alert("Clipped single cut.")
|
||||||
|
else:
|
||||||
|
writer = csv.writer(fp)
|
||||||
|
for r in cuts:
|
||||||
|
writer.writerow(
|
||||||
|
[strutils.always_str(c) or "" for c in r] # type: ignore
|
||||||
|
)
|
||||||
|
ctx.log.alert("Clipped %s cuts as CSV." % len(cuts))
|
||||||
|
pyperclip.copy(fp.getvalue())
|
||||||
|
|||||||
@@ -152,6 +152,7 @@ def default_keymap(km):
|
|||||||
km.add("A", "flow.resume @all", context="flowlist")
|
km.add("A", "flow.resume @all", context="flowlist")
|
||||||
km.add("a", "flow.resume @focus", context="flowlist")
|
km.add("a", "flow.resume @focus", context="flowlist")
|
||||||
km.add("b", "console.command 'cut.save s.content|@focus '", context="flowlist")
|
km.add("b", "console.command 'cut.save s.content|@focus '", context="flowlist")
|
||||||
|
km.add("C", "console.command 'cut.clip '", context="flowlist")
|
||||||
km.add("d", "view.remove @focus", context="flowlist")
|
km.add("d", "view.remove @focus", context="flowlist")
|
||||||
km.add("D", "view.duplicate @focus", context="flowlist")
|
km.add("D", "view.duplicate @focus", context="flowlist")
|
||||||
km.add("e", "set console_eventlog=toggle", context="flowlist")
|
km.add("e", "set console_eventlog=toggle", context="flowlist")
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from mitmproxy.test import taddons
|
|||||||
from mitmproxy.test import tflow
|
from mitmproxy.test import tflow
|
||||||
from mitmproxy.test import tutils
|
from mitmproxy.test import tutils
|
||||||
import pytest
|
import pytest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
|
||||||
def test_extract():
|
def test_extract():
|
||||||
@@ -101,6 +102,26 @@ def qr(f):
|
|||||||
return fp.read()
|
return fp.read()
|
||||||
|
|
||||||
|
|
||||||
|
def test_cut_clip():
|
||||||
|
v = view.View()
|
||||||
|
c = cut.Cut()
|
||||||
|
with taddons.context() as tctx:
|
||||||
|
tctx.master.addons.add(v, c)
|
||||||
|
v.add([tflow.tflow(resp=True)])
|
||||||
|
|
||||||
|
with mock.patch('pyperclip.copy') as pc:
|
||||||
|
tctx.command(c.clip, "q.method|@all")
|
||||||
|
assert pc.called
|
||||||
|
|
||||||
|
with mock.patch('pyperclip.copy') as pc:
|
||||||
|
tctx.command(c.clip, "q.content|@all")
|
||||||
|
assert pc.called
|
||||||
|
|
||||||
|
with mock.patch('pyperclip.copy') as pc:
|
||||||
|
tctx.command(c.clip, "q.method,q.content|@all")
|
||||||
|
assert pc.called
|
||||||
|
|
||||||
|
|
||||||
def test_cut_file(tmpdir):
|
def test_cut_file(tmpdir):
|
||||||
f = str(tmpdir.join("path"))
|
f = str(tmpdir.join("path"))
|
||||||
v = view.View()
|
v = view.View()
|
||||||
|
|||||||
Reference in New Issue
Block a user