From 3b7c62a1161c4f52d66e15bf41a0323f5b273051 Mon Sep 17 00:00:00 2001 From: Andrei Regiani Date: Fri, 11 May 2018 04:36:59 +0200 Subject: [PATCH] Shortcut to print values of discarded symbols: ```>>> x```, type any expression and see its return value --- src/inim.nim | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/inim.nim b/src/inim.nim index 0943888..5325081 100644 --- a/src/inim.nim +++ b/src/inim.nim @@ -1,4 +1,5 @@ # MIT License +# Copyright (c) 2018 Andrei Regiani import os, osproc, rdstdin, strutils, terminal, times const @@ -57,14 +58,47 @@ proc getFileData(path: string): string = result = nil proc showError(output: string) = - ## Print only error message, without file and line number + ## 'Discarded' errors will be handled to print its value and type. + ## Other errors print only relevant message without file and line number info. ## e.g. "inim_1520787258.nim(2, 6) Error: undeclared identifier: 'foo'" - ## echo "Error: undeclared identifier: 'foo'" - stdout.setForegroundColor(fgRed, true) let pos = output.find(")") + 2 - echo output[pos..^1].strip - stdout.resetAttributes() - stdout.flushFile() + # "Error: expression 'foo' is of type 'int' and has to be discarded" + var message = output[pos..^1].strip + + # Discarded error: shortcut to print values: >>> foo + if message.endsWith("discarded"): + # Remove text until we can split by single-quote: foo'int + message = message.replace("Error: expression '") + message = message.replace(" is of type '") + message = message.replace("' and has to be discarded") + let message_seq = message.split("'") + let symbol_identifier = message_seq[0] # foo + let symbol_type = message_seq[1] # int + let shortcut = "echo " & symbol_identifier & ", \" : " & symbol_type & "\"" + + buffer.writeLine(shortcut) + buffer.flushFile() + + let (output, status) = execCmdEx(compileCmd) + if status == 0: + let lines = output.splitLines() + stdout.setForegroundColor(fgCyan, true) + echo lines[^2] # ^1 is empty line, ^2 is last stdout + stdout.resetAttributes() + stdout.flushFile() + currentOutputLine = len(lines)-1 + else: + stdout.setForegroundColor(fgRed, true) + echo output.splitLines()[0] + stdout.resetAttributes() + stdout.flushFile() + + # Display all other errors + else: + stdout.setForegroundColor(fgRed, true) + echo message + stdout.resetAttributes() + stdout.flushFile() proc init(preload: string = nil) = setControlCHook(cleanExit) @@ -161,13 +195,13 @@ proc runForever() = # Compilation error else: - indentLevel = 0 - showError(output) # Write back valid code to buffer buffer.close() buffer = open(bufferSource, fmWrite) buffer.write(validCode) buffer.flushFile() + showError(output) + indentLevel = 0 # Clean up tempIndentCode = ""