From 47803ab4c587a5677806a704decebed1133fcf0e Mon Sep 17 00:00:00 2001 From: The-UltimateGamer <2357931342@qq.com> Date: Sun, 9 Aug 2020 20:57:07 +0800 Subject: [PATCH 1/3] Kinda done --- SnakeTest.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ kernel.py | 57 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 SnakeTest.py diff --git a/SnakeTest.py b/SnakeTest.py new file mode 100644 index 0000000..46d8764 --- /dev/null +++ b/SnakeTest.py @@ -0,0 +1,66 @@ +import pygame, sys, random + +width, height = 1024, 708 + +pygame.init() +screen = pygame.display.set_mode((width, height)) +pygame.display.set_caption("Winnux 58") +clock = pygame.time.Clock() +raster = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 15) + +class SnakeGame(object): + def __init__(self): + self.tileWidth = 32 + self.snakeLen = 1 + self.snakePos = [] + self.direction = 0 + self.speedX = (1, 0, -1, 0) + self.speedY = (0, 1, 0, -1) + self.sx, self.sy = 0, 0 + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) + self.lost = 0 + def draw(self, canvas): + if self.lost == 1: + for pos in self.snakePos[-self.snakeLen:]: + pygame.draw.rect(canvas, (60, 110, 5), (pos[0] * self.tileWidth, pos[1] * self.tileWidth, self.tileWidth, self.tileWidth)) + pygame.draw.rect(canvas, (190, 30, 50), (self.ix * self.tileWidth, self.iy * self.tileWidth, self.tileWidth, self.tileWidth)) + def move(self): + if self.lost == 1: + return + self.sx = (self.sx + self.speedX[self.direction]) % 20 + self.sy = (self.sy + self.speedY[self.direction]) % 15 + if (self.sx, self.sy) in self.snakePos[-self.snakeLen:]: + self.lost = 1 + self.snakePos.append((self.sx, self.sy)) + if self.sx == self.ix and self.sy == self.iy: + self.ix, self.iy = random.randint(0, 20), random.randint(0, 15) + self.snakeLen += 1 + def keyDown(self, key): + if key == pygame.K_UP: + self.direction = 3 + if key == pygame.K_DOWN: + self.direction = 1 + if key == pygame.K_LEFT: + self.direction = 2 + if key == pygame.K_RIGHT: + self.direction = 0 + if key == pygame.K_r and self.lost == 1: + self.snakeLen = 1 + self.snakePos = [] + self.sx, self.sy = 0, 0 + self.ix, self.iy = random.randint(0, 20), random.randint(0, 15) + self.lost = 0 + +snake = SnakeGame() +while True: + screen.fill((0, 0, 0)) + snake.draw(screen) + snake.move() + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + if event.type == pygame.KEYDOWN: + snake.keyDown(event.key) + pygame.display.update() + clock.tick(10) \ No newline at end of file diff --git a/kernel.py b/kernel.py index 5953a42..dd7f5bf 100644 --- a/kernel.py +++ b/kernel.py @@ -76,6 +76,7 @@ def __init__(self, picName): self.txtField = TxtField(0, 0, 0, 0) self.txtFieldEnabled = False self.canvas = pygame.Surface((width, height - 60)) + self.canvas.fill((40, 40, 40)) self.canvasEnabled = False self.secretList = [] def draw(self, screen): @@ -91,6 +92,10 @@ def draw(self, screen): self.txtField.content = self.txtField.content[-self.txtField.h:] self.txtField.draw(screen, self.txtField.content) if self.canvasEnabled: + if self.appID == snake.appID: + self.canvas.fill((40, 40, 40)) + snakeGame.draw(self.canvas) + snakeGame.move() framework.screen.blit(self.canvas, (0, 60)) def addButton(self, b): self.btnList.append(b) @@ -130,7 +135,51 @@ def keyDown(self, key): self.txtField.keyDown(key) if self.canvasEnabled: if self.appID == snake.appID: - pass + snakeGame.keyDown(key) + +class SnakeGame(object): + def __init__(self): + self.tileWidth = 32 + self.snakeLen = 1 + self.snakePos = [] + self.direction = 0 + self.speedX = (1, 0, -1, 0) + self.speedY = (0, 1, 0, -1) + self.sx, self.sy = 0, 0 + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) + self.lost = 0 + def draw(self, canvas): + if self.lost == 1: + framework.addDialog(Dialog("Game Over!", "Game over! Press R to restart!", DlgStatus.WARNING)) + for pos in self.snakePos[-self.snakeLen:]: + pygame.draw.rect(canvas, (60, 110, 5), (pos[0] * self.tileWidth, pos[1] * self.tileWidth, self.tileWidth, self.tileWidth)) + pygame.draw.rect(canvas, (190, 30, 50), (self.ix * self.tileWidth, self.iy * self.tileWidth, self.tileWidth, self.tileWidth)) + def move(self): + if self.lost == 1: + return + self.sx = (self.sx + self.speedX[self.direction]) % 20 + self.sy = (self.sy + self.speedY[self.direction]) % 15 + if (self.sx, self.sy) in self.snakePos[-self.snakeLen:]: + self.lost = 1 + self.snakePos.append((self.sx, self.sy)) + if self.sx == self.ix and self.sy == self.iy: + self.ix, self.iy = random.randint(0, 20), random.randint(0, 15) + self.snakeLen += 1 + def keyDown(self, key): + if key == pygame.K_UP: + self.direction = 3 + if key == pygame.K_DOWN: + self.direction = 1 + if key == pygame.K_LEFT: + self.direction = 2 + if key == pygame.K_RIGHT: + self.direction = 0 + if key == pygame.K_r and self.lost == 1: + self.snakeLen = 1 + self.snakePos = [] + self.sx, self.sy = 0, 0 + self.ix, self.iy = random.randint(0, 20), random.randint(0, 15) + self.lost = 0 class Button: def __init__(self, picFile, x, y, appID, **txt): @@ -280,12 +329,16 @@ def mouseDown(self, pos, button): bg = App("res/clouds.jpg") term = App("res/blank.jpg") snake = App("res/blank.jpg") +snake.enableCanvas() +snakeGame = SnakeGame() framework.appID = bg.appID framework.addApp(bg) framework.addApp(term) +framework.addApp(snake) framework.addDialog(Dialog("Hey there!", "Welcome to Winnux 58!")) -bg.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, term.appID, font=framework.raster, content="TERMINAL")) +bg.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, snake.appID, font=framework.raster, content="TERMINAL")) term.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, bg.appID, font=framework.raster, content="CLOSE")) +snake.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, bg.appID, font=framework.raster, content="CLOSE")) term.enableTxtField(0, 0, 80, 20) while True: From cb636cff75cff762225d9b485d3ba03b69628b27 Mon Sep 17 00:00:00 2001 From: The-UltimateGamer <2357931342@qq.com> Date: Mon, 10 Aug 2020 19:24:17 +0800 Subject: [PATCH 2/3] Snake done --- kernel.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/kernel.py b/kernel.py index dd7f5bf..a78086f 100644 --- a/kernel.py +++ b/kernel.py @@ -1,7 +1,7 @@ # kernel.py # Core of our OS -import pygame, sys, random +import pygame, sys, random, time from enum import Enum width, height = 1024, 768 @@ -76,7 +76,6 @@ def __init__(self, picName): self.txtField = TxtField(0, 0, 0, 0) self.txtFieldEnabled = False self.canvas = pygame.Surface((width, height - 60)) - self.canvas.fill((40, 40, 40)) self.canvasEnabled = False self.secretList = [] def draw(self, screen): @@ -93,7 +92,6 @@ def draw(self, screen): self.txtField.draw(screen, self.txtField.content) if self.canvasEnabled: if self.appID == snake.appID: - self.canvas.fill((40, 40, 40)) snakeGame.draw(self.canvas) snakeGame.move() framework.screen.blit(self.canvas, (0, 60)) @@ -140,6 +138,7 @@ def keyDown(self, key): class SnakeGame(object): def __init__(self): self.tileWidth = 32 + self.x0, self.y0 = 192, 144 self.snakeLen = 1 self.snakePos = [] self.direction = 0 @@ -148,12 +147,22 @@ def __init__(self): self.sx, self.sy = 0, 0 self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) self.lost = 0 + self.bigFont = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 100) + self.smallFont = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 60) + self.bg = pygame.Surface((640, 480)) + self.bg.fill((255, 255, 255)) + self.gameOver = self.bigFont.render("Game Over!", True, (255, 255, 255)) + self.restart = self.smallFont.render("PRESS R TO RESTART", True, (255, 255, 255)) def draw(self, canvas): + canvas.fill((40, 40, 40)) if self.lost == 1: - framework.addDialog(Dialog("Game Over!", "Game over! Press R to restart!", DlgStatus.WARNING)) + canvas.blit(self.gameOver, (250, 200)) + canvas.blit(self.restart, (230, 300)) + return + canvas.blit(self.bg, (self.x0, self.y0)) for pos in self.snakePos[-self.snakeLen:]: - pygame.draw.rect(canvas, (60, 110, 5), (pos[0] * self.tileWidth, pos[1] * self.tileWidth, self.tileWidth, self.tileWidth)) - pygame.draw.rect(canvas, (190, 30, 50), (self.ix * self.tileWidth, self.iy * self.tileWidth, self.tileWidth, self.tileWidth)) + pygame.draw.rect(canvas, (60, 110, 5), (self.x0 + pos[0] * self.tileWidth, self.y0 + pos[1] * self.tileWidth, self.tileWidth, self.tileWidth)) + pygame.draw.rect(canvas, (190, 30, 50), (self.x0 + self.ix * self.tileWidth, self.y0 + self.iy * self.tileWidth, self.tileWidth, self.tileWidth)) def move(self): if self.lost == 1: return @@ -163,7 +172,7 @@ def move(self): self.lost = 1 self.snakePos.append((self.sx, self.sy)) if self.sx == self.ix and self.sy == self.iy: - self.ix, self.iy = random.randint(0, 20), random.randint(0, 15) + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) self.snakeLen += 1 def keyDown(self, key): if key == pygame.K_UP: @@ -178,7 +187,7 @@ def keyDown(self, key): self.snakeLen = 1 self.snakePos = [] self.sx, self.sy = 0, 0 - self.ix, self.iy = random.randint(0, 20), random.randint(0, 15) + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) self.lost = 0 class Button: @@ -336,7 +345,8 @@ def mouseDown(self, pos, button): framework.addApp(term) framework.addApp(snake) framework.addDialog(Dialog("Hey there!", "Welcome to Winnux 58!")) -bg.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, snake.appID, font=framework.raster, content="TERMINAL")) +bg.addButton(Button("res/button/txt_btn.bmp", 20, 20, term.appID, font=framework.raster, content="TERMINAL")) +bg.addButton(Button("res/button/txt_btn.bmp", 20, 60, snake.appID, font=framework.raster, content="SNAKE")) term.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, bg.appID, font=framework.raster, content="CLOSE")) snake.addButton(Button("res/button/txt_btn.bmp", width // 2 - 35, 20, bg.appID, font=framework.raster, content="CLOSE")) term.enableTxtField(0, 0, 80, 20) From 53648ab0ceef385a1b4c43e2af6a43390b0d7bde Mon Sep 17 00:00:00 2001 From: The-UltimateGamer <2357931342@qq.com> Date: Tue, 11 Aug 2020 19:23:57 +0800 Subject: [PATCH 3/3] Primarily fixed --- TODO | 6 -- kernel.py | 208 ++++++++++++++++++++++++++++++++++------------------ terminal.py | 70 ++++++++++++++++-- vis.py | 6 +- 4 files changed, 204 insertions(+), 86 deletions(-) delete mode 100644 TODO diff --git a/TODO b/TODO deleted file mode 100644 index be949ec..0000000 --- a/TODO +++ /dev/null @@ -1,6 +0,0 @@ -- Integrate TextDraw.py and the buffer provided into kernel.py - Specifications: - - Should contain a variable that controls the availability of the buffer - - Because of this, apps will trigger the buffer by setting the value of that variable - - Should have a button to go back (to the desktop), which means integrating a button too - - Preferably make this into a class and make the buffer sizeable \ No newline at end of file diff --git a/kernel.py b/kernel.py index 6b9c72d..76732cd 100644 --- a/kernel.py +++ b/kernel.py @@ -6,14 +6,76 @@ from contextlib import redirect_stdout from enum import Enum -from ls import ls -from rm import rm -from pwd import pwd -from cat import cat -from calc import calc - width, height = 1024, 768 +def pwd(working_dir, *args): + print(working_dir, end='') + +def ls(working_dir, *args): + files = open("files.img", 'r+') + lines = files.read().strip('\0').split('\n') + for i, line in enumerate(lines): + if line == "!LOC=%s" % working_dir: + print(lines[i + 1].strip('!FNAME='), end='') + +def cat(working_dir, *args): + if not args: + print("Missing argument. Usage: cat ", end='') + return + for target in args: + files = open("files.img", 'r+') + lines = files.read().strip('\0').split('\n') + contents = [] + for i, line in enumerate(lines): + if line == '!LOC=%s' % working_dir \ + and lines[i + 1] == '!FNAME=%s' % target: + for i in range(i+2, len(lines)): + if lines[i] and lines[i][0] == '!': break + contents.append(lines[i]) + break + else: + print("cat: %s: no such file or directory" % target, end='') + print(''.join(contents)) + files.close() + +def rm(working_dir, *args): + files = open("files.img", 'r+') + to_be_written = [] + target = args[0] + lines = files.read().strip('\0').split('\n') + write_or_not = True + for i, line in enumerate(lines): + if write_or_not: + if line == '!FNAME=%s' % target \ + and lines[i-1] == '!LOC=%s' % working_dir: + write_or_not = False + del to_be_written[-1] + else: + if line and line[0] == '!': + write_or_not = True + if write_or_not: + to_be_written.append(line) + files.close() + fw = open("files.img", 'w+') + fw.write('\n'.join(to_be_written)) + fw.close() + +def calc(working_dir, *args): + expr = input() + while expr != 'q': + result = None + try: + result = eval(expr) + except ZeroDivisionError: + print("Don't divide by zero!", end='') + except SyntaxError: + print("Your expression doesn't seem to be valid.", end='') + except: + print("This expression doesn't seem to work.", end='') + if result: + print(result, end='') + expr = input() + class Pic(object): def __init__(self, fileName): img = pygame.image.load(fileName) @@ -108,7 +170,7 @@ def addButton(self, b): def addTooltip(self, txt, font, x, y, c, rect): tt = Tooltip(txt, font, x, y, c, rect) self.txtList.append(tt) - def enableTxtField(self, x, y, w, h, placeholder = "/# "): + def enableTxtField(self, x, y, w, h, placeholder = "/$ "): if self.canvasEnabled: print("Only one of either the text field or the canvas can be enabled in an App.") return @@ -143,61 +205,6 @@ def keyDown(self, key): if self.appID == snake.appID: snakeGame.keyDown(key) -class SnakeGame(object): - def __init__(self): - self.tileWidth = 32 - self.x0, self.y0 = 192, 144 - self.snakeLen = 1 - self.snakePos = [] - self.direction = 0 - self.speedX = (1, 0, -1, 0) - self.speedY = (0, 1, 0, -1) - self.sx, self.sy = 0, 0 - self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) - self.lost = 0 - self.bigFont = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 100) - self.smallFont = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 60) - self.bg = pygame.Surface((640, 480)) - self.bg.fill((255, 255, 255)) - self.gameOver = self.bigFont.render("Game Over!", True, (255, 255, 255)) - self.restart = self.smallFont.render("PRESS R TO RESTART", True, (255, 255, 255)) - def draw(self, canvas): - canvas.fill((40, 40, 40)) - if self.lost == 1: - canvas.blit(self.gameOver, (250, 200)) - canvas.blit(self.restart, (230, 300)) - return - canvas.blit(self.bg, (self.x0, self.y0)) - for pos in self.snakePos[-self.snakeLen:]: - pygame.draw.rect(canvas, (60, 110, 5), (self.x0 + pos[0] * self.tileWidth, self.y0 + pos[1] * self.tileWidth, self.tileWidth, self.tileWidth)) - pygame.draw.rect(canvas, (190, 30, 50), (self.x0 + self.ix * self.tileWidth, self.y0 + self.iy * self.tileWidth, self.tileWidth, self.tileWidth)) - def move(self): - if self.lost == 1: - return - self.sx = (self.sx + self.speedX[self.direction]) % 20 - self.sy = (self.sy + self.speedY[self.direction]) % 15 - if (self.sx, self.sy) in self.snakePos[-self.snakeLen:]: - self.lost = 1 - self.snakePos.append((self.sx, self.sy)) - if self.sx == self.ix and self.sy == self.iy: - self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) - self.snakeLen += 1 - def keyDown(self, key): - if key == pygame.K_UP: - self.direction = 3 - if key == pygame.K_DOWN: - self.direction = 1 - if key == pygame.K_LEFT: - self.direction = 2 - if key == pygame.K_RIGHT: - self.direction = 0 - if key == pygame.K_r and self.lost == 1: - self.snakeLen = 1 - self.snakePos = [] - self.sx, self.sy = 0, 0 - self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) - self.lost = 0 - class Button: def __init__(self, picFile, x, y, appID, **txt): self.img = pygame.image.load(picFile).convert() @@ -273,15 +280,15 @@ def draw(self, screen, lines, c = (255, 255, 255), y = 0): img = self.raster.render(line, True, c) screen.blit(img, (0, y)) y += 30 - def exec_cmd(self, on_scr): cmd = on_scr.split() - main = cmd.pop() + try: main = cmd.pop(0) + except: pass output = io.StringIO() with redirect_stdout(output): - exec("%s('%s', %s)" % (main, self.pwd, str(cmd))) + try: exec("%s('%s', %s)" % (main, self.pwd, str(cmd))) + except: print("%s: command not found" % on_scr, end='') self.txtBuffer.append(output.getvalue()) - def keyUp(self, key): if key == pygame.K_LSHIFT or key == pygame.K_RSHIFT: self.shift = False @@ -290,14 +297,12 @@ def keyUp(self, key): def keyDown(self, key): if key == pygame.K_BACKSPACE: if (self.txtBuffer and self.txtBuffer[-1] != '\n') or not self.txtBuffer: - try: - self.txtBuffer.pop() - except: - pass + try: self.txtBuffer.pop() + except: pass elif key == pygame.K_RETURN: cmd = '' i = -1 - while len(self.txtBuffer) > -i-1 and self.txtBuffer[i] != '\n': + while len(self.txtBuffer) > - i - 1 and self.txtBuffer[i] != '\n': cmd = self.txtBuffer[i] + cmd i -= 1 self.txtBuffer.append('\r') @@ -363,9 +368,70 @@ def draw(self, screen): def mouseDown(self, pos, button): self.closeBtn.mouseDown(pos, button) +class SnakeGame: + def __init__(self): + self.tileWidth = 32 + self.x0, self.y0 = 192, 144 + self.snakeLen = 1 + self.snakePos = [] + self.direction = 0 + self.speedX = (1, 0, -1, 0) + self.speedY = (0, 1, 0, -1) + self.sx, self.sy = 0, 0 + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) + self.lost = 0 + self.bigFont = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 100) + self.smallFont = pygame.font.Font("res/Perfect_DOS_VGA_437.ttf", 60) + self.bg = pygame.Surface((640, 480)) + self.bg.fill((255, 255, 255)) + self.gameOver = self.bigFont.render("Game Over!", True, (255, 255, 255)) + self.restart = self.smallFont.render("PRESS R TO RESTART", True, (255, 255, 255)) + def draw(self, canvas): + canvas.fill((40, 40, 40)) + if self.lost == 1: + canvas.blit(self.gameOver, (250, 200)) + canvas.blit(self.restart, (230, 300)) + return + canvas.blit(self.bg, (self.x0, self.y0)) + for pos in self.snakePos[-self.snakeLen:]: + pygame.draw.rect(canvas, (60, 110, 5), (self.x0 + pos[0] * self.tileWidth, self.y0 + pos[1] * self.tileWidth, self.tileWidth, self.tileWidth)) + pygame.draw.rect(canvas, (190, 30, 50), (self.x0 + self.ix * self.tileWidth, self.y0 + self.iy * self.tileWidth, self.tileWidth, self.tileWidth)) + def move(self): + if self.lost == 1: + return + self.sx = (self.sx + self.speedX[self.direction]) % 20 + self.sy = (self.sy + self.speedY[self.direction]) % 15 + if (self.sx, self.sy) in self.snakePos[-self.snakeLen:]: + self.lost = 1 + self.snakePos.append((self.sx, self.sy)) + if self.sx == self.ix and self.sy == self.iy: + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) + self.snakeLen += 1 + def keyDown(self, key): + if key == pygame.K_UP: + self.direction = 3 + if key == pygame.K_DOWN: + self.direction = 1 + if key == pygame.K_LEFT: + self.direction = 2 + if key == pygame.K_RIGHT: + self.direction = 0 + if key == pygame.K_r and self.lost == 1: + self.snakeLen = 1 + self.snakePos = [] + self.sx, self.sy = 0, 0 + self.ix, self.iy = random.randint(0, 19), random.randint(0, 14) + self.lost = 0 + +class Terminal(TxtField): + def __init__(self, x, y, w, h, pwd = '/'): + self.pwd = pwd + super().__init__(x, y, w, h) + framework = Kernel() bg = App("res/clouds.jpg") term = App("res/blank.jpg") +termCtrl = Terminal(0, 0, 80, 20) snake = App("res/blank.jpg") snake.enableCanvas() snakeGame = SnakeGame() diff --git a/terminal.py b/terminal.py index 28aa57a..101f815 100644 --- a/terminal.py +++ b/terminal.py @@ -1,9 +1,67 @@ -from math import * -from kernel import TxtField - class Terminal(TxtField): def __init__(self, x, y, w, h): self.pwd = '/' - super().__init__(x, y, w, h, '%s# ' % self.pwd) - - + super().__init__(x, y, w, h, '%s$ ' % self.pwd) + def pwd(working_dir, args): + print(working_dir) + def ls(working_dir, *args): + files = open("files.img", 'r+') + lines = files.read().strip('\0').split('\n') + for i, line in enumerate(lines): + if line == "!LOC=%s" % working_dir: + print(lines[i+1].strip('!FNAME=')) + def cat(working_dir, *args): + if not args: + print("Missing argument.\nUsage: cat ") + return + for target in args: + files = open("files.img", 'r+') + lines = files.read().strip('\0').split('\n') + contents = [] + for i, line in enumerate(lines): + if line == '!LOC=%s' % working_dir \ + and lines[i + 1] == '!FNAME=%s' % target: + for i in range(i+2, len(lines)): + if lines[i] and lines[i][0] == '!': break + contents.append(lines[i]) + break + else: + print("cat: %s: no such file or directory" % target) + print('\n'.join(contents)) + files.close() + def rm(working_dir, *args): + files = open("files.img", 'r+') + to_be_written = [] + target = args[0] + lines = files.read().strip('\0').split('\n') + write_or_not = True + for i, line in enumerate(lines): + if write_or_not: + if line == '!FNAME=%s' % target\ + and lines[i-1] == '!LOC=%s' % working_dir: + write_or_not = False + del to_be_written[-1] + else: + if line and line[0] == '!': + write_or_not = True + if write_or_not: + to_be_written.append(line) + files.close() + fw = open("files.img", 'w+') + fw.write('\n'.join(to_be_written)) + fw.close() + def calc(working_dir, *args): + expr = input() + while expr != 'q': + result = None + try: + result = eval(expr) + except ZeroDivisionError: + print("Don't divide by zero!") + except SyntaxError: + print("Your expression doesn't seem to be valid.") + except: + print("This expression doesn't seem to work.") + if result: + print(result) + expr = input() \ No newline at end of file diff --git a/vis.py b/vis.py index d9dd10b..77fb049 100644 --- a/vis.py +++ b/vis.py @@ -1,8 +1,8 @@ +# TODO: Rewrite vis file I/O + # vis.py # Basic CLI text editor -import sys - # Accepts text input from user def write(f): # Keeps asking for input @@ -27,7 +27,7 @@ def write(f): cmd = input() if cmd == "i": f = open(sys.argv[1], "w+") - write(f); + write(f) elif cmd == "a": f = open(sys.argv[1], "a+") write(f)