Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 0fcdd92

Browse files
author
js-d-coder
committed
fix bug which outputs unnecessary newline at the end
1 parent 7ae8bed commit 0fcdd92

File tree

2 files changed

+93
-47
lines changed

2 files changed

+93
-47
lines changed

‎README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# lsi
2-
ls command alternative. Output is either cleanly formatted table or string of null terminated file names parseable by xargs command. Default behaviour is to show only non-hidden files and directories of directory passed or current directory if no directory is given, in table format.
2+
ls command alternative (not a replacement). Output is either cleanly formatted table or string of null terminated file names parseable by xargs command. Default behaviour is to show only non-hidden files and directories of directory passed or current directory if no directory is given, in table format.
33
Written in Python version 3 for UNIX like OS
4-
Version 1.1.1
4+
Version 1.1.2
55

66
## Usage
77

@@ -65,3 +65,31 @@ Version 1.1.1
6565

6666
# move all hidden files to hidden-files directory
6767
$ lsi -ifx | xargs -r0 mv -t hidden-files
68+
69+
70+
## Requirement
71+
72+
lsi requires Python version 3 installed before you run it
73+
74+
# apt install python3 # Ubuntu, Linux Mint, Debian
75+
# pacman -S python # Arch Linux
76+
# yum install python3 # Fedora
77+
# pkg install python3 # \*BSD
78+
79+
## Installation
80+
81+
(For Ubuntu, Fedora, Linux Mint, Debian, Arch Linux and other Linux distributions including \*BSD and other UNIX like OS)
82+
83+
$ git clone https://github.com/js-d-coder/lsi-python3.git
84+
$ cd lsi-python3
85+
$ cp lsi /usr/local/bin/
86+
# chmod +x /usr/local/bin/lsi # requires root permission
87+
88+
## Screenshot (lsi vs ls)
89+
90+
![lsi output](https://imgur.com/a/O4BHc)
91+
92+
## License
93+
94+
[MIT](https://mit-license.org/)
95+
Copyright (c) 2017 js-d-coder (www.github.com/js-d-coder)

‎lsi

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import argparse
1515
import os
1616
import sys
1717

18-
# get size of the terminal, useful if output is in table format
18+
# get size of the terminal for output in table format
1919
terminalRowSize, terminalColumnSize = os.popen('stty size', 'r').read().split()
2020

2121
parser = argparse.ArgumentParser(description=
@@ -71,7 +71,7 @@ def version():
7171
"""Print version, license and exit"""
7272
if args.version:
7373
print(
74-
"""lsi version 1.1.1
74+
"""lsi version 1.1.2
7575
7676
Copyright (c) 2017 js-d-coder (www.github.com/js-d-coder)
7777
@@ -120,6 +120,7 @@ def createFileList(dirname):
120120
else:
121121
return files + dirs
122122

123+
123124
def xargsInput():
124125
"""create string of null terminated file names for processing by xargs command"""
125126
outputString = ""
@@ -132,33 +133,65 @@ def xargsInput():
132133
return outputString
133134

134135

136+
def columnOutput(printFunc):
137+
"""function when requested output is either table or one-file-per-line.
138+
Note the use of printFunc; it substitue prettyPrint function for tabular output,
139+
or oneFilePerLine function to output one file per line."""
140+
global didErrorOccur
141+
moreThanOneArg = 0
142+
for file in args.files:
143+
"""keep processing next file in the files list even if error occur.
144+
if error occur print the error and process next file in the files list.
145+
didErrorOccur is used as a flag whether error occur or not"""
146+
try:
147+
if not os.path.exists(file):
148+
raise FileNotFoundError("{}: cannot access '{}': no such file or directory".format(os.path.basename(sys.argv[0]), file))
149+
elif not os.path.isdir(file):
150+
if moreThanOneArg: print()
151+
print(file)
152+
else:
153+
if moreThanOneArg: print()
154+
if len(args.files) > 1:
155+
print(file, ":", sep="")
156+
printFunc(createFileList(file))
157+
except FileNotFoundError as err:
158+
if moreThanOneArg: print()
159+
print(err)
160+
didErrorOccur = 1
161+
except PermissionError:
162+
if moreThanOneArg: print()
163+
print("{}: cannot open directory '{}': Permission denied".format(os.path.basename(sys.argv[0]), file))
164+
didErrorOccur = 1
165+
finally:
166+
moreThanOneArg = 1
167+
168+
169+
def oneFilePerLine(list):
170+
for l in list:
171+
print(l)
172+
135173
def prettyPrint(list):
136174
"""print files in table format"""
137-
if args.one:
175+
maxlen = 0
176+
for f in list:
177+
if len(f) > maxlen:
178+
maxlen = len(f)
179+
# 2 for enclosing single qoutes, 2 for spaces after filename
180+
colWidth = maxlen + 4
181+
noOfCol = int(int(terminalColumnSize)/colWidth)
182+
if noOfCol == 0 or noOfCol == 1:
138183
for f in list:
139-
print('{!r}'.format(f))
140-
print()
184+
print("{!r}".format(f))
141185
else:
142-
maxlen=0
186+
nthCol=0
143187
for f in list:
144-
if len(f) > maxlen:
145-
maxlen = len(f)
146-
# 2 for enclosing single qoutes, 2 for spaces after filename
147-
colWidth = maxlen + 4
148-
noOfCol = int(int(terminalColumnSize)/colWidth)
149-
n = 1
150-
if noOfCol == 0:
151-
for f in list:
152-
print("{!r}".format(f))
153-
else:
154-
for f in list:
155-
print('{!r}'.format(f).ljust(colWidth), end="")
156-
if n == noOfCol:
157-
n = 0
158-
print("")
159-
n += 1
160-
print("\n")
161-
188+
nthCol += 1
189+
print('{!r}'.format(f).ljust(colWidth), end="")
190+
if nthCol == noOfCol:
191+
print()
192+
nthCol=0
193+
if nthCol != noOfCol:
194+
print()
162195
"""function declarations ENDS here"""
163196

164197

@@ -179,28 +212,13 @@ def main():
179212
except PermissionError as err:
180213
print(err)
181214
sys.exit(2)
215+
elif args.one:
216+
"""output one file per line"""
217+
columnOutput(oneFilePerLine)
218+
pass
182219
else:
183-
"""if -x or --xargs is not given, output file names in table format"""
184-
185-
for file in args.files:
186-
"""keep processing next file in the files list even if error occur.
187-
if error occur print the error and process next file in the files list.
188-
didErrorOccur is used as a flag whether error occur or not"""
189-
try:
190-
if not os.path.exists(file):
191-
raise FileNotFoundError("{}: cannot access '{}': no such file or directory\n".format(os.path.basename(sys.argv[0]), file))
192-
elif not os.path.isdir(file):
193-
print(file, end="\n\n")
194-
else:
195-
if len(args.files) > 1:
196-
print(file, ":", sep="")
197-
prettyPrint(createFileList(file))
198-
except FileNotFoundError as err:
199-
print(err)
200-
didErrorOccur = 1
201-
except PermissionError:
202-
print("{}: cannot open directory '{}': Permission denied\n".format(os.path.basename(sys.argv[0]), file))
203-
didErrorOccur = 1
220+
"""output file names in table format"""
221+
columnOutput(prettyPrint)
204222

205223

206224
if __name__ == "__main__":

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /