You are Here:
Linux Lite 4.4 Final has been released. See the Release Announcements section for more information.



colourfind (either version of python, or pypy)

Author (Read 124 times)

0 Members and 2 Guests are viewing this topic.

colourfind (either version of python, or pypy)
« on: April 05, 2019, 12:06:18 PM »
 

freemedia2018

  • New to Forums
  • *
  • 6
    Posts
  • Country: us
  • Reputation: 1
  • Linux Lite Member
    • digital literacy for everyone

  • Linux Lite: 3.8 32bit

  • CPU: varies

  • VIDEO CARD: not nvidia
usage:

find | colourfind
or:
find | colourfind -d
(which adds the date)

since I'm new here, I expect that you wouldn't simply trust this code without some kind of vetting. It happens to be perfectly alright, but that isn't the point.

Code: [Select]
#!/usr/bin/env python3
# coding: utf-8
#### license: creative commons cc0 1.0 (public domain)
#### http://creativecommons.org/publicdomain/zero/1.0/
proginf = "colourfind, mar 2019 mn"

import sys
import os
from datetime import datetime

from sys import stdin, stdout
from os import popen
from os import name as osname

def colortext(f, b ):
    if  f == None:
        f = 0
    if  b == None:
        b = 0
    n = "0"
    if  f > 7:
        n = "1"
        f = f - 8
    if  f == 1:
        f = 4 ## switch ansi colours for qb
    elif f == 4:
        f = 1
    if  f == 3:
        f = 6
    elif f == 6:
        f = 3
    if  b > 7:
        b = b - 8
    if  b == 1:
        b = 4
    elif b == 4:
        b = 1
    if  b == 3:
        b = 6
    elif b == 6:
        b = 3
    return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+ b ) + "m"

def padleft(p, s, c=7):
    if type( c ) == int:
        c = (c, 0)
    else:
        c = c.split()
    return colortext(int(c[0]),
    int(c[1])) + (" " * s + str(p))[-s:] + colortext(7, 0)

def padright(p, s, c="5 0"):
    if type( c ) == int:
        c = (c, 0)
    else:
        c = c.split()
    return colortext(int(c[0]),
    int(c[1])) + (str(p) + " " * s)[:s] + colortext(7, 0)

def colourdc(c, f="p"):
    if type( c ) != int: c = c.lower()
    if c in [0, "0"]:
        return "9 0"
    cb = "0"
    if len(f.split()) > 1:
        cb = "1"
    if c in [2, ".bat", ".com", ".exe"]:
        return "10 " + cb
    if c in [1, ".lnk"]:
        return "11 " + cb
    if c in [".tgz", ".tar.gz", ".gz", ".zip", ".7z"]:
        return "4 " + cb
    if c in [".fig", ".alex", ".py", ".fig.py", ".rose"]:
        return "2 " + cb
    if c in [".ogg", ".mp4", ".mpeg", ".mpg", ".mp3", ".webm", ".odt",
    ".odp", ".pdf"]:
        return "3 " + cb
    if c in ["."]:
        return "8 " + cb
    if c in [".jpg", ".jpeg", ".gif", ".bmp", ".pbm", ".pgm", ".ppm",
    ".tga", ".xbm", ".xpm", ".tif", ".tiff", ".png", ".svg", ".svgz",
    ".mng", ".pcx", ".mov", ".mpg", ".mpeg", ".m2v", ".mkv", ".ogm",
    ".m4v", ".mp4v", ".vob", ".flc", ".avi", ".fli", ".flv", ".gl",
    ".dl", ".xcf", ".xwd", ".yuv", ".cgm", ".emf", ".ogv"]:
        return "13 " + cb
    if c in [".htm", ".html", ".txt", ".js", ".php"]:
        return "14 " + cb
    return "7 " + cb

dates = ""
cw = 80
sizel = 0

dates = dates[1:]

from os import listdir
from os import path

for fp in stdin:   
    fp = fp[:-1]
    b = []
    bfext = 0
    bp = 0
    bfs = 0
    bfiletime = 19
 
    try:
        paths = 1
        for p in chr(0):
            p = fp
            paths = 1
            fext = path.splitext(fp + path.sep + p)[1]
            if len(p) > bfext:
                bfext = len(p)

            try:
                fs = int(os.path.getsize(fp))
                bfs = 0
            except:
                fs = ""

            try:
                if os.access(fp, os.X_OK):
                    fext = 2
                if os.path.isdir(fp):
                    fext = 0
                if os.path.islink(fp):
                    fext = 1
            except:
                pass

            try:
                filetime = str(
                datetime.fromtimestamp(os.path.getmtime(fp)))[0:19]
            except:
                filetime = ""

            tab = chr(9)

            pld = 1
            tab = chr(9)
            bfile = tab + tab + padleft(str(filetime), bfiletime)
            try:
                flag = (sys.argv[1] == "-d")
            except:
                flag = 0
            if not flag:
                bfile = ""
            print(str(fs) + " " + tab +  padright(p,
            bfext, colourdc(fext, p)) + bfile)

    except OSError:
        pass
 


Tags:
 


Linux Lite 4.4 Final has been released. See the Release Announcements section for more information.