├── .gitignore ├── LICENSE ├── README.md ├── bin └── .gitignore ├── lq.nimble ├── src ├── config.nim ├── listprocs.nim ├── listutils.nim ├── lq.nim └── utils.nim └── tast.txt /.gitignore: -------------------------------------------------------------------------------- 1 | lq -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | (Tested only on Linux) 2 | 3 | This is a directory/file listing tool. 4 | 5 | Similar to what ls and tree do, except it doesn't have all of their features. 6 | 7 | It has different modes to list the contents of a directory. 8 | 9 | It's in the [AUR](https://aur.archlinux.org/packages/lq-git). 10 | 11 | ![](https://i.imgur.com/j2C1s4z.jpg) -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore -------------------------------------------------------------------------------- /lq.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "5.1.0" 4 | author = "madprops" 5 | description = "Directory listing tool" 6 | license = "GPL-2.0" 7 | srcDir = "src" 8 | bin = @["lq"] 9 | skipExt = @["nim"] 10 | 11 | 12 | # Dependencies 13 | 14 | requires "nim >= 1.0.0" 15 | requires "nap >= 3.0.0" -------------------------------------------------------------------------------- /src/config.nim: -------------------------------------------------------------------------------- 1 | import std/[os, strutils, strformat, posix, tables] 2 | import nap 3 | 4 | let version = "5.1.0" 5 | 6 | type Config* = ref object 7 | path*: string 8 | just_dirs*: bool 9 | just_files*: bool 10 | just_execs*: bool 11 | absolute*: bool 12 | filter*: string 13 | list*: bool 14 | prefix*: bool 15 | dircount*: bool 16 | mix*: bool 17 | size*: bool 18 | date*: bool 19 | dirdate*: bool 20 | dirsize*: bool 21 | sizesort*: bool 22 | sizesort2*: bool 23 | dirsizesort*: bool 24 | datesort*: bool 25 | datesort2*: bool 26 | dirdatesort*: bool 27 | permissions*: bool 28 | tree*: bool 29 | exclude*: seq[string] 30 | max_width*: int 31 | output*: string 32 | reverse*: bool 33 | snippets*: bool 34 | snippets_length*: int 35 | mix_files*: bool 36 | no_format*: bool 37 | no_hidden*: bool 38 | 39 | # Set automatically 40 | piped*: bool 41 | 42 | # Table to map colors 43 | colors*: Table[string, seq[string]] 44 | 45 | var 46 | oconf*: Config 47 | first_print* = false 48 | 49 | proc fix_path(path:string): string 50 | 51 | proc get_config*() = 52 | let 53 | path = add_arg(name="path", kind="argument", value="", help="Path to a directory") 54 | just_dirs = add_arg(name="dirs", kind="flag", help="Just show directories", alt="1") 55 | just_files = add_arg(name="files", kind="flag", help="Just show files", alt="2") 56 | just_execs = add_arg(name="execs", kind="flag", help="Just show executables", alt="3") 57 | absolute = add_arg(name="absolute", kind="flag", help="Use absolute paths", alt="a") 58 | filter = add_arg(name="filter", kind="value", help="Filter the list.\nStart with re: to use regex.\nFor instance --filter=re:\\\\d+", alt="f") 59 | prefix = add_arg(name="prefix", kind="flag", help="Use prefixes like '[F]'", alt="p") 60 | list = add_arg(name="list", kind="flag", help="Show in a vertical list", alt="l") 61 | dircount = add_arg(name="count", kind="flag", help="Count items inside directories", alt="c") 62 | mix = add_arg(name="mix", kind="flag", help="Mix and sort everything", alt="m") 63 | size = add_arg(name="size", kind="flag", help="Show the size of files", alt="z") 64 | dirsize = add_arg(name="dirsize", kind="flag", help="Show the size of directories", alt="Z") 65 | date = add_arg(name="date", kind="flag", help="Show the last modification date on files", alt="k") 66 | dirdate = add_arg(name="dirdate", kind="flag", help="Show the last modification date on directories", alt="K") 67 | sizesort = add_arg(name="sizesort", kind="flag", help="Sort files by size. Repeat, like '-ii', to semi-sort directories too", alt="i") 68 | dirsizesort = add_arg(name="dirsizesort", kind="flag", help="Sort directories by size", alt="I") 69 | datesort = add_arg(name="datesort", kind="flag", help="Sort files by modification date. Repeat, like '-dd', to semi-sort directories too", alt="d") 70 | dirdatesort = add_arg(name="dirdatesort", kind="flag", help="Sort directories by modification date", alt="D") 71 | permissions = add_arg(name="permissions", kind="flag", help="Show posix permissions", alt="P") 72 | tree = add_arg(name="tree", kind="flag", help="Show directories in a tree structure", alt="t") 73 | exclude = add_arg(name="exclude", kind="value", multiple=true, help="Directories to exclude", alt="e") 74 | max_width = add_arg(name="max-width", kind="value", value="0", help="Maximum horizontal size", alt="w") 75 | output = add_arg(name="output", kind="value", help="Path to a file to save the output", alt="o") 76 | reverse = add_arg(name="reverse-sort", kind="flag", help="Reverse sorting", alt="r") 77 | snippets = add_arg(name="snippets", kind="flag", help="Show text file snippets", alt="s") 78 | snippets_length = add_arg(name="snippets-length", kind="value", value="0", help="Max length of snippets", alt="n") 79 | mix_files = add_arg(name="mix-files", kind="flag", help="Mix files and executables", alt="M") 80 | no_format = add_arg(name="no-format", kind="flag", help="Don't color the text", alt="F") 81 | no_hidden = add_arg(name="no-hidden", kind="flag", help="Don't show hidden files", alt="h") 82 | 83 | # Presets 84 | info = add_arg(name="info", kind="flag", help="Preset to show some information", alt="?") 85 | allsizesort = add_arg(name="allsizesort", kind="flag", help="Sort files and directories by size", alt="9") 86 | alldatesort = add_arg(name="alldatesort", kind="flag", help="Sort files and directories by date", alt="0") 87 | 88 | add_header("List directories") 89 | add_header(&"Version: {version}") 90 | add_note("Git Repo: https://github.com/madprops/lq") 91 | 92 | parse_args() 93 | 94 | var st: posix.Stat 95 | discard posix.fstat(0, st) 96 | 97 | oconf = Config( 98 | piped: st.st_mode.S_ISFIFO(), 99 | path: path.value, 100 | just_dirs: just_dirs.used, 101 | just_files: just_files.used, 102 | just_execs: just_execs.used, 103 | absolute: absolute.used, 104 | filter: filter.value, 105 | list: list.used, 106 | prefix: prefix.used, 107 | dircount: dircount.used, 108 | mix: mix.used, 109 | size: size.used, 110 | dirsize: dirsize.used, 111 | date: date.used, 112 | dirdate: dirdate.used, 113 | sizesort: sizesort.used, 114 | dirsizesort: dirsizesort.used, 115 | datesort: datesort.used, 116 | dirdatesort: dirdatesort.used, 117 | permissions: permissions.used, 118 | tree: tree.used, 119 | exclude: exclude.values, 120 | max_width: max_width.getInt(), 121 | output: output.value, 122 | reverse: reverse.used, 123 | snippets: snippets.used, 124 | snippets_length: snippets_length.getInt(), 125 | mix_files: mix_files.used, 126 | no_format: no_format.used, 127 | no_hidden: no_hidden.used, 128 | ) 129 | 130 | # Path 131 | oconf.path = fix_path(oconf.path) 132 | 133 | if info.used: 134 | oconf.size = true 135 | oconf.dirsize = true 136 | oconf.date = true 137 | oconf.dirdate = true 138 | 139 | if allsizesort.used: 140 | oconf.sizesort = true 141 | oconf.dirsizesort = true 142 | 143 | if alldatesort.used: 144 | oconf.datesort = true 145 | oconf.dirdatesort = true 146 | 147 | oconf.sizesort2 = sizesort.used and sizesort.count >= 2 148 | oconf.datesort2 = datesort.used and datesort.count >= 2 149 | 150 | # Table of colors to use 151 | oconf.colors = initTable[string, seq[string]]() 152 | oconf.colors["titles"] = @["green", "bright"] 153 | oconf.colors["dirs"] = @["blue"] 154 | oconf.colors["dirlinks"] = @["blue", "underscore"] 155 | oconf.colors["files"] = @[""] 156 | oconf.colors["filelinks"] = @["underscore"] 157 | oconf.colors["exefiles"] = @["bright"] 158 | oconf.colors["exefilelinks"] = @["bright", "underscore"] 159 | oconf.colors["labels"] = @[""] 160 | oconf.colors["filtermatch"] = @[""] 161 | oconf.colors["pipes"] = @["cyan", "dim"] 162 | oconf.colors["details"] = @["cyan", "dim"] 163 | oconf.colors["snippets"] = @["green"] 164 | 165 | # Default directories to exclude 166 | oconf.exclude.add(".git") 167 | oconf.exclude.add(".svn") 168 | oconf.exclude.add(".mypy_cache") 169 | oconf.exclude.add("node_modules") 170 | 171 | proc conf*(): Config = 172 | return oconf 173 | 174 | proc fix_path(path:string): string = 175 | var path = expandTilde(path) 176 | if not path.startsWith("/"): 177 | path = getCurrentDir().joinPath(path) 178 | normalizePath(path) 179 | return path -------------------------------------------------------------------------------- /src/listprocs.nim: -------------------------------------------------------------------------------- 1 | import std/[os, nre, strformat, strutils, algorithm, terminal, times, tables] 2 | import config 3 | import listutils 4 | import utils 5 | 6 | var 7 | og_path* = "" 8 | aotfilter* = false 9 | filts*: Table[string, bool] 10 | title_printed_len* = 0 11 | listed = false 12 | first_line = false 13 | 14 | proc list_dir*(path:string, level=0) 15 | 16 | var 17 | space_level = 2 18 | space = "" 19 | 20 | for x in 0.. 0 and conf().max_width <= termwidth: 45 | (conf().max_width - 4) else: (termwidth - 4) 46 | 47 | var 48 | current_file = QFile() 49 | current_index = 0 50 | 51 | proc space_item(s:string): string = 52 | return &"{s}{space}" 53 | 54 | proc check_last(): bool = 55 | last and cfiles == files.len 56 | 57 | proc print_line() = 58 | let line = sline.strip(leading=false, trailing=true) 59 | log(line, check_last()) 60 | first_line = true 61 | sline = get_defsline() 62 | slen = 0 63 | if has_snippet(current_file): 64 | let lvl = if conf().tree: level + 1 else: level 65 | show_snippet(path.joinPath(current_file.path), current_file.size, lvl) 66 | 67 | proc add_to_line(s:string, clen:int) = 68 | sline.add(space_item(s)) 69 | slen += clen + space_level 70 | inc(cfiles) 71 | 72 | if listed: print_line() 73 | 74 | for i, file in files: 75 | current_index = i 76 | current_file = file 77 | 78 | let 79 | fmt = format_item(file, path, level, i, files.len, last) 80 | s = fmt[0] 81 | clen = fmt[1] 82 | 83 | if not listed: 84 | let tots = slen + clen 85 | # Add to line 86 | if tots <= limit: 87 | add_to_line(s, clen) 88 | if tots == limit: 89 | print_line() 90 | # Line overflow 91 | elif tots > limit: 92 | print_line() 93 | add_to_line(s, clen) 94 | # List item 95 | else: 96 | add_to_line(s, clen) 97 | if conf().tree: 98 | if file.kind == pcDir: 99 | list_dir(path.joinPath(file.path), level + 1) 100 | 101 | if slen > 0: 102 | print_line() 103 | 104 | proc list_dir*(path:string, level=0) = 105 | var path = path 106 | if level == 0: og_path = path 107 | 108 | var 109 | dirs: seq[QFile] 110 | files: seq[QFile] 111 | execs: seq[QFile] 112 | 113 | let 114 | do_filter = conf().filter != "" 115 | do_regex_filter = conf().filter.startsWith("re:") 116 | 117 | var 118 | filter = "" 119 | res: Regex 120 | 121 | if do_regex_filter: 122 | res = re(conf().filter.replace(re"^re\:", "")) 123 | else: filter = conf().filter 124 | let do_calculate_dirs = conf().dirdatesort or conf().dirsize or 125 | conf().dirsizesort or conf().dirdate 126 | let info = get_info(path, level == 0) 127 | 128 | proc check_exclude(short_path:string): bool = 129 | for e in conf().exclude: 130 | if short_path.contains(&"{e}{os.DirSep}"): 131 | return true 132 | return false 133 | 134 | proc add_filt(short_path:string) = 135 | var sp = short_path 136 | filts[sp] = true 137 | while sp != "": 138 | sp = sp.splitPath[0] 139 | filts[sp] = true 140 | 141 | proc process_file(fpath:string): bool = 142 | let 143 | full_path = path.joinPath(fpath) 144 | short_path = full_path.replace(&"{og_path}{os.DirSep}", "") 145 | info = get_info(full_path) 146 | 147 | if conf().no_hidden and short_path 148 | .extractFileName().startsWith("."): 149 | return false 150 | 151 | if not aotfilter: 152 | if check_exclude(short_path): 153 | show_label("(Excluded)", level) 154 | return true 155 | 156 | # Filter 157 | if aotfilter: 158 | if not filts.hasKey(short_path): 159 | return false 160 | else: 161 | if do_filter: 162 | if do_regex_filter: 163 | let m = fpath.find(res) 164 | if m.isNone: return false 165 | else: 166 | if not fpath.contains(filter): 167 | return false 168 | 169 | # Add to proper list 170 | case info.kind 171 | 172 | # If directory 173 | of pcDir, pcLinkToDir: 174 | if conf().just_files or conf().just_execs: return false 175 | 176 | var 177 | size: int64 = 0 178 | date: int64 = 0 179 | perms = "" 180 | 181 | if do_calculate_dirs: 182 | let calc = calculate_dir(full_path) 183 | size = calc.size 184 | date = calc.date 185 | if conf().permissions: 186 | perms = posix_perms(info) 187 | if conf().datesort2: 188 | date = info.lastWriteTime.toUnix() 189 | if conf().sizesort2: 190 | size = info.size 191 | let qf = QFile(kind:info.kind, path:fpath, size:size, date:date, perms:perms, exe:false) 192 | dirs.add(qf) 193 | 194 | # If file 195 | of pcFile, pcLinkToFile: 196 | var 197 | size = info.size 198 | date: int64 = 0 199 | perms = "" 200 | 201 | if conf().size or conf().sizesort or conf().datesort or conf().permissions or conf().date: 202 | if conf().datesort or conf().date: 203 | date = info.lastWriteTime.toUnix() 204 | if conf().permissions: 205 | perms = posix_perms(info) 206 | 207 | let 208 | exe = info.permissions.contains(fpUserExec) 209 | qf = QFile(kind:info.kind, path:fpath, size:size, date:date, perms:perms, exe:exe) 210 | 211 | if conf().just_dirs: return false 212 | if conf().just_files and exe: return false 213 | if conf().just_execs and not exe: return false 214 | 215 | if exe: 216 | if conf().mix_files: files.add(qf) 217 | else: execs.add(qf) 218 | else: files.add(qf) 219 | 220 | return false 221 | 222 | proc sort_list(list: var seq[QFile]) = 223 | if list.len == 0: return 224 | let kind = list[0].kind 225 | 226 | if conf().sizesort and 227 | (kind == pcFile or kind == pcLinkToFile) or 228 | ((kind == pcDir or kind == pcLinkToDir) and (conf().dirsizesort or conf().sizesort2)): 229 | list = list.sortedByIt(it.size) 230 | if not conf().reverse: 231 | list.reverse() 232 | 233 | elif conf().datesort and 234 | (kind == pcFile or kind == pcLinkToFile) or 235 | ((kind == pcDir or kind == pcLinkToDir) and (conf().dirdatesort or conf().datesort2)): 236 | list = list.sortedByIt(it.date) 237 | if not conf().reverse: 238 | list.reverse() 239 | 240 | else: 241 | list = list.sortedByIt(it.path.toLowerAscii) 242 | if conf().reverse: 243 | list.reverse() 244 | 245 | proc sort_lists() = 246 | sort_list(dirs) 247 | sort_list(files) 248 | sort_list(execs) 249 | 250 | proc do_all(last=false) = 251 | if not conf().mix: sort_lists() 252 | var all = dirs & files & execs 253 | if conf().mix: 254 | sort_list(all) 255 | show_files(all, path, level, last) 256 | else: 257 | show_files(all, path, level, last) 258 | 259 | proc total_files(): int = 260 | dirs.len + files.len + execs.len 261 | 262 | # # # - M A I N - # # # 263 | 264 | listed = conf().list or conf().tree or conf().snippets 265 | 266 | # Check files ahead of time if filtering a tree 267 | if do_filter and level == 0 and conf().tree: 268 | aotfilter = true 269 | 270 | for full_path in walkDirRec(path, yieldFilter={pcFile, pcLinkToFile, pcDir, pcLinkToDir}): 271 | let short_path = full_path.replace(&"{og_path}/", "") 272 | 273 | if conf().no_hidden and short_path 274 | .extractFileName().startsWith("."): 275 | continue 276 | 277 | # Exclude 278 | if check_exclude(short_path): 279 | continue 280 | 281 | # Add to filts on matches 282 | if do_regex_filter: 283 | let m = short_path.find(res) 284 | if m.isSome: 285 | add_filt(short_path) 286 | else: 287 | if short_path.contains(filter): 288 | add_filt(short_path) 289 | 290 | if filts.len == 0: 291 | quit(0) 292 | 293 | if info.kind == pcFile or 294 | info.kind == pcLinkToFile: 295 | conf().prefix = true 296 | conf().permissions = true 297 | conf().size = true 298 | conf().date = true 299 | let split = path.splitPath() 300 | og_path = split[0] 301 | path = split[0] 302 | discard process_file(split[1]) 303 | 304 | else: # If it's a directory check every file in it 305 | block filesblock: 306 | for file in walkDir(path, relative=true): 307 | if process_file(file.path): break filesblock 308 | 309 | if level == 0 and total_files() == 0: 310 | quit(0) 311 | 312 | # Only tree mode gets spaced 313 | 314 | do_all(true) -------------------------------------------------------------------------------- /src/listutils.nim: -------------------------------------------------------------------------------- 1 | import std/[os, strformat, strutils, times, tables, sugar, sequtils] 2 | import config 3 | import utils 4 | 5 | type QFile* = object 6 | kind*: PathComponent 7 | path*: string 8 | size*: int64 9 | date*: int64 10 | perms*: string 11 | exe*: bool 12 | 13 | var 14 | # Used to create spaces on levels 15 | levspace* = " " 16 | levspace_2* = "\\s\\s\\s\\s" 17 | levlines* = initTable[int, bool]() 18 | rightnow* = getTime().toUnix() 19 | 20 | # This stores file info to be recycled 21 | # instead of it getting fetched again 22 | info_cache = initTable[string, FileInfo]() 23 | 24 | proc get_info*(path:string, canfail=false): FileInfo = 25 | try: 26 | if info_cache.hasKey(path): 27 | return info_cache[path] 28 | else: 29 | let info = getFileInfo(path) 30 | info_cache[path] = info 31 | return info 32 | except: 33 | if canfail: 34 | log(&"Error: Can't read '{path}'") 35 | quit(0) 36 | else: 37 | return FileInfo() 38 | 39 | proc posix_perms*(info:FileInfo): string = 40 | result.add([pcFile: '-', 'l', pcDir: 'd', 'l'][info.kind]) 41 | for i, fp in [fpUserRead, fpUserWrite, fpUserExec, fpGroupRead, fpGroupWrite, 42 | fpGroupExec, fpOthersRead, fpOthersWrite, fpOthersExec]: 43 | result.add(if fp in info.permissions: "rwx"[i mod 3] else: '-') 44 | 45 | proc calculate_dir*(path:string): QFile = 46 | let info = get_info(path) 47 | 48 | var 49 | size: int64 = 0 50 | date: int64 = 0 51 | path = fix_path_2(path) 52 | 53 | for file in walkDirRec(&"{path}"): 54 | let info2 = get_info(file) 55 | size += info2.size 56 | let d = info2.lastWriteTime.toUnix() 57 | if d > date: 58 | date = d 59 | 60 | date = max(date, info.lastWriteTime.toUnix()) 61 | QFile(size:size, date:date) 62 | 63 | proc format_perms*(perms:string): string = 64 | &" ({perms})" 65 | 66 | proc format_size*(size: int64): string = 67 | if size == 0: return " (Empty)" 68 | 69 | let 70 | fsize = float(size) 71 | divider: float64 = 1024.0 72 | kb: float64 = fsize / divider 73 | mb: float64 = kb / divider 74 | gb: float64 = mb / divider 75 | size = if gb >= 1.0: &"{gb.formatFloat(ffDecimal, 1)} GB" 76 | elif mb >= 1.0: &"{mb.formatFloat(ffDecimal, 1)} MB" 77 | elif kb >= 1.0: &"{int(kb)} KB" 78 | else: &"{int(fsize)} B" 79 | 80 | return &" ({size})" 81 | 82 | proc format_date*(date:int64): string = 83 | let days = int( float( rightnow - date ) / 3600.0 / 24.0 ) 84 | if days == 0: 85 | return " (Today)" 86 | if days < 365: 87 | if days == 1: 88 | return &" ({days} day)" 89 | else: 90 | return &" ({days} days)" 91 | else: 92 | let years = int( float(days) / 365.0 ) 93 | if years == 1: 94 | return &" ({years} year)" 95 | else: 96 | return &" ({years} years)" 97 | 98 | proc get_kind_color*(kind:PathComponent): string = 99 | case kind 100 | of pcDir: 101 | get_ansi(conf().colors["dirs"]) 102 | of pcLinkToDir: 103 | get_ansi(conf().colors["dirlinks"]) 104 | of pcFile: 105 | get_ansi(conf().colors["files"]) 106 | of pcLinkToFile: 107 | get_ansi(conf().colors["filelinks"]) 108 | 109 | proc get_prefix*(file:QFile): string = 110 | case file.kind 111 | of pcDir: "[D] " 112 | of pcLinkToDir: "[d] " 113 | of pcFile: 114 | if file.exe: "[E] " else: "[F] " 115 | of pcLinkToFile: 116 | if file.exe: "[e] " else: "[f] " 117 | 118 | proc get_level_space*(level:int): string = 119 | var levs = "" 120 | for x in 0.. 0: 165 | levs = get_ansi(conf().colors["pipes"]) 166 | 167 | for lvl in 1.. x.len > 0).len > 0: 208 | let i = path2.find(conf().filter) 209 | if i != -1: 210 | let cm = get_ansi(conf().colors["filtermatch"]) 211 | path2 = &"{path2.substr(0, i - 1)}{cm}{path2.substr(i, i + conf().filter.len - 1)}" & 212 | &"{reset()}{path2.substr(i + conf().filter.len, path2.len - 1)}" 213 | 214 | let s = &"{levs}{c1}{c2}{prefix}{reset()}{c1}{path2}{reset()}{c2}{size}{date}{perms}{scount}{reset()}" 215 | return (s, clen) 216 | 217 | proc show_label*(msg:string, level:int, is_snippet=false) = 218 | let msg2 = if is_snippet: &" {msg}" else: msg 219 | log format_item(label=msg2, level=level, is_snippet=is_snippet)[0] 220 | 221 | proc has_snippet*(file:QFile): bool = 222 | conf().snippets and (file.kind == pcFile or file.kind == pcLinkToFile) 223 | 224 | proc show_snippet*(full_path:string, size:int64, level:int) = 225 | try: 226 | var len = conf().snippets_length 227 | if len == 0: len = 300 228 | let blen = min(size, max(512, len)) 229 | if blen == 0: return 230 | let f = open(full_path) 231 | 232 | var bytes: seq[uint8] 233 | for x in 0.. line.strip() != "") 248 | 249 | # Print each line 250 | for line in lines: 251 | show_label(line, level, true) 252 | except: 253 | discard -------------------------------------------------------------------------------- /src/lq.nim: -------------------------------------------------------------------------------- 1 | import utils 2 | import config 3 | import listprocs 4 | 5 | when isMainModule: 6 | get_config() 7 | toke() 8 | list_dir(conf().path) 9 | toke() 10 | 11 | # Output to file if 12 | # output is enabled 13 | if conf().output != "": 14 | writeFile(conf().output, all_output) -------------------------------------------------------------------------------- /src/utils.nim: -------------------------------------------------------------------------------- 1 | import std/[os, terminal, strutils, strformat] 2 | import config 3 | 4 | type AnsiKind* = enum 5 | ansi_green 6 | ansi_cyan 7 | ansi_red 8 | ansi_blue 9 | ansi_magenta 10 | ansi_yellow 11 | ansi_white 12 | ansi_black 13 | ansi_bright 14 | ansi_dim 15 | ansi_italic 16 | ansi_underscore 17 | ansi_blink 18 | ansi_blinkrapid 19 | ansi_reverse 20 | ansi_hidden 21 | ansi_strikethrough 22 | ansi_reset 23 | 24 | var 25 | all_output* = "" 26 | 27 | proc get_ansi*(kind:string): string = 28 | if conf().piped or conf().no_format: return "" 29 | 30 | case kind 31 | of "green": ansiForegroundColorCode(fgGreen) 32 | of "cyan": ansiForegroundColorCode(fgCyan) 33 | of "red": ansiForegroundColorCode(fgRed) 34 | of "blue": ansiForegroundColorCode(fgBlue) 35 | of "magenta": ansiForegroundColorCode(fgMagenta) 36 | of "yellow": ansiForegroundColorCode(fgYellow) 37 | of "white": ansiForegroundColorCode(fgWhite) 38 | of "black": ansiForegroundColorCode(fgBlack) 39 | of "bright": ansiStyleCode(styleBright) 40 | of "dim": ansiStyleCode(styleDim) 41 | of "italic": ansiStyleCode(styleItalic) 42 | of "underscore": ansiStyleCode(styleUnderscore) 43 | of "blink": ansiStyleCode(styleBlink) 44 | of "blinkrapid": ansiStyleCode(styleBlinkRapid) 45 | of "reverse": ansiStyleCode(styleReverse) 46 | of "hidden": ansiStyleCode(styleHidden) 47 | of "strikethrough": ansiStyleCode(styleStrikethrough) 48 | of "reset": ansiResetCode 49 | else: "" 50 | 51 | proc get_ansi*(kind:AnsiKind): string = 52 | case kind 53 | of ansi_green: get_ansi("green") 54 | of ansi_cyan: get_ansi("cyan") 55 | of ansi_red: get_ansi("red") 56 | of ansi_blue: get_ansi("blue") 57 | of ansi_magenta: get_ansi("magenta") 58 | of ansi_yellow: get_ansi("yellow") 59 | of ansi_white: get_ansi("white") 60 | of ansi_black: get_ansi("black") 61 | of ansi_bright: get_ansi("bright") 62 | of ansi_dim: get_ansi("dim") 63 | of ansi_italic: get_ansi("italic") 64 | of ansi_underscore: get_ansi("underscore") 65 | of ansi_blink: get_ansi("blink") 66 | of ansi_blinkrapid: get_ansi("blinkrapid") 67 | of ansi_reverse: get_ansi("reverse") 68 | of ansi_hidden: get_ansi("hidden") 69 | of ansi_strikethrough: get_ansi("strikethrough") 70 | of ansi_reset: get_ansi("reset") 71 | 72 | proc get_ansi*(list:seq[string]): string = 73 | var s = "" 74 | for item in list: 75 | let a = case item 76 | of "green": get_ansi("green") 77 | of "cyan": get_ansi("cyan") 78 | of "red": get_ansi("red") 79 | of "blue": get_ansi("blue") 80 | of "magenta": get_ansi("magenta") 81 | of "yellow": get_ansi("yellow") 82 | of "white": get_ansi("white") 83 | of "black": get_ansi("black") 84 | of "bright": get_ansi("bright") 85 | of "dim": get_ansi("dim") 86 | of "underscore": get_ansi("underscore") 87 | of "italic": get_ansi("italic") 88 | of "blink": get_ansi("blink") 89 | of "blinkrapid": get_ansi("blinkrapid") 90 | of "reverse": get_ansi("reverse") 91 | of "hidden": get_ansi("hidden") 92 | of "strikethrough": get_ansi("strikethrough") 93 | of "reset": get_ansi("reset") 94 | else: "" 95 | 96 | if a != "": s.add(a) 97 | 98 | return s 99 | 100 | proc reset*(): string = 101 | get_ansi("reset") 102 | 103 | proc log*(s:string, last=false, linebreak=true) = 104 | let 105 | line = &"{reset()}{s}" 106 | lbrk = if linebreak: "\n" else: "" 107 | line2 = &"{line}{lbrk}" 108 | 109 | stdout.write(line2) 110 | 111 | if conf().output != "": 112 | all_output.add(line2) 113 | 114 | proc toke*() = 115 | let spaced = not conf().list 116 | if spaced: log "" 117 | 118 | proc fix_path*(path:string): string = 119 | var path = expandTilde(path) 120 | normalizePath(path) 121 | if not path.startsWith("/"): 122 | path = getCurrentDir().joinPath(path) 123 | return path 124 | 125 | proc fix_path_2*(path:string): string = 126 | var path = expandTilde(path) 127 | normalizePath(path) 128 | if not path.startsWith("/"): 129 | path = fix_path(conf().path).joinPath(path) 130 | return path -------------------------------------------------------------------------------- /tast.txt: -------------------------------------------------------------------------------- 1 | 2 | .git bin src .gitignore LICENSE lq.nimble README.md tast.txt lq 3 | 4 | --------------------------------------------------------------------------------