Skip to content

Commit

Permalink
add -L option, tweak dates
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ward committed Jul 18, 2024
1 parent aae82c7 commit f690eb5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
All notable changes to this project will be documented in this file.

## [Unreleased]
### Added
- compact date with week day (e.g. Thu 18 Jul'24 09:03)

### Updated
- tweaked the help layout
- removed the last vestiages of line buffering
- remove leading 0 in compact date format

## [2024.4] - 2024-07-07
### Added
Expand Down
17 changes: 9 additions & 8 deletions lsv/entry.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn make_entry(file string, dir_name string, options Options) Entry {
}

filetype := stat.get_filetype()
is_link := filetype == os.FileType.symbolic_link
is_link := filetype == .symbolic_link
link_origin := if is_link { read_link(path) } else { '' }
mut size := stat.size
mut link_stat := os.Stat{}
Expand All @@ -76,8 +76,7 @@ fn make_entry(file string, dir_name string, options Options) Entry {
is_character_device := filetype == .character_device
is_unknown := filetype == .unknown
is_exe := !is_dir && is_executable(stat)
is_file := !is_dir && !is_fifo && !is_block && !is_socket && !is_character_device && !is_unknown
&& !is_exe && !invalid
is_file := filetype == .regular
indicator := if is_dir && options.dir_indicator { '/' } else { '' }

return Entry{
Expand Down Expand Up @@ -105,10 +104,6 @@ fn make_entry(file string, dir_name string, options Options) Entry {
}
}

fn is_executable(stat os.Stat) bool {
return stat.get_mode().bitmask() & 0b001001001 > 0
}

fn readable_size(size u64, si bool) string {
kb := if si { f64(1024) } else { f64(1000) }
mut sz := f64(size)
Expand Down Expand Up @@ -152,6 +147,12 @@ fn checksum(name string, dir_name string, options Options) string {
}
}

@[inline]
fn is_executable(stat os.Stat) bool {
return stat.get_mode().bitmask() & 0b001001001 > 0
}

@[inline]
fn is_dot_file(file string) bool {
return file.len > 0 && file[0] == `.`
return file.starts_with('.')
}
14 changes: 10 additions & 4 deletions lsv/format_long.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const space = ' '
const date_format = 'MMM DD YYYY HH:mm:ss'
const date_iso_format = 'YYYY-MM-DD HH:mm:ss'
const date_compact_format = "DD MMM'YY HH:mm"
const date_compact_format_with_day = "ddd DD MMM'YY HH:mm"

struct Longest {
inode int
Expand Down Expand Up @@ -286,9 +287,10 @@ fn format_header(options Options, longest Longest) (string, []int) {
fn time_format(options Options) string {
return match true {
// vfmt off
options.time_iso { date_iso_format }
options.time_compact { date_compact_format }
else { date_format }
options.time_iso { date_iso_format }
options.time_compact { date_compact_format }
options.time_compact_with_day { date_compact_format_with_day }
else { date_format }
// vfmt on
}
}
Expand Down Expand Up @@ -388,10 +390,14 @@ fn format_time(entry Entry, stat_time StatTime, options Options) string {
.modified { entry.stat.mtime }
}

date := time.unix(entry_time)
mut date := time.unix(entry_time)
.local()
.custom_format(time_format(options))

if date.starts_with('0') {
date = ' ' + date[1..]
}

dim := if options.no_dim { no_style } else { dim_style }
content := if entry.invalid { '?' + space.repeat(date.len - 1) } else { date }
return format_cell(content, date.len, .left, dim, options)
Expand Down
40 changes: 21 additions & 19 deletions lsv/options.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ struct Options {
sort_width bool
//
// long view options
accessed_date bool
changed_date bool
header bool
inode bool
no_count bool
no_date bool
no_group_name bool
no_hard_links bool
no_owner_name bool
no_permissions bool
no_size bool
octal_permissions bool
size_kb bool
size_ki bool
time_iso bool
time_compact bool
checksum string
accessed_date bool
changed_date bool
header bool
inode bool
no_count bool
no_date bool
no_group_name bool
no_hard_links bool
no_owner_name bool
no_permissions bool
no_size bool
octal_permissions bool
size_kb bool
size_ki bool
time_iso bool
time_compact bool
time_compact_with_day bool
checksum string
//
// from ls colors
style_di Style
Expand Down Expand Up @@ -115,6 +116,7 @@ fn parse_args(args []string) Options {
header := fp.bool('', `H`, false, 'show column headers')
time_iso := fp.bool('', `I`, false, 'show time in iso format')
time_compact := fp.bool('', `J`, false, 'show time in compact format')
time_compact_with_day := fp.bool('', `L`, false, 'show time in compact format with week day')
inode := fp.bool('', `N`, false, 'show inodes')
no_wrap := fp.bool('', `Z`, false, 'do not wrap long lines\n')

Expand All @@ -128,8 +130,7 @@ fn parse_args(args []string) Options {
no_permissions := fp.bool('no-permissions', 0, false, 'hide permissions')
no_size := fp.bool('no-size', 0, false, 'hide file size\n')

fp.footer('
fp.footer('\n
The -c option emits color codes when standard output is
connected to a terminal. Colors are defined in the LS_COLORS
environment variable.'.trim_indent())
Expand Down Expand Up @@ -189,6 +190,7 @@ fn parse_args(args []string) Options {
style_so: style_map['so']
table_format: table_format && long_format
time_compact: time_compact
time_compact_with_day: time_compact_with_day
time_iso: time_iso
width_in_cols: width_in_cols
with_commas: with_commas
Expand Down

0 comments on commit f690eb5

Please sign in to comment.