From 6dd7b064a3c1e8d51a3d31d77add52ef8f8b7a31 Mon Sep 17 00:00:00 2001 From: x86y Date: Wed, 18 Oct 2023 20:47:35 +0400 Subject: [PATCH] feat: add a k repl formatter --- Beacon/Utilities/Utils.swift | 2 +- Beacon/Views/ContentView.swift | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Beacon/Utilities/Utils.swift b/Beacon/Utilities/Utils.swift index 738ca34..d0e0542 100644 --- a/Beacon/Utilities/Utils.swift +++ b/Beacon/Utilities/Utils.swift @@ -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) } diff --git a/Beacon/Views/ContentView.swift b/Beacon/Views/ContentView.swift index d8a33d8..30db32a 100644 --- a/Beacon/Views/ContentView.swift +++ b/Beacon/Views/ContentView.swift @@ -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) } }