Skip to content

Commit

Permalink
added automatic peco install on jl>=1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
rapus95 committed Jan 16, 2020
1 parent 8c442ab commit b423019
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.3.3-DEV"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
peco_jll = "b89f85f3-45f8-5ea8-8223-9dfa39faf294"

[compat]
julia = "= 0.7, 1.0"
Expand Down
95 changes: 47 additions & 48 deletions src/InteractiveCodeSearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,31 @@ using Base
using Base: IOError
using InteractiveUtils: edit, gen_call_with_extracted_types, methodswith

function _readandwrite(cmds)
processes = open(cmds, "r+")
return (processes.out, processes.in, processes)
end


abstract type SearchPolicy end
struct Shallow <: SearchPolicy end
struct Recursive <: SearchPolicy end


mutable struct SearchConfig # CONFIG
open
interactive_matcher::Cmd
interactive_matcher::Function
auto_open::Bool
end

function setmatcher!(cmd::Function, obj::SearchConfig)
obj.interactive_matcher = cmd
return
end

function setmatcher!(obj::SearchConfig, cmd::Cmd)
maybe_warn_matcher(cmd)
setmatcher!(obj) do f
maybe_warn_matcher(cmd)
f(cmd)
end
return
end

maybe_identifier(s) = !startswith(string(s), "#")

is_locatable(::Any) = false
Expand Down Expand Up @@ -147,6 +155,12 @@ function find_source_file(file)
return path
end

function _readandwrite(matcher)
proc = matcher() do cmd
open(`$cmd`, "r+")
end
return (proc.out, proc.in, proc)
end

"""
read_stdout(input::AbstractString, cmd)
Expand Down Expand Up @@ -182,7 +196,6 @@ function parse_loc(line)
end

function run_matcher(input)
maybe_warn_matcher()
return String(read_stdout(input, CONFIG.interactive_matcher))
end

Expand Down Expand Up @@ -265,8 +278,7 @@ InteractiveCodeSearch.CONFIG.auto_open = false # open matcher even when there
## Using InteractiveCodeSearch.jl by default
Put the following code in your `~/.julia/config/startup.jl` (≥ Julia 0.7)
or `~/.juliarc.jl` (Julia 0.6):
Put the following code in your `~/.julia/config/startup.jl` (≥ Julia 0.7):
```julia
using InteractiveCodeSearch
Expand Down Expand Up @@ -437,10 +449,23 @@ macro searchmethods(x)
end
end

const preferred_terminal = Cmd[
`peco`,
`percol`,
]
################################################################################
# matcher binaries #
################################################################################

@static if VERSION<v"1.3-"
const preferred_terminal = Cmd[
`peco`,
`percol`,
]
else
using peco_jll
const preferred_terminal = Union{Function,Cmd}[
`peco`,
peco,
`percol`,
]
end

const preferred_gui = Cmd[
`rofi -dmenu -i -p "🔎"`,
Expand All @@ -451,43 +476,18 @@ function need_gui(stdstreams = [stdout, stdin])
return !all(isa.(stdstreams, Ref(Base.TTY)))
end

function choose_preferred_command(commands::Vector{Cmd})
function choose_preferred_command(commands::Vector{<:Union{Function,Cmd}}, default=nothing)
for cmd in commands
if Sys.which(cmd.exec[1]) !== nothing
if !(cmd isa Cmd) || Sys.which(cmd.exec[1]) !== nothing
return cmd
end
end
return nothing
end

function choose_preferred_command(f, commands::Vector{Cmd})
cmd = choose_preferred_command(commands)
if cmd !== nothing
return cmd
else
return f()
end
return default
end

# Julia 0.6
const _preferred_terminal = preferred_terminal
const _preferred_gui = preferred_gui

function choose_interactive_matcher(;
preferred_terminal = _preferred_terminal,
preferred_gui = _preferred_gui,
gui = need_gui())
if gui
return choose_preferred_command(preferred_gui) do
return preferred_gui[1]
end
else
return choose_preferred_command(preferred_terminal) do
return choose_preferred_command(preferred_gui) do
return preferred_terminal[1]
end
end
end
function choose_interactive_matcher()
need_gui() && return choose_preferred_command(preferred_gui, preferred_gui[1])
return choose_preferred_command(vcat(preferred_terminal, preferred_gui), preferred_terminal[1])
end

function matcher_installation_tips(program::AbstractString)
Expand All @@ -509,7 +509,7 @@ function matcher_installation_tips(program::AbstractString)
"""
end

function maybe_warn_matcher(cmd = CONFIG.interactive_matcher)
function maybe_warn_matcher(cmd::Cmd)
if Sys.which(cmd.exec[1]) === nothing
@warn """
Matcher $(cmd.exec[1]) not installed.
Expand All @@ -519,8 +519,7 @@ function maybe_warn_matcher(cmd = CONFIG.interactive_matcher)
end

function __init__()
CONFIG.interactive_matcher = choose_interactive_matcher()
maybe_warn_matcher()
setmatcher!(choose_interactive_matcher())
end

include("taskmanager.jl")
Expand Down

0 comments on commit b423019

Please sign in to comment.