<commit_before>#!/usr/bin/env python3

# (C) 2016-04-08 <NAME> (DerWOK)

import os
import sys
import os.path
from os.path import expanduser
import tty
import termios
import shutil
import signal
import time

VERSION = "0.1"
CONFIGFILE = expanduser("~")+"/.tailor"
TAILCMD = "tail -F"
KEYS = "<KEY>"
HISTORYMAX = len(KEYS)
HISTORY = []


if os.name == "nt":
    print("Sorry, no Win support.")
    exit(1)


# Read a single key from keyboard
def _getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


def read_history_from_file():
    global HISTORY
    if not os.path.isfile(CONFIGFILE):
        return
    with open(CONFIGFILE) as infile:
        HISTORY = infile.readlines()
    for i in range(0, len(HISTORY)):
        HISTORY[i] = HISTORY[i].rstrip('\n')


def save_history_to_file():
    outfile = open(CONFIGFILE, 'w')
    outfile.write("\n".join(HISTORY))
    outfile.close()


def add_to_history(logfile):
    global HISTORY
    for i in range(len(HISTORY)-1, -1, -1):   # remove old duplicates
        if HISTORY[i] == logfile:
            del HISTORY[i]
            print("DEL:"+str(i+1))
    HISTORY.insert(0, logfile)
    if len(HISTORY) > HISTORYMAX:           # shorten list
        HISTORY = HISTORY[0:HISTORYMAX]
    print(HISTORY)
    print(len(HISTORY))


def trim_to_col(line, terminal_col):
    choice_width = 4+1        # [x]_ before every line plus 1 space at end
    if len(line)+choice_width < terminal_col:
        return line
    shortened = "..."+line[-(terminal_col-choice_width-3):]
    return shortened


def print_history_menu(maxlines, terminal_col):
    if maxlines <= 0:
        print("Error: No files in history yet or your terminal has too few lines.")
        mode_help()

    for i in range(0, maxlines):      # print numbered menu
        print("["+KEYS[i]+"] "+trim_to_col(HISTORY[i], terminal_col))


# Do actual work of calling TAILCMD
def tail_file(logfile):
    add_to_history(logfile)
    save_history_to_file()
    os.system(TAILCMD+" "+logfile)


######################################### WORK MODES #############################
def mode_help():
    print("tailor "+VERSION+" (C) by <NAME> (DerWOK)")
    print("Usage: tailor [file]")<commit_msg>New: sudo if not enough access rights<commit_after>#!/usr/bin/env python3

# (C) 2016-04-08 <NAME> (DerWOK)

import os
import sys
import os.path
from os.path import expanduser
import tty
import termios
import shutil
import signal
import time

VERSION = "0.1"
CONFIGFILE = expanduser("~")+"/.tailor"
TAILCMD = "tail -F"
KEYS = "<KEY>"
HISTORYMAX = len(KEYS)
HISTORY = []


if os.name == "nt":
    print("Sorry, no Win support.")
    exit(1)


# Read a single key from keyboard
def _getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


def read_history_from_file():
    global HISTORY
    if not os.path.isfile(CONFIGFILE):
        return
    with open(CONFIGFILE) as infile:
        HISTORY = infile.readlines()
    for i in range(0, len(HISTORY)):
        HISTORY[i] = HISTORY[i].rstrip('\n')


def save_history_to_file():
    outfile = open(CONFIGFILE, 'w')
    outfile.write("\n".join(HISTORY))
    outfile.close()


def add_to_history(logfile):
    global HISTORY
    for i in range(len(HISTORY)-1, -1, -1):   # remove old duplicates
        if HISTORY[i] == logfile:
            del HISTORY[i]
            print("DEL:"+str(i+1))
    HISTORY.insert(0, logfile)
    if len(HISTORY) > HISTORYMAX:           # shorten list
        HISTORY = HISTORY[0:HISTORYMAX]
    print(HISTORY)
    print(len(HISTORY))


def trim_to_col(line, terminal_col):
    choice_width = 4+1        # [x]_ before every line plus 1 space at end
    if len(line)+choice_width < terminal_col:
        return line
    shortened = "..."+line[-(terminal_col-choice_width-3):]
    return shortened


def print_history_menu(maxlines, terminal_col):
    if maxlines <= 0:
        print("Error: No files in history yet or your terminal has too few lines.")
        mode_help()

    for i in range(0, maxlines):      # print numbered menu
        print("["+KEYS[i]+"] "+trim_to_col(HISTORY[i], terminal_col))


# Do actual work of calling TAILCMD
def tail_file(logfile):
    add_to_history(logfile)
    save_history_to_file()
    if os.access(logfile, os.R_OK):
        os.system(TAILCMD+" "+logfile)
    else:
        os.system("sudo "+TAILCMD+" "+logfile)



######################################### WORK MODES #############################
def mode_help():
    print("tailor "+VERSION+" (C) by <NAME> (DerWOK)")
    print("Usage: tailor [file]")
{'fraction_non_alphanumeric': 0.15577476366625564, 'fraction_numerical': 0.010686395396629675, 'mean_word_length': 3.1598290598290597, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2160229', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1697, 'n_words': 422}