Skip to content

Instantly share code, notes, and snippets.

@pfitzseb
pfitzseb / lint_me.jl
Last active April 25, 2023 09:24
Lint a Julia project
using LanguageServer, StaticLint, SymbolServer
path = abspath(ARGS[1])
root_file = if length(ARGS) > 1
abspath(ARGS[2])
else
joinpath(path, "src", string(basename(path), ".jl"))
end
s = LanguageServerInstance(Pipe(), stdout, path)
using REPL
using REPL.LineEdit
# basically the same as Base's `display_error`, just with different frames removed
function display_error(io, err, st)
ind = findfirst(frame -> frame.file == Symbol(@__FILE__) && frame.func == :repleval, st)
st = st[1:(ind == nothing ? end : ind - 2)]
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
showerror(IOContext(io, :limit => true), err, st)
println(io)
end
@pfitzseb
pfitzseb / id.jl
Last active August 24, 2021 13:54
Julia Identifier Regex
Regex("""(?:[
[:alpha:]_\\p{Lu}\\p{Ll}\\p{Lt}\\p{Lm}\\p{Lo}\\p{Nl}\\p{Sc}\u2140-\u2144\u223f\u22be
\u22bf\u22a4\u22a5\u2202\u2205-\u2207\u220e\u220f\u2210\u2211\u221e\u221f\u222b-\u2233
\u22c0-\u22c3\u25f8-\u25ff\u266f\u27d8\u27d9\u27c0\u27c1\u29b0-\u29b4\u2a00-\u2a06
\u2a09-\u2a16\u2a1b\u2a1c\U1d6c1\U1d6db\U1d6fb\U1d715\U1d735\U1d74f\U1d76f\U1d789\U1d7a9
\U1d7c3\u2071-\u207e\u2081-\u208e\u2220-\u2222\u299b-\u29af\u2118\u212e\u309b-\u309c
\U1d7ce-\U1d7e1
]|[^\\P{So}\u2190-\u21FF])
(?:[
[:word:]_!\\p{Lu}\\p{Ll}\\p{Lt}\\p{Lm}\\p{Lo}\\p{Nl}\\p{Sc}\u2140-\u2144\u223f\u22be
@pfitzseb
pfitzseb / tmuxedit.jl
Last active February 22, 2021 15:38
edit in existing tmux pane that runs `vim`
# put this in your startup.jl
using InteractiveUtils
let
tmuxsession = nothing
sessions = split(String(read(`tmux list-panes -a -F '#{pane_tty} #{session_name}'`)), '\n')
io = IOBuffer()
run(pipeline(`tty`, stdout=io))
tty = chomp(String(take!(io)))
for session in sessions
if occursin(tty, session)
@pfitzseb
pfitzseb / encode_uri_component.jl
Last active June 24, 2020 11:53
encodeURIcomponent
using Printf
UNESCAPED = Set(codeunits("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.!~*'()"))
# https://tc39.es/ecma262/#sec-encodeuricomponent-uricomponent
function encode_uri_component(uri)
isvalid(uri) || throw(ArgumentError("`encode_uri_component` can only handle valid UTF8 strings."))
io = IOBuffer()
for cp in codeunits(uri)
@pfitzseb
pfitzseb / gist:37adca96a13c7097631c15e1af4b7409
Last active May 13, 2020 16:02
open nvim when pressing enter on empty prompt
using REPL
using REPL: LineEdit
atreplinit() do repl
repl.interface = REPL.setup_interface(repl)
repl.interface.modes[1].on_enter = function (s)
mktemp() do path, io
input = chomp(String(take!(copy(LineEdit.buffer(s)))))
if isempty(input)
run(`nvim $(path)`)
sleep(0.1)
@pfitzseb
pfitzseb / TraceCalls.jl
Created December 17, 2018 15:10
TraceCalls.jl with Cassette
module TraceCalls
using Cassette
mutable struct Trace
level::Int
cutoff::Int
end
Cassette.@context TraceCtx
~
λ mv ~/.julia/packages .julia/packages_old
~
λ mkdir jenvtest
~
λ cd jenvtest
~/jenvtest
julia> @enter wrapper()
In wrapper() at /home/pfitzseb/.julia/dev/Atom/test.jl:2
1 function wrapper()
>2 mktempdir() do dir
3 open(joinpath(dir, "test.txt"), "w") do io
4 println(io, "data")
5 end
6 end
7 end
@pfitzseb
pfitzseb / timeperiod.jl
Created November 27, 2019 12:03
timeperiod parsing
using Dates
period(::Dates.DatePart{T}, val) where T = Dates.CONVERSION_SPECIFIERS[T](val)
period(::Dates.Delim, val) = Millisecond(0)
Base.parse(t::Type{TimePeriod}, str::AbstractString, df::AbstractString) = parse(t, str, DateFormat(df))
function Base.parse(t::Type{TimePeriod}, str::AbstractString, df::DateFormat)
out = Millisecond(0)
len = length(str)
i = 1