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

feat(filesystem): customizable timestamp formats #1614

Merged
merged 12 commits into from
Jan 13, 2025

Conversation

MastarCheeze
Copy link
Contributor

I've added a format field in the configs for the components last_modified and created so that users can change the format string used to display timestamps in the file explorer. The field also accepts a function that returns a string so that features like relative timestamps can be implemented. The change is also applied to the file details popup window.

Here's an example setup:

require("neo-tree").setup({
  default_component_configs = {
    last_modified = {
      enabled = true,
      format = function(seconds)
        local now = os.time()
        local diff = now - seconds

        local function pluralise(value, unit)
          return value .. " " .. unit .. (value == 1 and "" or "s") .. " ago"
        end

        if diff < 60 then
          return "Just now"
        elseif diff < 3600 then
          local minutes = math.floor(diff / 60)
          return pluralise(minutes, "minute")
        elseif diff < 86400 then
          local hours = math.floor(diff / 3600)
          return pluralise(hours, "hour")
        elseif diff < 86400 * 30 then
          local days = math.floor(diff / 86400)
          return pluralise(days, "day")
        elseif diff < 86400 * 365 then
          local months = math.floor(diff / (86400 * 30))
          return pluralise(months, "month")
        else
          local years = math.floor(diff / (86400 * 365))
          return pluralise(years, "year")
        end
      end,
    },
    created = {
      enabled = true,
      format = "%e %b %y at %I:%M %p",
    },
  },
})

File explorer
File details popup window

The columns are offset in the image because the the timestamp is longer than the column heading. I'll be opening a separate pull request that deals with that.

@MastarCheeze
Copy link
Contributor Author

I just found out that there is already an existing issue (#1505) and a PR (#1506) for this exact topic.

lua/neo-tree/defaults.lua Outdated Show resolved Hide resolved
@MastarCheeze
Copy link
Contributor Author

I've added the changes you suggested @pynappo.

On second thought, I think its better to use default_component_configs.created.format and default_component_configs.modified.format as fallback values for the mapping level configs, as you suggested, to reduce the amount of configuration the user has to do. I've implemented this in my commit, along with documentation.

Thanks for the suggestions!

@MastarCheeze
Copy link
Contributor Author

I'm thinking of adding relative timestamps as a builtin feature that can be enabled by specifying:

default_component_configs = {
  last_modified = {
    relative = true
  }
}

Or perhaps like this:

default_component_configs = {
  last_modified = {
    format = "relative"
  }
}

Or even just include a commented-out implementation of a relative timestamp function in README.md.

Any opinions about this? Should this be in its own PR?

@pynappo
Copy link
Collaborator

pynappo commented Dec 23, 2024

I'm thinking of adding relative timestamps as a builtin feature that can be enabled by specifying:

default_component_configs = {
  last_modified = {
    relative = true
  }
}

Or perhaps like this:

default_component_configs = {
  last_modified = {
    format = "relative"
  }
}

Or even just include a commented-out implementation of a relative timestamp function in README.md.

Any opinions about this? Should this be in its own PR?

Personally I'm leaning towards format = "relative". Commented out implementation would work as well.

I think it can be bundled in with this PR. Separately is okay too if you don't want to do that rn.

lua/neo-tree/defaults.lua Outdated Show resolved Hide resolved
lua/neo-tree/sources/common/commands.lua Outdated Show resolved Hide resolved
MastarCheeze and others added 2 commits December 26, 2024 19:11
Make the timestamp format in the file details popup properly fall back to the format used in the file explorer.

Co-authored-by: pynappo <[email protected]>
@MastarCheeze
Copy link
Contributor Author

All done! I've added the suggested changes.

@MastarCheeze
Copy link
Contributor Author

Any updates on this @pynappo?

@pynappo
Copy link
Collaborator

pynappo commented Jan 13, 2025

Apologies, had to think about the best way to reduce the amount of configuration needed. I've decided to remove the fallback of file_details's timestamps on the component configs because I think it should always default to giving a detailed absolute time. I also added format = "relative" as shorthand.

@pynappo pynappo changed the title feat(filesystem): Customisable timestamps for last modified and created feat(filesystem): customizable timestamp formats Jan 13, 2025
@pynappo pynappo merged commit 343886b into nvim-neo-tree:main Jan 13, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants