Skip to content

Commit

Permalink
feat: add a k repl formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
x86y committed Oct 18, 2023
1 parent 5234794 commit 6dd7b06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Beacon/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func e(input: String) -> String {
func ke(input: String) -> String {
var input = input.replacingOccurrences(of: "\\", with: #"\\"#)
input = input.replacingOccurrences(of: "\"", with: #"\""#)
input = ".[{`0:`k@.\"\(input)\"};[];{`0:(,\"Error: \"),(-2_\"\n\"\\x)}]"
input = ".[{line `k@.\"\(input)\"};[];{`0:(,\"Error: \"),(-2_\"\n\"\\x)}]"
return runCmd(kCmd, input)
}

Expand Down
25 changes: 25 additions & 0 deletions Beacon/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,30 @@ struct ContentView: View {
viewModel.load(Buffers.get())
kinit()
repl_init()
// below is adapted from https://codeberg.org/ngn/k/src/branch/master/repl.k
let k_formatter = """
(opn;sem;cls):"(;)"
lines:cols:80
upd:(lines;cols)
lim:{(x<#y)(x#)/y}
dd:{(x<#y)(,[;".."](x-2)#)/y}
fmt:{upd[];$[(::)~x;"";(1<#x)&|/`m`M`A=@x;mMA x;(dd[cols]`k@lim[cols]x),"\n"]}
fmtx:{$[(::)~x;"";`k[x],"\n"]}
mMA:{(P;f;q):((,"!/+(";dct;,")");(("+![";" +(");tbl;")]");(,,"(";lst;,")"))`m`M`A?t:@x
w:cols-#*P;u:w-#q;h:lines-2
x:$[h<(`M=t)+#x;,[;,".."](h-1)#f[w;u;h#x];f[w;u;x]]
,[;"\n"]@"\n"/@[;-1+#x;,;q]P[!#x],'x}
lst:{[w;u;x](((-1+#x)#w),u)dd'`k'lim[cols]'x}
dct:{[w;u;x]k:(|/#'k)$k:`k'!x;par'(((-1+#x)#w-3),u-3)dd'sem/'+(k;`k'.x)}
tbl:{[w;u;x]h:`k'!x;d:`k''.x;W:(#'h)|/'#''d
r:,$[`S~@!x;dd[w](""opn),(""sem)/;par@dd[w-2]@sem/]W$'h
r,par'dd[w-2]'sem/'+@[W;&~^`i`d?_@'.x;-:]$'}
cell:{$[|/`i`d=@y;-x;x]$z}
par:{opn,x,cls}
line0:{c:{0x07~*-2#*x}{(l;r):x;(1:1;r,,(-2_l))}/(x;());"\n"/(*|c),,*c}
line1:{$[#x;;:0];.[`1:(fmt;fmtx)[" "~*x]@.:;,x;{`0:`err[]}]}
line:line1@line0@
"""
let _ = runCmd(kCmd, k_formatter)
}
}

0 comments on commit 6dd7b06

Please sign in to comment.