Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interactiveutil: clipboard for FreeBSD #23151

Merged
merged 2 commits into from
Dec 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,49 @@ if Sys.isapple()
end
clipboard() = read(`pbpaste`, String)

elseif Sys.islinux()
elseif Sys.islinux() || Sys.KERNEL === :FreeBSD
_clipboardcmd = nothing
const _clipboardcmds = Dict(
:copy => Dict(
:xsel => Sys.islinux() ?
`xsel --nodetach --input --clipboard` : `xsel -c`,
:xclip => `xclip -silent -in -selection clipboard`,
),
:paste => Dict(
:xsel => Sys.islinux() ?
`xsel --nodetach --output --clipboard` : `xsel -p`,
:xclip => `xclip -quiet -out -selection clipboard`,
)
)
function clipboardcmd()
global _clipboardcmd
_clipboardcmd !== nothing && return _clipboardcmd
for cmd in (:xclip, :xsel)
success(pipeline(`which $cmd`, DevNull)) && return _clipboardcmd = cmd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-existing, but this'll give a pretty misleading error message if which isn't installed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or... we can implement a simple version of which in Julia?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I generally do is if I know the program I'm looking for, usually there's a safe no-op flag like -h or --version that you can try executing in a try-catch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-h works on xclip. unfortunately, the bsd version xsel doesn't have any no-op flag.

end
error("no clipboard command found, please install xsel or xclip")
pkgs = @static if Sys.islinux()
"xsel or xclip"
elseif Sys.KERNEL === :FreeBSD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think @static will resolve the elseif yet

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems works... is it expected?

julia> @static if true
         42
       elseif false
         24
       end                                                                                                            
42                                                                                                                    
                                                                                                                      
julia> @static if false
         42
       elseif true
         24
       end                                                                                                            
24                                                                                                                    

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the elseif condition is getting evaluated at run time, I believe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

support of elseif block is added in #24787

"x11/xsel or x11/xclip"
end
error("no clipboard command found, please install $pkgs")
end
function clipboard(x)
c = clipboardcmd()
cmd = c == :xsel ? `xsel --nodetach --input --clipboard` :
c == :xclip ? `xclip -silent -in -selection clipboard` :
cmd = get(_clipboardcmds[:copy], c, nothing)
if cmd === nothing
error("unexpected clipboard command: $c")
end
open(pipeline(cmd, stderr=STDERR), "w") do io
print(io, x)
end
end
function clipboard()
c = clipboardcmd()
cmd = c == :xsel ? `xsel --nodetach --output --clipboard` :
c == :xclip ? `xclip -quiet -out -selection clipboard` :
cmd = get(_clipboardcmds[:paste], c, nothing)
if cmd === nothing
error("unexpected clipboard command: $c")
end
read(pipeline(cmd, stderr=STDERR), String)
end

Expand Down