Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced tput commands with escape codes + python API call #18

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ok-show.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

from __future__ import print_function
import argparse, codecs, os, re, sys
import argparse, codecs, os, re, shutil, sys

def ansi_len(s):
no_ansi_s = rx.ansi_len.sub('', s)
Expand Down Expand Up @@ -130,12 +130,12 @@ def print_line(l, clr, nr_positions_line_nr, format_line):
def main():
# customizations
clr = ok_color()

terminal_size = shutil.get_terminal_size()
# handle arguments
parser = argparse.ArgumentParser(description='Show the ok-file colorized (or just one line).')
parser.add_argument('--verbose', '-v', metavar='V', type=int, default=1, help='0=quiet, 1=normal, 2=verbose. Defaults to 1. ')
parser.add_argument('--comment_align', '-c', metavar='CA', type=int, default=2, choices= [0,1,2,3], help='Level ($e) of comment alignment. 0=no alignment, 1=align consecutive lines (Default), 2=including whitespace, 3 align all.')
parser.add_argument('--terminal_width', '-t', metavar='TW', type=int, default=230, help='number of columns of the terminal (tput cols)')
parser.add_argument('--terminal_width', '-t', metavar='TW', type=int, default=terminal_size.columns, help='number of columns of the terminal (tput cols)')
parser.add_argument('only_line_nr', metavar='N', type=int, nargs='?', help='the line number to show')
args = parser.parse_args()

Expand Down
8 changes: 4 additions & 4 deletions ok.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ environment variables (for internal use):
shift
# get the line to be executed
local line_text
line_text="$(cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$(tput cols)" "$line_nr")"
line_text="$(cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" "$line_nr")"
local res=$?
if [[ $res -ne 0 ]]; then
#because stdout/stderr are swapped by ok-show.py in this case, handle this too
Expand All @@ -86,7 +86,7 @@ environment variables (for internal use):
function _ok_cmd_list {
unset -f _ok_cmd_list

cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" -t "$(tput cols)" || return $?
cat "$ok_file" | "$_OK__PATH_TO_PYTHON" "${_OK__PATH_TO_ME}/ok-show.py" -v "$verbose" -c "$comment_align" || return $?
}

# export variables because python is a sub-process, and variables might have changed since initialization
Expand All @@ -97,8 +97,8 @@ environment variables (for internal use):
local -r version="0.8.0"
# used for colored output (see: https://stackoverflow.com/a/20983251/56)
# notice: this is partly a duplication from code in ok-show.py
local -r c_nc=$(tput sgr0)
if [ -z ${_OK_C_NUMBER+x} ]; then local c_number=$(tput setaf 6); else local c_number=$_OK_C_NUMBER; fi #NUMBER defaults to CYAN
local -r c_nc='\[\e[0m\]'
if [ -z ${_OK_C_NUMBER+x} ]; then local c_number='\[\e[0;36m\]'; else local c_number=$_OK_C_NUMBER; fi #NUMBER defaults to CYAN
if [ -z ${_OK_C_PROMPT+x} ]; then local c_prompt=$c_number; else local c_prompt=$_OK_C_PROMPT; fi #PROMPT defaults to same color as NUMBER
# other customizations (some environment variables can be overridden by arguments)
if [ -z ${_OK_PROMPT+x} ]; then local prompt="$ "; else local prompt=$_OK_PROMPT; fi
Expand Down