Skip to content

Commit

Permalink
add --before, --after options
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ward committed Jan 5, 2025
1 parent 0301ed7 commit 66343ba
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- relative time option (-T)
- mime type (-M)
- entry number (-#)
- --before, --after TIME options

## [2024.6]
### Added
Expand Down
52 changes: 46 additions & 6 deletions lsv/filter.v
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import time

enum ByTime {
before_modified
before_accessed
before_changed
after_modified
after_accessed
after_changed
}

fn filter(entries []Entry, options Options) []Entry {
return match true {
// vfmt off
options.only_dirs { entries.clone().filter(it.dir) }
options.only_files { entries.clone().filter(it.file) }
else { entries }
// vfmt on
mut filtered := entries.clone()

if options.only_dirs {
filtered = filtered.filter(it.dir)
}

if options.only_files {
filtered = filtered.filter(it.file)
}

filtered = filter_time(filtered, options.time_before_modified, .before_modified)
filtered = filter_time(filtered, options.time_before_modified, .before_accessed)
filtered = filter_time(filtered, options.time_before_modified, .before_changed)

filtered = filter_time(filtered, options.time_after_modified, .after_modified)
filtered = filter_time(filtered, options.time_after_accessed, .after_accessed)
filtered = filter_time(filtered, options.time_after_changed, .after_changed)

return filtered
}

fn filter_time(entries []Entry, time_str string, by ByTime) []Entry {
if time_str.len == 0 {
return entries
}

target_time := time.parse_iso8601(time_str) or { exit_error(err.msg()) }

return match by {
.before_modified { entries.filter(time.unix(it.stat.mtime) < target_time) }
.before_accessed { entries.filter(time.unix(it.stat.atime) < target_time) }
.before_changed { entries.filter(time.unix(it.stat.ctime) < target_time) }
.after_modified { entries.filter(time.unix(it.stat.mtime) > target_time) }
.after_accessed { entries.filter(time.unix(it.stat.atime) > target_time) }
.after_changed { entries.filter(time.unix(it.stat.ctime) > target_time) }
}
}
52 changes: 35 additions & 17 deletions lsv/options.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ struct Options {
no_wrap bool
//
// filter, group and sorting options
all bool
almost_all bool
dirs_first bool
only_dirs bool
only_files bool
recursion_depth int
recursive bool
sort_ext bool
sort_ignore_case bool
sort_natural bool
sort_none bool
sort_reverse bool
sort_size bool
sort_time bool
sort_width bool
all bool
almost_all bool
dirs_first bool
only_dirs bool
only_files bool
recursion_depth int
recursive bool
sort_ext bool
sort_ignore_case bool
sort_natural bool
sort_none bool
sort_reverse bool
sort_size bool
sort_time bool
sort_width bool
time_before_modified string
time_before_accessed string
time_before_changed string
time_after_modified string
time_after_accessed string
time_after_changed string
//
// long view options
accessed_date bool
Expand Down Expand Up @@ -105,14 +111,20 @@ fn parse_args(args []string) Options {
width_in_cols := fp.int('width', 0, 0, 'set output width to <int>\n\nFiltering and Sorting Options:')
only_dirs := fp.bool('', `d`, false, 'list only directories')
only_files := fp.bool('', `f`, false, 'list only files')
dirs_first := fp.bool('', `g`, false, 'group directories before files')
dirs_first := fp.bool('', `g`, false, 'sort directories before files')
sort_reverse := fp.bool('', `r`, false, 'reverse the listing order')
sort_size := fp.bool('', `s`, false, 'sort by file size, largest first')
sort_time := fp.bool('', `t`, false, 'sort by time, newest first')
sort_natural := fp.bool('', `v`, false, 'sort digits within text as numbers')
sort_width := fp.bool('', `w`, false, 'sort by width, shortest first')
sort_ext := fp.bool('', `x`, false, 'sort by file extension')
sort_none := fp.bool('', `u`, false, 'no sorting\n')
time_after_modifed := fp.string('after', 0, '', 'after modified time <string>')
time_after_accessed := fp.string('after-access ', 0, '', 'after access time <string>')
time_after_changed := fp.string('after-change', 0, '', 'after change time <string>')
time_before_modifed := fp.string('before', 0, '', 'before modified time <string>')
time_before_accessed := fp.string('before-access', 0, '', 'before access time <string>')
time_before_changed := fp.string('before-change', 0, '', 'before change time <string>\n${flag.space}<string> is any ISO 8601 time format)\n')
sort_ignore_case := fp.bool('ignore-case', 0, false, 'ignore case when sorting\n\nLong Listing Options:')

blocked_output := fp.bool('', `b`, false, 'blank line every 5 rows')
Expand All @@ -132,7 +144,7 @@ fn parse_args(args []string) Options {
time_relative := fp.bool('', `T`, false, 'show relative time')
mime_type := fp.bool('', `M`, false, 'show mime type')
inode := fp.bool('', `N`, false, 'show inodes')
no_wrap := fp.bool('', `Z`, false, 'do not wrap long lines\n')
no_wrap := fp.bool('', `W`, false, 'do not wrap long lines\n')

checksum := fp.string('cs', 0, '', 'show file checksum\n${flag.space}(md5, sha1, sha224, sha256, sha512, blake2b)')
no_count := fp.bool('no-counts', 0, false, 'hide file/dir counts')
Expand Down Expand Up @@ -209,6 +221,12 @@ fn parse_args(args []string) Options {
style_pi: style_map['pi']
style_so: style_map['so']
table_format: table_format && long_format
time_before_modified: time_before_modifed
time_before_accessed: time_before_accessed
time_before_changed: time_before_changed
time_after_modified: time_after_modifed
time_after_accessed: time_after_accessed
time_after_changed: time_after_changed
time_compact: time_compact
time_compact_with_day: time_compact_with_day
time_iso: time_iso
Expand Down

0 comments on commit 66343ba

Please sign in to comment.