how to get string from stdin using Purescript? - purescript

as title says, I want to know about how to get stdin using Purescript.
I want to input string using my keyboard. that's all.
in fact, I can find some code just googling this. but nothing runs corretly. they omit import statement or complie error occurs.
It could be good if I got full code about stdin. thank you

module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Console (log)
import Node.ReadLine (prompt, close, setLineHandler, setPrompt, noCompletion, createConsoleInterface)
main :: Effect Unit
main = do
interface <- createConsoleInterface noCompletion
setPrompt "> " interface
prompt interface
interface # setLineHandler \s ->
if s == "quit"
then close interface
else do
log $ "You typed: " <> s
prompt interface
Source: https://github.com/purescript-node/purescript-node-readline/blob/v6.0.0/test/Main.purs
Documentation: https://pursuit.purescript.org/packages/purescript-node-readline

Here is a package for reading from stdin, but it might not work the way you expect with keyboard input.
https://pursuit.purescript.org/packages/purescript-node-streams-aff

Related

How to go through Effect (Array String) in purescript

I'm learning purescript and trying to log a directory content.
module Main where
import Prelude
import Data.Traversable (traverse)
import Effect (Effect)
import Effect.Console (log)
import Node.FS.Sync (readdir)
fnames = readdir "."
main = do
travere (\a -> log $ show a) fnames
I want to get folder entries printed in console output.
I can not get rid of (or pass through) the Effect which I get from Node.FS.Sync (readdir) (I get Effect (Array String)). And traverse or log or show can not work with Effect in front of Array String.
I get No type class instance was found for Data.Traversable.Traversable Effect.
Effect is a program, not a value. Effect (Array String) is a program that, when executed, will produce an Array String. You cannot get the Array String out of that program without executing it.
One way to execute this program is to make it part of a larger program, such as, for example, your main program. Like this:
main = do
ns <- fnames
traverse (\a -> log $ show a) ns
Of course, there is really no need to put it in a global variable fnames before making it part of the main program. You can include readdir "." directly:
main = do
ns <- readdir "."
traverse (\a -> log $ show a) ns

python subprocess pipe comparing string - removing rubbish

I have a c program that reads a rfid tag. I am trying to get output from that c program (it uses sudo and args) and compare it to a string
Here is my code as well as some debugging information.
import subprocess
#args - the c program and its args
args = ["sudo", "./rc522", "-r", "-b", "1"]
process = subprocess.Popen(args,stdout=subprocess.PIPE,stderr=None)
rfidRead=process.communicate()[0]
rfidRead=rfidRead.decode('utf-8')
print (len(rfidRead))
rfidRead = rfidRead[14:440]
print (len(rfidRead))
print (rfidRead)
if "49.4f.09.0a.0c.0f.00.00.00.00.00.00.00.00.00.00" in rfidRead:
print("unlocked")
else:
print("Not unlocked")
here is the output...
446
426
49.4f.09.0a.0c.0f.00.00.00.00.00.00.00.00.00.00
Not unlocked
I just cant find the hidden characters and things from the pipe output
Can you help please?
This is the rfidRead output using repr:
'\x1b[1;93m4\x1b[00m\x1b[1;93m9\x1b[00m.\x1b[1;93m4\x1b[00m\x1b[1;93mf\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m9\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93ma\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93mc\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93mf\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0\x1b[00m.\x1b[1;93m0\x1b[00m\x1b[1;93m0'
I worked out the answer with the help of jasonharper.
After doing the repr(...) I decided to google \x1b and found a stackoverflow article about vt100 and that this is vt100 code, so I did another google search to decode vt100, and I got a regular expression to remove the escape codes... here is the article I found. How can I remove the ANSI escape sequences from a string in python

Is there any equivalent command for !findstack to filter the managed code?

I found the !findstack just can be used to filter unmanaged code, however it failed to filter managed code, so is there any equivalent command for !findstack to filter the managed code ?
I don't know of a ready-to-use function like this, so I see two options here:
fiddle around with some WinDbg internal commands (which you can hardly understand half a year later)
use PyKd and write a nice script in Python
Approach a)
This may look similar to this:
~*e .foreach(word {!clrstack}) {.if ($spat("${word}", "?*RunMessageLoop?*") == 1) {.printf "Found!\n"}}
Approach b)
Put the following into a file called clrfindstack.py
from pykd import *
import sys
if len(sys.argv) == 1: # script name only
print "Please provide a search string as argument"
exit()
threads = getNumberThreads()
for thread in range(0, threads):
dbgCommand("~"+str(thread)+"s") # select the thread
stack = dbgCommand("!clrstack") # run !clrstack
if sys.argv[1] in stack: # [0] is the script name
print "Found", sys.argv[1], "in thread", thread, "(Use ~"+str(thread)+"s to select it)"
And then run it
0:000> !py c:\tmp\clrfindstack.py
Please provide a search string as argument
0:000> !py c:\tmp\clrfindstack.py RunMessageLoop
Found RunMessageLoop in thread 0 (Use ~0s to select it)
The implementation is probably not very pythonic, but does its job.

inline iPython call to system

I really like to use system shell commands in iPython. But I was wondering if it is possible to loop over the returned values from a call to e.g. !ls. This works:
files = !ls ./*_subcell_cooc.txt
for f in files:
print f
But this does not:
for f in ( !ls ./*_subcell_cooc.txt):
print f
Error is:
File "<ipython-input-1-df2bc72907d7>", line 5
for f in ( !ls $ROOT/*_subcell_cooc.txt):
^
SyntaxError: invalid syntax
No it is not possible, the syntax var = !something is special cased in IPython. It is not valid python syntax, and we will not extend for loops and so on to work with it.
You can do assignment as you show in your first example, but using glob,os and other real python module to do that will be more robust, not much harder, and also work outside of IPython...
For the anecdote Guido was really not happy with IPython half-shell syntax when he saw it last time at SciPy2013.
(Also it uppercase I in IPython please.)

Call custom vim completetion menu with Information from Perl-Script

I wrote a script analyzing perl-files (totally without PPI, because it will be used on Servers where the admins don't want PPI to be installed and so on and so forth, but let's not talk about that).
Now, let's say I have this code:
my $object = MySQL->new();
my $ob2 = $object;
$ob2->
(Where MySQL is one of our modules).
My script correctly identifies that $ob2 is a MySQL-Object and sees where it came from, and then returns a list of found subs in that module.
My idea was, that, since I use vim for editing, this could be a really cool way for "CTRL-n"-Completetion.
So, when...
$ob2->[CTRL-n]
It shows the CTRL-n-Box which opens my Perl-Script and gives it a few parameters (I would need: The line that I am actually on, the cursor position and the whole file as it is in vim).
I already found things like vim-perl, which allows me to write something like
if has('perl')
function DefPerl()
perl << EOF
use MyModule;
return call_to_my_function(); # returns all the methods from the object for example
EOF
endfunction
call DefPerl()
endif
But somehow this does not get executed (I tried writing something to a file with a system call just for the sake of testing)...
So, in short:
Does anyone here know how to achieve that? Calling a perl-function from vim by pressing CTRL-n with the full file-code and the line vim is actually in and the position, and then opening a completetion-menu with the results it got from the perl-script?
I hope someone knows what I mean here. Any help would be appreciated.
The details and tips for invoking embedded Perl code from Vim can be found in this Vim Tips Wiki article. Your attempts are already pretty close, but to return stuff from Perl, you need to use Vim's Perl API:
VIM::DoCommand "let retVal=". aMeaningfullThingToReturn
For the completion menu, your Perl code needs to return a List of Vim objects that adhere to the format as described by :help complete-items. And :help complete-functions shows how to trigger the completion. Basically, you define an insert-mode mapping that sets 'completefunc' and then trigger your function via <C-x><C-u>. Here's a skeleton to get your started:
function! ExampleComplete( findstart, base )
if a:findstart
" Locate the start of the keyword.
let l:startCol = searchpos('\k*\%#', 'bn', line('.'))[1]
if l:startCol == 0
let l:startCol = col('.')
endif
return l:startCol - 1 " Return byte index, not column.
else
" Find matches starting with a:base.
let l:matches = [{'word': 'example1'}, {'word': 'example2'}]
" TODO: Invoke your Perl function here, input: a:base, output: l:matches
return l:matches
endif
endfunction
function! ExampleCompleteExpr()
set completefunc=ExampleComplete
return "\<C-x>\<C-u>"
endfunction
inoremap <script> <expr> <Plug>(ExampleComplete) ExampleCompleteExpr()
if ! hasmapto('<Plug>(ExampleComplete)', 'i')
imap <C-x><C-z> <Plug>(ExampleComplete)
endif