Skip to content

Commit

Permalink
pretty print/truncate eval results
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbicodes committed Jun 28, 2023
1 parent ffaf575 commit 205f7a3
Show file tree
Hide file tree
Showing 10 changed files with 1,643 additions and 4,753 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Alpha. Certainly contains bugs. There is a [live demo](https://bobbicodes.github
- ✅ Create test suite
- ✅ Test published package
- ✅ Handle errors
- [ ] Truncate very long eval results
- ✅ Pretty-print eval result
- ✅ Truncate very long eval result
- [ ] Handle infinite loops

## Run demo locally
Expand Down
1,030 changes: 0 additions & 1,030 deletions dist/assets/index-1ed6b52a.js

This file was deleted.

1,051 changes: 1,051 additions & 0 deletions dist/assets/index-53d4dbe5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/lang-clojure-eval/assets/vite-4a748afd.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<script type="module" crossorigin src="/lang-clojure-eval/assets/index-1ed6b52a.js"></script>
<script type="module" crossorigin src="/lang-clojure-eval/assets/index-53d4dbe5.js"></script>
<link rel="stylesheet" href="/lang-clojure-eval/assets/index-b488241e.css">
</head>
<body>
Expand Down
1,671 changes: 0 additions & 1,671 deletions dist/js/sci.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lang-clojure-eval",
"version": "0.0.12",
"version": "0.0.13",
"author": "Bobbi Towers <[email protected]>",
"description": "Lezer-based Clojure Codemirror 6 extension with live evaluation",
"main": "dist/index.cjs",
Expand Down
1,671 changes: 0 additions & 1,671 deletions public/js/sci.js

This file was deleted.

13 changes: 10 additions & 3 deletions src/eval-region.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ function updateEditor(view, text, pos) {
})
}

function splitResult(s) {
if (s.length > 100) {
return "\n" + s.substring(0, 100) + "..." + s.substring(s.length -2)
}
return s
}

function tryEval(ctx, s) {
try {
return evalString(ctx, s)
Expand All @@ -146,7 +153,7 @@ function evalAtCursor(view) {
posBeforeEval = view.state.selection.main.head
const codeBeforeCursor = codeBeforeEval.slice(0, posBeforeEval)
const codeAfterCursor = codeBeforeEval.slice(posBeforeEval, codeBeforeEval.length)
evalResult = tryEval(ctx, cursorNodeString(view.state))
evalResult = splitResult(tryEval(ctx, cursorNodeString(view.state)))
const codeWithResult = codeBeforeCursor + " => " + evalResult + " " + codeAfterCursor
updateEditor(view, codeWithResult, posBeforeEval)
view.dispatch({selection: {anchor: posBeforeEval, head: posBeforeEval}})
Expand All @@ -167,15 +174,15 @@ function evalTopLevel(view) {
codeBeforeEval = doc
const codeBeforeFormEnd = codeBeforeEval.slice(0, posAtFormEnd)
const codeAfterFormEnd = codeBeforeEval.slice(posAtFormEnd, codeBeforeEval.length)
evalResult = tryEval(ctx, topLevelString(view.state))
evalResult = splitResult(tryEval(ctx, topLevelString(view.state)))
const codeWithResult = codeBeforeFormEnd + " => " + evalResult + " " + codeAfterFormEnd
updateEditor(view, codeWithResult, posBeforeEval)
return true
}

function evalCell(view) {
const doc = view.state.doc.toString()
evalResult = tryEval(ctx, view.state.doc.text.join(" "))
evalResult = splitResult(tryEval(ctx, view.state.doc.text.join(" ")))
const codeWithResult = doc + "\n" + " => " + evalResult
updateEditor(view, codeWithResult, posBeforeEval)
//console.log("evalCell>", evalString(ctx, view.state.doc.text.join(" ")))
Expand Down
7 changes: 5 additions & 2 deletions src/lang_clojure_eval/main.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns lang-clojure-eval.main
(:require [sci.core :as sci]))
(:require [sci.core :as sci]
[clojure.pprint :as pprint]))

(defn sci-init []
(sci/init {}))

(defn eval-string [ctx s]
(str (sci/eval-string* ctx s)))
(with-out-str (pprint/pprint (sci/eval-string* ctx s))))


946 changes: 573 additions & 373 deletions src/sci.js

Large diffs are not rendered by default.

0 comments on commit 205f7a3

Please sign in to comment.