让我成为窗口管理员!


14

我讨厌代码膨胀!

因此,我决定用高尔夫球版替换Windows 7系统。但是,我知道这并不容易,所以让我们首先构建一个原型。它将获取用户的窗口列表,并在标准输出上使用伪图形显示来显示它们。

例如:用户输入:

0,0,15,10
15,10,20,15
10,13,15,15
9,1,16,3
17,5,20,7
11,2,17,4
15,4,19,6
13,3,18,5

CodeGolf®Window Manager™输出:

────────────┐
│::::::::┌─────┴┐
│:::::::::││┌────┴┐
│::::::::└─┤:┌───┴┐
│:::::::::::└─┤::::├┐
│:::::::::::::└─┬──┘├┐
│:::::::::::::::├─┬─┘│
│:::::::::::::::│└──┘
│:::::::::::::::│
│:::::::::::::::│
└────────────┼
               │::::│
               │::::│
          ┌────┤::::│
          │::::││:::│
          └────┴────┘

输入:

  • 来自标准输入(或者,如果您的系统没有stdin,那么任何可以提供多行输入的方法)
  • 每行包含4个数字,以逗号分隔-窗口的坐标
  • 前两个数字:左上角;最后两个数字:右下角
  • 使用x,y符号

输出:

  • 将其写入标准输出(或者,如果您的系统没有stdout,则显示等宽文本的任何内容)
  • 使用代码页437中的单个框图绘制字符绘制窗口边界
  • 稍后在输入中指定的Windows使先前指定的窗口模糊
  • 用冒号填充窗口: :
  • 开头和结尾的空格都可以,只要它们不中断窗口的对齐

笔记:

  • 我要支持的最大分辨率:76(水平)乘57(垂直)
  • 无需支持错误的输入
  • 最大窗口数:255(如果需要限制)
  • 我的Windows 7 cmdShell默认情况下显示代码页437个字符;如果有人可以在linux上(使用xterm或其他方式)执行此操作,请在此处进行描述

作为参考,字符代码为:

┌da   ─c4   ┬c2   ┐bf

│b3   :3a   │b3   │b3

├c3   ─c4   ┼c5   ┤b4

└c0   ─c4   ┴c1   ┘d9

总计:12个不同的字符。


请注意,如果将以下代码粘贴到浏览器地址栏中,则可以在此页面上显示漂亮的窗口:javascript:$('pre').css('line-height','1em')
Michael M.

Answers:


3

Python,397个字符

#coding:437
import os
J=range
M=[[0]*76 for _ in J(57)]
for A,C,B,D in[map(int,q.split(','))for q in os.read(0,9999).split('\n')]:
 for x in J(A+1,B):
    for y in J(C+1,D):M[C][A]|=5;M[C][B]|=6;M[D][A]|=9;M[D][B]|=10;M[C][x]|=3;M[D][x]|=3;M[y][A]|=12;M[y][B]|=12;M[y][x]=16;M[y][x-1]&=~1;M[y][x+1]&=~2;M[y-1][x]&=~4;M[y+1][x]&=~8
C=" rl─d┌┐┬u└┘┴│├┤┼:"
for l in M:print''.join((C+C[1:])[m]for m in l)

更改C="...C=u"...,它将改为以unicode打印!要正确保存文件可能会比较棘手,因为代码页437个字符未转义(需要第一个“编码”注释行)。

该方法是使用按位运算符来逐步构建映射。高尔夫版本更少,带注释:

#coding:437
import os
J=range
# set up the field
# Each element is a bitfield. Flags are:
# 16 - inside a window?
# 8  - up
# 4  - down
# 2  - left
# 1  - right
M=[[0]*76 for _ in J(57)]
# for each window...
for A,C,B,D in[map(int,q.split(','))for q in os.read(0,9999).split('\n')]:
    # add the directions for the corners
    M[C][A]|=5;M[C][B]|=6;M[D][A]|=9;M[D][B]|=10
    # add the top and bottom edges
    for y in J(C+1,D):M[y][A]|=12;M[y][B]|=12
    # add the left and right edges
    for x in J(A+1,B):M[C][x]|=3;M[D][x]|=3 
    # deal with the middle
    for x in J(A+1,B):
       for y in J(C+1,D):
           # Clear the current spot by setting to inside a window
           M[y][x]=16
           # Remove the right direction from the left spot, top from the bottom, etc
           M[y][x-1]&=~1;M[y][x+1]&=~2;M[y-1][x]&=~4;M[y+1][x]&=~8
 # print it out
 C=u" rl─d┌┐┬u└┘┴│├┤┼:"
 for l in M:print''.join((C+C[1:])[m]for m in l)

6

JavaScript ES6(FF≥31.0),404个字符

w=s=>{a=[];for(i=0;i<57;)a[i++]=Array(76).fill(0);s.split('\n').map(e=>{r=e.split(',');a[x=r[1]][w=r[0]]|=5;a[x][y=r[2]]|=6;a[z=r[3]][w]|=9;a[z][y]|=10;for(i=x;++i<z;)a[i][w]|=12,a[i][w]&=14,a[i][y]|=12,a[i][y]&=13;for(i=w;++i<y;)a[x][i]|=3,a[x][i]&=11,a[z][i]|=3,a[z][i]&=7;for(i=x;++i<z;)for(j=w;++j<y;)a[i][j]=16});console.log(a.map(e=>e.map(t=>t==16?':':' xx─x┌┐┬x└┘┴│├┤┼'[t&15]).join('')).join('\n'))}

没有ES6:

function w(s){a=[];for(i=0;i<57;i++){a[i]=[];for(j=0;j<76;j++)a[i][j]=0}s.split('\n').forEach(function(e){r=e.split(',');a[r[1]][r[0]]|=5;a[r[1]][r[2]]|=6;a[r[3]][r[0]]|=9;a[r[3]][r[2]]|=10;for(i=r[1];++i<r[3];)a[i][r[0]]|=12,a[i][r[0]]&=14,a[i][r[2]]|=12,a[i][r[2]]&=13;for(i=r[0];++i<r[2];)a[r[1]][i]|=3,a[r[1]][i]&=11,a[r[3]][i]|=3,a[r[3]][i]&=7;for(i=r[1];++i<r[3];)for(j=r[0];++j<r[2];)a[i][j]=16});console.log(a.map(function(e){return e.map(function(t){return t==16?':':' xx─x┌┐┬x└┘┴│├┤┼'[t&15]}).join('')}).join('\n'))}

w('0,0,15,10\n15,10,20,15\n10,13,15,15\n9,1,16,3\n17,5,20,7\n11,2,17,4\n15,4,19,6\n13,3,18,5'); 正确输出OP的示例。

窗口的边缘是使用按位运算符(Up = 8,Bottom = 4,Left = 2,Right = 1)构建的。


没有经过详尽的测试,但似乎可以很好地使用548个字符的字符串文字而不是数组。(仅在Firefox中进行过测试。)
manatwork

您可以使用ECMAScript 6来保存一堆字符:function w(s){...}变得w=(s)=>{...}(对于所有其他函数文字而言,也是如此)。字符查找表可能可以替换为具有相同字符的字符串。
Martin Ender 2014年

今天晚些时候,Firefox 31.0发布后,您将可以使用Array.fill()初始化为“桌面”。
manatwork 2014年

@manatwork,我尝试使用FF Aurora [].fill([].fill(0,0,76),0,57),太可惜了,它不起作用。我可以写短于new Array(57).fill(new Array(76).fill(0))
Michael M.

尝试跳过new运算符:Array(57).fill(Array(76).fill(0))
manatwork 2014年

0

Python,672个字符

可读性较低的版本:

import sys
r=range
M=[0,0,0,191,0,196,218,194,0,217,179,180,192,193,195,197]
Z=[map(int,l.split(",")) for l in sys.stdin.readlines()]
S=[[[0]*5 for x in r(77) ] for y in r(58)]
for i in r(len(Z)):
 A,C,B,D=Z[i]
 for a,b,c in [(C,A,2),(C,A,3),(D,A,1),(D,A,2),(C,B,3),(C,B,4),(D,B,1),(D,B,4)]:S[a][b][c]=1
 for x in r(A+1,B):
  for a,b in [(C,2),(C,3),(C,4),(D,1),(D,2),(D,4)]:S[a][x][b]=(b+1)&1
 for y in r(C+1,D):
  for a,b in [(A,1),(A,2),(A,3),(B,1),(B,3),(B,4)]:S[y][a][b]=b&1
 for x in r(A+1,B):
  for y in r(C+1,D):S[y][x]=[i+1]+[0]*4
O=sys.stdout.write
for l in S:
 for k in l:
  c=' ';x=M[k[1]*8|k[2]*4|k[3]*2|k[4]]
  if k[0]:c=':'
  if x:c=chr(x) 
  O(c)
 O('\n')

从以下版本开始:

import sys

coords = [ tuple(map(int,l.strip().split(","))) for l in sys.stdin.readlines() ]

screen = [ [ [-1, [False,False,False,False]] for x in range(0, 77) ] for y in range(0, 58) ]

def mergeBorders(screen, w):
    x0,y0,x1,y1 = w
    screen[y0][x0][1][1] = True
    screen[y0][x0][1][2] = True
    screen[y1][x0][1][0] = True
    screen[y1][x0][1][1] = True
    screen[y0][x1][1][2] = True
    screen[y0][x1][1][3] = True
    screen[y1][x1][1][0] = True
    screen[y1][x1][1][3] = True

    for x in range(x0+1,x1):
        screen[y0][x][1][1] = True
        screen[y0][x][1][2] = False
        screen[y0][x][1][3] = True
        screen[y1][x][1][0] = False
        screen[y1][x][1][1] = True
        screen[y1][x][1][3] = True

    for y in range(y0+1,y1):
        screen[y][x0][1][0] = True
        screen[y][x0][1][1] = False
        screen[y][x0][1][2] = True
        screen[y][x1][1][0] = True
        screen[y][x1][1][2] = True
        screen[y][x1][1][3] = False

def paintInside(screen, w, wId):
    x0,y0,x1,y1 = w
    for x in range(x0+1,x1):
        for y in range(y0+1,y1):
            screen[y][x][0] = wId 
            screen[y][x][1] = [False, False, False, False]

for wId in range(len(coords)):
    w = coords[wId]
    mergeBorders(screen, w)
    paintInside(screen, w, wId)

borderMap = { (False, True, True, False): 0xda,
              (False, True, False, True): 0xc4,
              (False, True, True, True):  0xc2,
              (False, False, True, True): 0xbf,
              (True, False, True, False): 0xb3,
              (True, True, True, False):  0xc3,
              (True, True, True, True):   0xc5,
              (True, False, True, True):  0xb4,
              (True, True, False, False): 0xc0,
              (True, True, False, True):  0xc1,
              (True, False, False, True): 0xd9 }

def borderChar(c):
    return chr(borderMap[(c[0],c[1],c[2],c[3])])


for screenLine in screen:
    for contents in screenLine:
        c = ' '
        if True in contents[1]:
            c = borderChar(contents[1])
        elif contents[0] >= 0:
            c = ':'
        sys.stdout.write(c)
    sys.stdout.write('\n')
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.