SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2014-01-19 23:13:48

canibanoglu
New Member
Registered: 2014-01-19
Posts: 7

Weird resize -s A B behavior

Hello,

I have just installed #! and I've been trying to get a script of mine to run. It is a simple terminal program written in Python and I call the resize program to change the terminal window to something that I can work with (this is important because the script uses curses). The thing is the call "resize -s 55 200" doesn't resize the window at all.

So, I have tried calling "resize -s 55 200" manually in a new terminal window but it still doesn't do what I expect it to do. It should resize the xterm window but it just gets the current "COLUMNS" and "LINES" environment variables and tries to resize to the same window size. It's as if the -s option and its arguments aren't interpreted at all. I have been trying to solve this issue and I have read a lot of different stuff but I just got to the point where I don't even understand what I'm reading, so I thought I'd ask for help here smile

FWIW, calling "xterm -geometry 200x55 -e "python myscript.py"" works as in spawning a new xterm instance but that's not really what I want to do and even if it were so, the borders are misplaced for some reason.

I tried running the resize program on OS X terminal and it does work as expected and resizes the window to the given size. I have also tried it on iTerm and as long as the "Disable session-initiated window resizing" is unchecked in its preferences, resize also works on iTerm. I've searched if there's a similar setting that I should flip on #! but I couldn't find it in preferences or in the man terminator output.

I believe I'm missing something terribly easy here and I would really appreciate it if someone could help me or point me in the right direction.

Thank you very much in advance!

Offline

Be excellent to each other!

#2 2014-01-19 23:24:03

Sector11
77345 ¡#
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 13,407
Website

Re: Weird resize -s A B behavior

Have you tried it on "terminator" - the default #! terminal?

Can you post you code so others can look at it and maybe come up with a solution.

Hard to do without the code.

Offline

#3 2014-01-19 23:38:34

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 4,440

Re: Weird resize -s A B behavior

How are you using the command?

man resize
....    For resize's output to take effect, resize must either be evaluated as part of  the  command  line  (usually
       done  with  a  shell  alias or function) or else redirected to a file which can then be read in......

Artwork at deviantArt;  Iceweasel Personas;  SLiM #! Themes;  Openbox themes

Offline

#4 2014-01-19 23:45:24

canibanoglu
New Member
Registered: 2014-01-19
Posts: 7

Re: Weird resize -s A B behavior

Oh right, this was done in terminator. I have tried it in its bare form ("resize -s 55 200") and also with "eval `resize -s 55 200`". The thing is resize doesn't export the the values I have supplied, it exports the current values of the window.

I have just tried the following:

canibanoglu@crunchbang:~$ resize -s 55 200 > res
canibanoglu@crunchbang:~$ cat res
COLUMNS=80;
LINES=25;
export COLUMNS LINES;
canibanoglu@crunchbang:~$ source res
# Didn't change the window size

As you can see, resize's output isn't what I requested and even if I save the output to a file, resize the window and then try to source the file, it doesn't change the window size.

Offline

#5 2014-01-19 23:56:01

canibanoglu
New Member
Registered: 2014-01-19
Posts: 7

Re: Weird resize -s A B behavior

The whole code is as follows. The call to subprocess.call is commented out because it kept timing out if I used it in an xterm that I spawn myself.

Another thing I tried is to spawn a new terminator instance with the desired size, passing in the command to execute to that instance and everything works great if I call time.sleep(0.5). The problem with that is that the code really wouldn't be portable.

Anyways, the code:

import curses
import time
import locale
import subprocess

# Resize the terminal window
# May not work on Windows systems
# subprocess.call(['resize', '-s', '55', '200'])

# This part is placeholder only
# In the future the text to be typed will be randomly selected
# from a database
def main(screen):
    message = "There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory which states that this has already happened."
    
    # Split the message into parts
    # Each part must have less than 80 characters and these will be scrolled
    # when the user comes close to the end of the part currently displayed
    
    length = 0
    parts = [[]]
    words = message.split()
    
    for word in words:
        if length + len(word) > 80:
            parts.append([word])
            length = len(word) + 1
        else:
            parts[-1].append(word)
            length += len(word) + 1
    
    substrings = [' '.join(part) for part in parts]
    
    # Initialize curses and set the color pairs
    screen = curses.initscr()
    curses.start_color() # This may not be needed if curses.wrapper is used
    curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
    curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_RED)
    screen.keypad(1)
    screen.border()
    
    dims = screen.getmaxyx() # This may not be needed because we resize the window anyway
    
    q = -1
    y = dims[0] / 2 + 1
    x = (dims[1] - 80) / 2
    startX = (dims[1] - 80) / 2
    partsIndex = 0
    substring = substrings[partsIndex] + ' '
    nextWord = substrings[partsIndex + 1].split(' ', 1)[0]
    screen.addstr(y, x, substring + nextWord)
    y += 1
    screen.move(y, x)
    screen.refresh()
    currentChar = 0
    started = 0
    mistakes = 0
    lastPart = 0
    
    while q != 27:
        # If this is the last substring and current character is the last character
        # of that substring, it means we are done. This is not an ideal solution,
        # because the user will not have the option of correcting the last character.
        if partsIndex + 1 == len(substrings): # This is the last substring
            lastPart = 1
    
        if lastPart and currentChar == len(substring):
            break
        elif currentChar == len(substring):
            # Load the next substring and move the cursor back to startX
            partsIndex += 1
            substring = substrings[partsIndex] + ' '
            if partsIndex + 1 == len(substrings):
                nextWord = ''
                substring = substring[:-1]
            else:
                nextWord = substrings[partsIndex + 1].split(' ', 1)[0]
            screen.addstr(y - 1, startX, ' ' * (dims[1] - startX-1))
            screen.addstr(y, startX, ' ' * (dims[1] - startX-1))
            screen.addstr(y - 1, startX, substring + nextWord)
            screen.move(y, startX)
            x = startX
            currentChar = 0
        q = screen.getch()
    
        # Start the timer if this is the first keystroke, else don't touch the timer
        if not started:
            started = 1
            now = time.time()
        try:
            if q == 127 or q == 263: # Backspace needs its own implementation
                if x == startX:
                    screen.addch(y, x - 1, ord(' '))
                    screen.addch(y, x, ord(' '))
                    screen.addch(y, x + 1, ord(' '))
                    screen.move(y, x)
                    continue
                screen.addch(y, x - 1, ord(' '))
                screen.addch(y, x, ord(' '))
                screen.addch(y, x + 1, ord(' '))
                screen.addch(y - 1, startX + currentChar - 1, substring[currentChar - 1], curses.color_pair(0))
                screen.move(y, x - 1)
                screen.refresh()
                currentChar -= 1
                x -= 1
            else:
                screen.addch(y, x, q)
                if chr(q) != substring[currentChar]: # Mistake happened
                    color = (substring[currentChar] == ' ' and 3) or 1
                    screen.addch(y - 1, startX + currentChar, substring[currentChar], curses.color_pair(color))
                    screen.move(y, x + 1)
                    mistakes += 1
                else:
                    screen.addch(y - 1, startX + currentChar, substring[currentChar], curses.color_pair(2))
                    screen.move(y, x + 1)
                screen.refresh()
                x += 1
                currentChar += 1
        except:
            print q
    elapsed = time.time() - now
    wpm = len(message) / float(elapsed) * 12
    accuracy = float(mistakes) / len(message) * 100
    screen.clear()
    screen.addstr(y, startX, "Your WPM is %5.2f" % wpm)
    screen.addstr(y + 1, startX, "Your accuracy is %{0:4.2f}".format(100 - accuracy))
    screen.getch()

time.sleep(2)
curses.wrapper(main)

Offline

#6 2014-01-22 14:12:50

canibanoglu
New Member
Registered: 2014-01-19
Posts: 7

Re: Weird resize -s A B behavior

I believe I have found the reason why this behavior happens. After going through some security tickets for Debian and *nix in general, I have learned that Xterm doesn't allow window operations by default due to security reasons. I have also learned that if needed one can modify .Xdefaults or .Xresources by adding xterm*allowWindowOps = True but personally, I couldn't get this to work on my #! installation.

As far as curses programs go it is widely suggested the program should be written to handle a variety of different sizes and ideally one shouldn't even try to resize the window without the user's consent.

Leaving this here as a help for the future so that others can also benefit from what I have learned.

Offline

#7 2014-01-22 14:16:21

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,302

Re: Weird resize -s A B behavior

Not sure what you are after, but you have to reread .Xresources, like
xrdb ~/.Xresources

Offline

#8 2014-01-22 22:40:12

canibanoglu
New Member
Registered: 2014-01-19
Posts: 7

Re: Weird resize -s A B behavior

Yeah, I know but somehow that didn't change anything. I'll update the above post if I find out why that happened. This can safely be marked as solved because the unexpected behaviour from resize -s x y is actually the expected behaviour on most systems.

Offline

#9 2014-01-23 00:06:39

canibanoglu
New Member
Registered: 2014-01-19
Posts: 7

Re: Weird resize -s A B behavior

OK, discovered why Terminator wouldn't listen to .Xresources. Terminator doesn't read the .Xdefaults or .Xresources files because it has its own config. So the above command will not work under Terminator but I can say that it does work for UXrvt.

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo