1

I am trying to do a text justification on the following array and transform it into a chat system as pictured. So far I am able to get the alignment right but I am stuck on trying to make it looks like a chat conversation

"1", "2" indicate the user, and their message, and the array should determine the alternating order of the alignment. Width is the total width of a line, user width is the max width that a user can take in one line. user 1 is aligned to the left, and user 2 to the right.

So far I have the following code:

messages = [["1", "Bob hello"], ["2", "Alice hi"], ["1", "How is your life"], ["1", "Better than before"],
            ["2", "M super"], ["1", "Wow pro"]]
userWidth = 6
width = 15


def chat(messages, userWidth, width):
    user1 = []
    user2 = []
    sep_num = []
    order = []
    for message in messages:
        user = message[0]
        convo = message[1]
        windowStart = 0
        sep_num.append(len(convo) // userWidth + 1)
        order.append(user)
        for windowEnd in range(len(convo)):
            if windowEnd > 0:
                if (windowEnd + 1) % userWidth == 0:
                    if user == "1":
                        left_aligned = convo[windowStart:windowEnd + 1]
                        user1.append(left_aligned)
                    else:
                        right_aligned = convo[windowStart:windowEnd + 1]
                        user2.append(right_aligned)
                    windowStart = windowEnd + 1
                if windowEnd == len(convo) - 1:
                    if user == "1":
                        left_aligned = convo[windowStart:windowEnd + 1]
                        if len(left_aligned) == 1 and user1[-1][-3] != " ":
                            left_aligned = "".join([user1[-1][-1],left_aligned])
                            user1[-1] = user1[-1][:-1]
                        if len(left_aligned) == 1 and user1[-1][-3] == " ":
                            left_aligned = "".join([user1[-1][-3:], left_aligned])
                            user1[-1] = user1[-1][:-3]
                        user1.append(left_aligned)
                    else:
                        right_aligned = convo[windowStart:windowEnd + 1]
                        if len(right_aligned) == 1 and user2[-1][-3] != " ":
                            right_aligned = "".join([user2[-1][-1], right_aligned])
                            user2[-1] = user2[-1][:-1]
                        if len(right_aligned) == 1 and user1[-1][-3] == " ":
                            right_aligned = "".join([user1[-1][-3:], right_aligned])
                            user1[-1] = user1[-1][:-3]
                        user2.append(right_aligned)

    constructor(user1, user2, width, order, sep_num)

def constructor(user1, user2, width, order, sep_num):
    for i in range(len(user1)):
        if (len(user1[i])) > 1:
            if user1[i][0] == " ":
                user1[i] = user1[i][1:]
            space = width - len(user1[i])
            line = "|" + user1[i] + (" " * space)
            print(line)
    for i in range(len(user2)):
        if (len(user2[i])) > 1:
            if user2[i][-1] == " ":
                user2[i] = user2[i][:-1]
            space = width - len(user2[i])
            line = (" " * space) + user2[i] + "|"
            print(line)

which makes it look like this:

|Bob he         
|llo            
|How is         
|your           
|life           
|Better         
|than           
|before         
|Wow            
|pro            
          Alice|
             hi|
          M sup|
             er|

But how can I transform it into the following:

text_justification

bloomsdayforever
  • 263
  • 2
  • 13

1 Answers1

0

You can try something like this:

from textwrap import wrap

messages = [["1", "Bob hello"], ["2", "Alice hi"], ["1", "How is your life"], ["1", "Better than before"],
            ["2", "M super"], ["1", "Wow pro"]]

win = 15
lw = 6

print("+" + "*" * win + "+")
for num, msg in messages:
    pad = "<" if num == "1" else ">"
    print("\n".join(f"|{s:{pad}{win}}|" for s in wrap(msg, lw)))
print("+" + "*" * win + "+")

It gives:

+***************+
|Bob            |
|hello          |
|          Alice|
|             hi|
|How is         |
|your           |
|life           |
|Better         |
|than           |
|before         |
|              M|
|          super|
|Wow            |
|pro            |
+***************+
bb1
  • 7,174
  • 2
  • 8
  • 23