1
0
mirror of https://github.com/amix/vimrc synced 2025-09-15 08:35:01 +08:00

add vim-minimap

This commit is contained in:
huhuaishun
2016-10-05 20:17:49 +08:00
committed by Huaishun Hu
parent 7762fafeb7
commit 54f1c0f3d0
26 changed files with 1742 additions and 1 deletions

View File

@ -0,0 +1,39 @@
from __future__ import print_function
from drawille import Canvas
import math
s = Canvas()
for x in range(1800):
s.set(x/10, math.sin(math.radians(x)) * 10)
print(s.frame())
s.clear()
for x in range(0, 1800, 10):
s.set(x/10, 10 + math.sin(math.radians(x)) * 10)
s.set(x/10, 10 + math.cos(math.radians(x)) * 10)
print(s.frame())
s.clear()
for x in range(0, 3600, 20):
s.set(x/20, 4 + math.sin(math.radians(x)) * 4)
print(s.frame())
s.clear()
for x in range(0, 360, 4):
s.set(x/4, 30 + math.sin(math.radians(x)) * 30)
for x in range(30):
for y in range(30):
s.set(x,y)
s.toggle(x+30, y+30)
s.toggle(x+60, y)
print(s.frame())

View File

@ -0,0 +1,207 @@
# -*- coding: utf-8 -*-
import curses
from drawille import Canvas, line
from time import sleep
from thread import start_new_thread
from Queue import Queue
import locale
from random import randint
locale.setlocale(locale.LC_ALL,"")
stdscr = curses.initscr()
stdscr.refresh()
keys = Queue()
speed = 0.0
fps = 20
frame_no = 0
score = 0
delta = frame_no / fps
height = 100
width = 100
position = height / 2
bird_map = [
#1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
[0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0], #1
[0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0], #2
[0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0], #3
[0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0], #4
[0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0], #5
[0,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,0,1,0,0], #6
[1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0], #7
[1,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0], #8
[1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1], #9
[1,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0], #0
[0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0], #1
[0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0], #2
[0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0], #3
]
bird = []
for y, row in enumerate(bird_map):
for x,col in enumerate(row):
if col:
bird.append((x, y))
def read_keys(stdscr):
while 1:
c = stdscr.getch()
keys.put(c)
class Bar():
def __init__(self, bar_width, cap_height=4, space=3*13):
self.height = randint(cap_height+space+1, height-1-cap_height)
self.width = bar_width
self.cap_height = cap_height
self.x = width - bar_width - 1
self.space = space
def draw(self):
for x,y in line(self.x,
self.height,
self.x+self.width,
self.height):
yield x, y
for x,y in line(self.x,
self.height,
self.x,
self.height+self.cap_height):
yield x, y
for x,y in line(self.x+self.width,
self.height,
x+self.width,
self.height+self.cap_height):
yield x, y
for x,y in line(self.x,
self.height+self.cap_height,
self.x+2,
self.height+self.cap_height):
yield x, y
for x,y in line(self.x+self.width-2,
self.height+self.cap_height,
self.x+self.width,
self.height+self.cap_height):
yield x, y
for x,y in line(self.x+2,
self.height+self.cap_height,
self.x+2,
height):
yield x, y
for x,y in line(self.x+self.width-2,
self.height+self.cap_height,
self.x+self.width-2,
height):
yield x, y
for x,y in line(self.x,
self.height-self.space,
self.x+self.width,
self.height-self.space):
yield x, y
for x,y in line(self.x,
self.height-self.space,
self.x,
self.height-self.cap_height-self.space):
yield x, y
for x,y in line(self.x+self.width,
self.height-self.space,
x+self.width,
self.height-self.cap_height-self.space):
yield x, y
for x,y in line(self.x,
self.height-self.cap_height-self.space,
self.x+2,
self.height-self.cap_height-self.space):
yield x, y
for x,y in line(self.x+self.width-2,
self.height-self.cap_height-self.space,
self.x+self.width,
self.height-self.cap_height-self.space):
yield x, y
for x,y in line(self.x+2,
self.height-self.cap_height-self.space,
self.x+2,
0):
yield x, y
for x,y in line(self.x+self.width-2,
self.height-self.cap_height-self.space,
self.x+self.width-2,
0):
yield x, y
def check_collision(bird_pos, bar):
# TODO more efficient collision detection
if bar.x > 21:
return False
if bar.height <= bird_pos-13 and bar.height+bar.space > bird_pos:
return False
for bar_x, bar_y in bar.draw():
for bird_x, bird_y in bird:
if int(bird_x) == int(bar_x) and int(bird_y+bird_pos) == int(bar_y):
return True
return False
def main(stdscr):
global frame_no, speed, position, score
c = Canvas()
bar_width = 16
bars = [Bar(bar_width)]
stdscr.refresh()
while True:
frame_no += 1
for bar in bars:
if check_collision(position, bar):
return
while not keys.empty():
if keys.get() == 113:
return
speed = 32.0
c.set(0,0)
c.set(width, height)
if frame_no % 50 == 0:
bars.append(Bar(bar_width))
for x,y in bird:
c.set(x,y+position)
for bar_index, bar in enumerate(bars):
if bar.x < 1:
bars.pop(bar_index)
score += 1
else:
bars[bar_index].x -= 1
for x,y in bar.draw():
c.set(x,y)
f = c.frame()+'\n'
stdscr.addstr(0, 0, f)
stdscr.addstr(height/4+1, 0, 'score: {0}'.format(score))
stdscr.refresh()
c.clear()
speed -= 2
position -= speed/10
if position < 0:
position = 0
speed = 0.0
elif position > height-13:
position = height-13
speed = 0.0
sleep(1.0/fps)
if __name__ == '__main__':
start_new_thread(read_keys, (stdscr,))
curses.wrapper(main)
print('Final score: {0}'.format(score))

View File

@ -0,0 +1,121 @@
# example:
# $ PYTHONPATH=`pwd` python examples/image2term.py http://fc00.deviantart.net/fs71/f/2011/310/5/a/giant_nyan_cat_by_daieny-d4fc8u1.png -t 100 -r 0.01
try:
from PIL import Image
except:
from sys import stderr
stderr.write('[E] PIL not installed\n')
exit(1)
from drawille import Canvas
from StringIO import StringIO
import urllib2
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
import fcntl
import termios
import struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def image2term(image, threshold=128, ratio=None, invert=False):
if image.startswith('http://') or image.startswith('https://'):
i = Image.open(StringIO(urllib2.urlopen(image).read())).convert('L')
else:
i = Image.open(open(image)).convert('L')
w, h = i.size
if ratio:
w = int(w * ratio)
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
else:
tw = getTerminalSize()[0]
tw *= 2
if tw < w:
ratio = tw / float(w)
w = tw
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
can = Canvas()
x = y = 0
try:
i_converted = i.tobytes()
except AttributeError:
i_converted = i.tostring()
for pix in i_converted:
if invert:
if ord(pix) > threshold:
can.set(x, y)
else:
if ord(pix) < threshold:
can.set(x, y)
x += 1
if x >= w:
y += 1
x = 0
return can.frame(0, 0)
def argparser():
import argparse
from sys import stdout
argp = argparse.ArgumentParser(description='drawille - image to terminal example script')
argp.add_argument('-o', '--output'
,help = 'Output file - default is STDOUT'
,metavar = 'FILE'
,default = stdout
,type = argparse.FileType('w')
)
argp.add_argument('-r', '--ratio'
,help = 'Image resize ratio'
,default = None
,action = 'store'
,type = float
,metavar = 'N'
)
argp.add_argument('-t', '--threshold'
,help = 'Color threshold'
,default = 128
,action = 'store'
,type = int
,metavar = 'N'
)
argp.add_argument('-i', '--invert'
,help = 'Invert colors'
,default = False
,action = 'store_true'
)
argp.add_argument('image'
,metavar = 'FILE'
,help = 'Image file path/url'
)
return vars(argp.parse_args())
def __main__():
args = argparser()
args['output'].write(image2term(args['image'], args['threshold'], args['ratio'], args['invert']))
args['output'].write('\n')
if __name__ == '__main__':
__main__()

View File

@ -0,0 +1,108 @@
from drawille import Canvas, line
import curses
import math
from time import sleep
import locale
locale.setlocale(locale.LC_ALL,"")
stdscr = curses.initscr()
stdscr.refresh()
class Point3D:
def __init__(self, x = 0, y = 0, z = 0):
self.x, self.y, self.z = float(x), float(y), float(z)
def rotateX(self, angle):
""" Rotates the point around the X axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
y = self.y * cosa - self.z * sina
z = self.y * sina + self.z * cosa
return Point3D(self.x, y, z)
def rotateY(self, angle):
""" Rotates the point around the Y axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
z = self.z * cosa - self.x * sina
x = self.z * sina + self.x * cosa
return Point3D(x, self.y, z)
def rotateZ(self, angle):
""" Rotates the point around the Z axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
x = self.x * cosa - self.y * sina
y = self.x * sina + self.y * cosa
return Point3D(x, y, self.z)
def project(self, win_width, win_height, fov, viewer_distance):
""" Transforms this 3D point to 2D using a perspective projection. """
factor = fov / (viewer_distance + self.z)
x = self.x * factor + win_width / 2
y = -self.y * factor + win_height / 2
return Point3D(x, y, 1)
vertices = [
Point3D(-20,20,-20),
Point3D(20,20,-20),
Point3D(20,-20,-20),
Point3D(-20,-20,-20),
Point3D(-20,20,20),
Point3D(20,20,20),
Point3D(20,-20,20),
Point3D(-20,-20,20)
]
# Define the vertices that compose each of the 6 faces. These numbers are
# indices to the vertices list defined above.
faces = [(0,1,2,3),(1,5,6,2),(5,4,7,6),(4,0,3,7),(0,4,5,1),(3,2,6,7)]
def __main__(stdscr, projection=False):
angleX, angleY, angleZ = 0, 0, 0
c = Canvas()
while 1:
# Will hold transformed vertices.
t = []
for v in vertices:
# Rotate the point around X axis, then around Y axis, and finally around Z axis.
p = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
if projection:
# Transform the point from 3D to 2D
p = p.project(50, 50, 50, 50)
#Put the point in the list of transformed vertices
t.append(p)
for f in faces:
for x,y in line(t[f[0]].x, t[f[0]].y, t[f[1]].x, t[f[1]].y):
c.set(x,y)
for x,y in line(t[f[1]].x, t[f[1]].y, t[f[2]].x, t[f[2]].y):
c.set(x,y)
for x,y in line(t[f[2]].x, t[f[2]].y, t[f[3]].x, t[f[3]].y):
c.set(x,y)
for x,y in line(t[f[3]].x, t[f[3]].y, t[f[0]].x, t[f[0]].y):
c.set(x,y)
f = c.frame(-40, -40, 80, 80)
stdscr.addstr(0, 0, '{0}\n'.format(f))
stdscr.refresh()
angleX += 2
angleY += 3
angleZ += 5
sleep(1.0/20)
c.clear()
if __name__ == '__main__':
from sys import argv
projection = False
if '-p' in argv:
projection = True
curses.wrapper(__main__, projection)

View File

@ -0,0 +1,28 @@
from __future__ import print_function
from drawille import Canvas, line, animate
import math
def __main__():
i = 0
height = 40
while True:
frame = []
frame.extend([coords for coords in
line(0,
height,
180,
math.sin(math.radians(i)) * height + height)])
frame.extend([(x/2, height + math.sin(math.radians(x+i)) * height)
for x in range(0, 360, 2)])
yield frame
i += 2
if __name__ == '__main__':
animate(Canvas(), __main__, 1./60)

View File

@ -0,0 +1,24 @@
from drawille import Canvas
from timeit import timeit
c = Canvas()
frames = 1000 * 10
sizes = ((0, 0),
(10, 10),
(20, 20),
(20, 40),
(40, 20),
(40, 40),
(100, 100))
for x, y in sizes:
c.set(0, 0)
for i in range(y):
c.set(x, i)
r = timeit(c.frame, number=frames)
print('{0}x{1}\t{2}'.format(x, y, r))
c.clear()

View File

@ -0,0 +1,11 @@
from drawille import Turtle
t = Turtle()
for _ in range(36):
t.right(10)
for _ in range(36):
t.right(10)
t.forward(8)
print(t.frame())

View File

@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
from sys import argv
try:
from PIL import Image
except:
from sys import stderr
stderr.write('[E] PIL not installed')
exit(1)
from StringIO import StringIO
import urllib2
import re
from drawille import Canvas
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
import fcntl
import termios
import struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
def usage():
print('Usage: %s <url/id>')
exit()
if __name__ == '__main__':
if len(argv) < 2:
url = 'http://xkcd.com/'
elif argv[1] in ['-h', '--help']:
usage()
elif argv[1].startswith('http'):
url = argv[1]
else:
url = 'http://xkcd.com/%s/' % argv[1]
c = urllib2.urlopen(url).read()
img_url = re.findall('http:\/\/imgs.xkcd.com\/comics\/[^"\']+', c)[0]
i = Image.open(StringIO(urllib2.urlopen(img_url).read())).convert('L')
w, h = i.size
tw, th = getTerminalSize()
tw *= 2
th *= 2
if tw < w:
ratio = tw / float(w)
w = tw
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
can = Canvas()
x = y = 0
try:
i_converted = i.tobytes()
except AttributeError:
i_converted = i.tostring()
for pix in i_converted:
if ord(pix) < 128:
can.set(x, y)
x += 1
if x >= w:
y += 1
x = 0
print(can.frame())