How to take user input form the terminal?
Self-explanatory, the best I could find with research was:
I got this error:
What is your name? pedro
Unhandled exception caught during execution: 'str' object has no attribute 'to_string'
mojo: error: execution exited with a non-zero result: 1
So my questions:
1. How can I do it?
Thank you.
20 Replies
@MyriadColors , the error it telling you that you're trying to do
to_string
on a str
object. So, it's already a string. If you just remove .to_string()
then it works. See attached pics.
Also, check out @toasty 's repo: https://github.com/thatstoasty/prism
It's more for making a CLI tool (a'la Go's famous "Cobra" library) and not so much for holding a terminal-conversation, but I'm sure you could achieve that, too.GitHub
GitHub - thatstoasty/prism: Mojo CLI Library modeled after Cobra.
Mojo CLI Library modeled after Cobra. Contribute to thatstoasty/prism development by creating an account on GitHub.
erk..... didn't mean to ping you, toasty. I meant to make that a silent "no ping" at-tag before posting. Sorry. 😐
Thank you!
Congrats @MyriadColors, you just advanced to level 1!
I just copied and pasted the code as I assumed it was needed.
Though I really do find it weird that mojo doesnt have this feature
as part of the language
You good. The beautiful thing about stumbling around with Mojo: we're all stumbling around with Mojo. It's new; we're all noobs. 😂
Im not compl,aining btw, its just something I figured every language would have
oh, give it time. The Mojo team has a stated goal of being a superset of Python. That means implementing stuff like
input()
, too. But it's brand-spanking-new. Just give 'em time. 🙂Cool, hopefully we get something like std::cout instead of printf
to make it easier 😛
also, sad to see f-strings doesnt work
also, you could always contrib to the std-lib! If they just add some screaming guitar solos in the background, then this new vid would be a good motivational vid. 😄
https://www.youtube.com/watch?v=TJpFSSIts5Q
Modular
YouTube
Contributing to Open-Source Mojo🔥 Standard Library
Mojo🔥 standard library is now open-source. In this video Modular engineer Joe Loser discusses how you can get started with contributing to Mojo🔥 using Mojo🔥 nightly builds.
Chapters:
00:00 Contributing to Mojo🔥 Standard Library
01:59 Demo: Contributing a simple test case
Resources:
Blog post: https://www.modular.com/blog/how-to-contribute-to-...
Given that they're aiming for pythonic syntax, I'd expect it to remain
print()
. And I agree 100% re f-strings. Anytime I'm in any language that doesn't have them, I find that I really miss my f-strings.Trying to mark your answer as the solution.
@MyriadColors
Try this/
It is possible to do it all in Mojo, but it's not simple as it would involve setting your terminal to raw mode via an external_call to C and capturing N bytes from stdin. If you're interested I could link you to an example from my termios repo
Would it work on linux?
I know a bit of C
It should work on linux, but you might need to mess with the structs a bit since I wrote this on a mac. It seems like the
termios
struct differs on linux and mac. Some of the constants might differ as well, so it's hard to guarantee.
https://github.com/thatstoasty/termios/blob/main/examples/get_key.mojo
I reimplemented the python's termios and tty functions by calling the underlying C functions via external_call
in Mojo. https://github.com/thatstoasty/termios/blob/main/termios/c/terminal.mojoTHanks, I will take a look
and if you get it working across platforms, perhaps contrib to std-lib
input()
function 😉 The code doesnt seem to be too complicated, problem is that Im not too familiar with posix
I will se ewhat i can do
maybe you two can tag-team it!