#๐Ÿ”’ how do I parse inputted text from curses with gather?

53 messages ยท Page 1 of 1 (latest)

next lichen
magic crescentBOT
#

@next lichen

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

next lichen
#

@lapis igloo

lapis igloo
#

!code
Please paste the code here directly.

magic crescentBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

#

Hey @next lichen!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
next lichen
#


        win = curses.newwin(19, 12, 20, 53)
        box = Textbox(win)

        rectangle(stdscr, 19, 52, 21, 75)

        stdscr.refresh()
        box.edit()
        text = box.gather()
        stdscr.addstr(10, 40, text)
        stdscr.getch()
        message = box.gather()


lapis igloo
#

I don't see any imports etc. Is that complete?

next lichen
#

it's sample code

lapis igloo
#

You need to post working code, whatever its shortcomings.

next lichen
#
import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None``` assuming this is the imports
lapis igloo
#

So put it all together in a .py file and run it. Show the complete .py file, and a transcript of the run.

next lichen
next lichen
lapis igloo
#

If I run the code you just posted:

import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None




        win = curses.newwin(19, 12, 20, 53)
        box = Textbox(win)

        rectangle(stdscr, 19, 52, 21, 75)

        stdscr.refresh()
        box.edit()
        text = box.gather()
        stdscr.addstr(10, 40, text)
        stdscr.getch()
        message = box.gather()


I get this:

[~/tmp/python-discord]fleet2*> py3 tofu.py
+ exec /Users/cameron/var/venv/3/bin/python tofu.py
  File "/Users/cameron/tmp/python-discord/tofu.py", line 12
    win = curses.newwin(19, 12, 20, 53)
IndentationError: unexpected indent
#

That looks nothing like your screenshot.

bronze vault
#

it doesn't seem like you do, no disrespect meant

lapis igloo
bronze vault
#

here you go

#

he provided the full code

#
import curses
import textwrap
import os
from curses import wrapper
from curses.textpad import Textbox,rectangle
from pathlib import Path
lim=None
def add_str(stdscr,string,y):
        stdscr.addstr(y,(lim[1]-lim[1]//4),string)
        stdscr.refresh()
def add_win(stdscr):
        lim = stdscr.getmaxyx()
        win = curses.newwin(lim[0] - 10, lim[1] - 10, 5, 5)
        win.refresh()
        stdscr.refresh()
def main(stdscr):
        global lim
        lim = stdscr.getmaxyx()
        add_win(stdscr)
        content=[*Path('').glob('*')] #lits the contents of ./test/*
        curses.init_pair(1,curses.COLOR_BLUE,curses.COLOR_YELLOW)
        BLUE_AND_YELLOW=curses.color_pair(1)
        for i,item in enumerate(content):
                add_str(stdscr,str(item),i+1)


        win = curses.newwin(19, 12, 20, 53)
        box = Textbox(win)

        rectangle(stdscr, 19, 52, 21, 75)

        stdscr.refresh()
        box.edit()
        text = box.gather()
        stdscr.addstr(10, 40, text)
        stdscr.getch()
        message = box.gather()


        print(message)


wrapper(main)
lapis igloo
# bronze vault here you go

Maybe you should look at what they just posted to the pastebin. It does produce the screenshot they showed.

bronze vault
lapis igloo
next lichen
#

for reading/parsing the text box?

lapis igloo
#

Well it looks to me like this line:

message = box.gather()

returns immediately. Possibly it just fetches the current context of the text box? print out message, see what's in it.

Maybe you need to call box.edit() again if you expect to enter more text in that text box.

next lichen
#

just one input in this text box

lapis igloo
#

Ok, so you've got the contets in text. What did you want to do with it then?

next lichen
#

parsing it, if I say I want to move to /usr/share from /var/www from input with chdir(path): os.chdir('input') content=[*Path('').glob('*')] #lits the contents of ./test/*

lapis igloo
#

Parsing means examining the text and extracting information from it.

What did you intend to enter into the textbox?

It is meant to be the path in your suggestion?

next lichen
#

example the input would be /var/www

#

or /usr/share

#

or a dir

lapis igloo
#

Ok. So why not:

path = box.gather()
os.chdir(path)
next lichen
#

FileNotFoundError: [Errno 2] No such file or directory: '/usr/share/ \n'

lapis igloo
#

It looks like your string includes a trailing space and a newline character. Did you type those?

#

You could go path = box.gather().strip() to trim those.

next lichen
next lichen
lapis igloo
#

The content is not magic. You need to reload it and redraw it.

#

Just as you originally drew it. (I assume you mean the listdir listing on the right?)

next lichen
#

stdscr.refresh()

lapis igloo
#

That just redraws the terminal display. in case is has been mangled.

#

It doesn't for example reread the directory list and show a new one.

lapis igloo
# next lichen stdscr.refresh()

You should probably run a loop in the main() function. Initially set up the screen. Then a while true loop which:

  • update the directory listing on the right
  • prompts for a new directory
  • chdir the new directory
    then next time round the loop the directory listing just gets redrawn
next lichen
#

!close

magic crescentBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.