Repository: gokcehan/lf Branch: master Commit: 551f9d3418c0 Files: 73 Total size: 721.6 KB Directory structure: gitextract_3_ndz1ve/ ├── .github/ │ ├── dependabot.yml │ └── workflows/ │ ├── go.yml │ └── release.yml ├── .gitignore ├── .golangci.yaml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── app.go ├── client.go ├── colors.go ├── colors_test.go ├── complete.go ├── complete_test.go ├── copy.go ├── df_openbsd.go ├── df_statfs.go ├── df_statvfs.go ├── df_windows.go ├── diacritics.go ├── diacritics_test.go ├── doc.md ├── doc.txt ├── etc/ │ ├── colors.example │ ├── icons.example │ ├── icons_colored.example │ ├── lf.bash │ ├── lf.csh │ ├── lf.fish │ ├── lf.nu │ ├── lf.ps1 │ ├── lf.vim │ ├── lf.zsh │ ├── lfcd.cmd │ ├── lfcd.csh │ ├── lfcd.fish │ ├── lfcd.nu │ ├── lfcd.ps1 │ ├── lfcd.sh │ ├── lfrc.cmd.example │ ├── lfrc.example │ ├── lfrc.ps1.example │ └── ruler.default ├── eval.go ├── eval_test.go ├── gen/ │ ├── build.sh │ ├── deflist.lua │ ├── doc.sh │ └── package.sh ├── go.mod ├── go.sum ├── icons.go ├── key.go ├── key_test.go ├── lf.1 ├── lf.desktop ├── main.go ├── misc.go ├── misc_test.go ├── nav.go ├── opts.go ├── os.go ├── os_windows.go ├── parse.go ├── ruler.go ├── scan.go ├── server.go ├── sixel.go ├── termseq.go ├── termseq_test.go ├── ui.go └── watch.go ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/dependabot.yml ================================================ version: 2 updates: - package-ecosystem: github-actions directory: / schedule: interval: daily commit-message: prefix: chore include: scope - package-ecosystem: gomod directory: / schedule: interval: daily commit-message: prefix: chore include: scope ================================================ FILE: .github/workflows/go.yml ================================================ name: Go on: push: branches: [master] pull_request: branches: [master] jobs: formatting: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - name: Setup Go uses: actions/setup-go@v6 with: go-version-file: go.mod - name: Check formatting run: go fmt && git diff --exit-code tests: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v6 - name: Setup Go uses: actions/setup-go@v6 with: go-version-file: go.mod - name: Build run: go build -v ./... - name: Test run: go test -v ./... - name: golangci-lint uses: golangci/golangci-lint-action@v9 with: version: v2.5.0 build: runs-on: ubuntu-latest strategy: matrix: include: - { os: android, arch: arm64 } - { os: darwin, arch: amd64 } - { os: darwin, arch: arm64 } - { os: dragonfly, arch: amd64 } - { os: freebsd, arch: "386" } - { os: freebsd, arch: amd64 } - { os: freebsd, arch: arm } - { os: illumos, arch: amd64 } - { os: linux, arch: "386" } - { os: linux, arch: amd64 } - { os: linux, arch: arm } - { os: linux, arch: arm64 } - { os: linux, arch: ppc64 } - { os: linux, arch: ppc64le } - { os: linux, arch: mips } - { os: linux, arch: mipsle } - { os: linux, arch: mips64 } - { os: linux, arch: mips64le } - { os: linux, arch: s390x } - { os: netbsd, arch: "386" } - { os: netbsd, arch: amd64 } - { os: netbsd, arch: arm } - { os: openbsd, arch: "386" } - { os: openbsd, arch: amd64 } - { os: openbsd, arch: arm } - { os: openbsd, arch: arm64 } - { os: solaris, arch: amd64 } - { os: windows, arch: "386" } - { os: windows, arch: amd64 } # Unsupported # - { os: aix, arch: ppc64 } # - { os: android, arch: "386" } # - { os: android, arch: amd64 } # - { os: android, arch: arm } # - { os: js, arch: wasm } # - { os: plan9, arch: "386" } # - { os: plan9, arch: amd64 } # - { os: plan9, arch: arm } steps: - uses: actions/checkout@v6 with: fetch-depth: 0 # https://github.com/actions/checkout/issues/2199 fetch-tags: true - name: Setup Go uses: actions/setup-go@v6 with: go-version-file: go.mod - name: Build run: gen/build.sh env: GOOS: ${{ matrix.os }} GOARCH: ${{ matrix.arch }} ================================================ FILE: .github/workflows/release.yml ================================================ name: Release on: push: tags: - "*" jobs: build: runs-on: ubuntu-latest strategy: matrix: include: - { os: android, arch: arm64 } - { os: darwin, arch: amd64 } - { os: darwin, arch: arm64 } - { os: dragonfly, arch: amd64 } - { os: freebsd, arch: "386" } - { os: freebsd, arch: amd64 } - { os: freebsd, arch: arm } - { os: illumos, arch: amd64 } - { os: linux, arch: "386" } - { os: linux, arch: amd64 } - { os: linux, arch: arm } - { os: linux, arch: arm64 } - { os: linux, arch: ppc64 } - { os: linux, arch: ppc64le } - { os: linux, arch: mips } - { os: linux, arch: mipsle } - { os: linux, arch: mips64 } - { os: linux, arch: mips64le } - { os: linux, arch: s390x } - { os: netbsd, arch: "386" } - { os: netbsd, arch: amd64 } - { os: netbsd, arch: arm } - { os: openbsd, arch: "386" } - { os: openbsd, arch: amd64 } - { os: openbsd, arch: arm } - { os: openbsd, arch: arm64 } - { os: solaris, arch: amd64 } - { os: windows, arch: "386" } - { os: windows, arch: amd64 } # Unsupported # - { os: aix, arch: ppc64 } # - { os: android, arch: "386" } # - { os: android, arch: amd64 } # - { os: android, arch: arm } # - { os: js, arch: wasm } # - { os: plan9, arch: "386" } # - { os: plan9, arch: amd64 } # - { os: plan9, arch: arm } steps: - uses: actions/checkout@v6 - name: Setup Go uses: actions/setup-go@v6 with: go-version-file: go.mod - name: Build run: gen/build.sh env: GOOS: ${{ matrix.os }} GOARCH: ${{ matrix.arch }} - name: Package run: gen/package.sh env: GOOS: ${{ matrix.os }} GOARCH: ${{ matrix.arch }} - name: Upload artifact uses: actions/upload-artifact@v7 with: name: lf-${{ matrix.os }}-${{ matrix.arch }} path: dist/* release: runs-on: ubuntu-latest needs: build steps: - name: Download artifacts uses: actions/download-artifact@v8 with: path: dist merge-multiple: true - name: Release uses: softprops/action-gh-release@v2 with: files: dist/* winget: runs-on: ubuntu-latest needs: build steps: - uses: vedantmgoyal2009/winget-releaser@v2 with: identifier: gokcehan.lf version: ${{ github.ref_name }} installers-regex: '-windows-\w+\.zip$' token: ${{ secrets.WINGET_TOKEN }} ================================================ FILE: .gitignore ================================================ lf lf.exe tags dist/ vendor/ ================================================ FILE: .golangci.yaml ================================================ version: "2" linters: settings: errcheck: exclude-functions: - (*github.com/fsnotify/fsnotify.Watcher).Close - (*net.TCPConn).CloseWrite - (*net.UnixConn).CloseWrite - (*os.File).Close - (*os.File).Write - (*text/tabwriter.Writer).Flush - (io.Closer).Close - (io.Writer).Write - (net.Conn).Close - (net.Listener).Close - fmt.Fprintf - fmt.Fprintln - io.WriteString - os.Setenv - syscall.Close issues: max-issues-per-linter: 0 # no limit max-same-issues: 0 # no limit ================================================ FILE: CHANGELOG.md ================================================ # Changelog All changes observable to end users should be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and should contain the following sections for each release: - `Changed` - `Added` - `Fixed` ## Unreleased ### Changed - The key `` has been renamed to `` for `map` keybindings (#2286). - `.Stat.DirSize` and `.Stat.DirCount` in the ruler file no longer have a pointer type, and will be set to `-1` instead of `nil` if the corresponding value cannot be determined (#2397). - `setlocal` commands no longer support the ability to specify recursive directories (#2415). For use cases where `setlocal` should apply to a directory based on some condition, it is recommended to script this inside the `on-load` hook command. ### Added - Emoji sequences containing Zero Width Joiner characters are now displayed as a single combined glyph (#2286). - A new field `.All` is added to the `ruler` file to display the number of all files (i.e. visible + hidden) in the current working directory (#2376). - A new option `numbercursorfmt` is added to further customize the appearance of line numbers (#2395). - A new option `terminalcursor` is added to customize the appearance of the terminal cursor (#2441). - A new option `borderstyle` is added to control whether `drawbox` draws an outline, separators, or both (#2445). - The `loading...` message delay of 100 milliseconds for file previews is now applied to directories as well (#2410). - `lf` will now automatically change to the parent directory if the current directory no longer exists and the `watch` option is enabled (#2424). ### Fixed - Previews are now cleaned when changing to an empty directory (#2369). - Previews are now correctly updated on `visual-change` (#2373). - The `dircounts` indicator for errors is changed back to `!` instead of `?` (#2372). - The `select` command can now select files immediately after creation as part of a script (#2377). - The `on-load` hook command now ignores `.git` directories to reduce flicker and repeated `on-load` triggers (#2382). - Preview messages like `empty` or `loading...` have their alignment improved (#2400). - A bug where the `loading...` message was not displayed for volatile previews after the first time is now fixed (#2410). - The `cmd-transpose` command now advances the cursor correctly after swapping characters (#2413). - Symbolic links are no longer followed when changing directories (#2423). - Using the `select` command with a blank string as the argument now properly raises an error instead of changing to the parent directory (#2429). - Executable files on Windows are now correctly recognized for icon and color lookup based on `PATHEXT` (#2448). ## [r41](https://github.com/gokcehan/lf/releases/tag/r41) ### Changed - The `previewer` script no longer skips non-regular files (#2327). - Line numbers now take up less space when both `number` and `relativenumber` are enabled (#2331). - Changes have been made to ruler configuration (#2338): - The ruler file is no longer experimental. - The ruler file will be used by default unless `rulerfmt` (now a blank string by default) is specified. - The ruler file is no longer read from fixed locations like `~/.config/lf/ruler`, and instead the `rulerfile` option has been repurposed to specify the path of the ruler file. ### Added - A new server command `list` is added to print the IDs of all currently connected clients (#2314). - The `previewer` and `cleaner` scripts now have their `stderr` output logged (#2316). - The `ruler` file now supports `.Stat.Extension`, `.Stat.AccessTime`, `.Stat.BirthTime`, `.Stat.ChangeTime` and `.Stat.CustomInfo` (#2329), as well as `.Stat.DirSize` and `.Stat.DirCount` (#2343). - A new option `mergeindicators` is added to reduce the gap before filenames, by merging tag and selection indicators into a single column (#2330). ### Fixed - A bug where sixel images fail to display when scrolling back and forth is now fixed (#2301). - Newline characters are now ignored when drawing the ruler with the `ruler` file configured (#2319). - A potential crash when using the `scroll-up`/`scroll-down` commands is now fixed (#2320). - Case-insensitive command-line completions no longer cause user input to be displayed in lowercase (#2336). - Calculation of window widths for the `ratios` option is now more accurate (#2347). ## [r40](https://github.com/gokcehan/lf/releases/tag/r40) ### Fixed - Error messages from the server are no longer written to the terminal which causes the UI to break (#2290). - A bug where file previews fail to load properly when scrolling quickly is now fixed (#2292). ## [r39](https://github.com/gokcehan/lf/releases/tag/r39) ### Changed - File extensions are now shown for long filenames even if they are truncated (#2159). - The history file will no longer contain a space between the prefix and the actual command (e.g. `:quit` instead of `: quit`) for each line (#2161). The command `sed -i -E 's/^(.) /\1/' ~/.local/share/lf/history` can be run to make an existing history file compatible with the new format. - The `cmd-history-next` and `cmd-history-prev` commands will now select only matching entries if part of the command is typed beforehand (#2161). Consecutive duplicate entries will also be skipped for convenience. - The string representation of commands shown when displaying keybindings is simplified (e.g. `cd ~` instead of `cd -- [~]`) (#2165). - `yes-no-prompts` now use the same design everywhere (#2212). - Logs generated by `-log ` now get appended to `` instead of overwriting it (#2215). - The `showbinds` menu now hides the redundant `mode` column (#2226) (#2228) and omits already typed keys (#2249). - The `setlocal` command no longer requires absolute paths (#2253). - The string representation of file permissions now matches the traditional `Unix` format instead of the one used by `Go` (#2270). ### Added - A new command `cmd-menu-discard` is added to allow exiting the completion menu with completions discarded (#2146). - The `lf_mode` environment variable will now be set to `compmenu` if the completion menu is active (#2146). - A `ruler` config file is added as an alternate method for customizing the ruler (#2186). This is intended to eventually replace the existing `rulerfmt`/`statfmt` options and must be enabled using the new `rulerfile` option. **This feature is currently experimental.** - A new option `preload` is added to enable calling the `previewer` to generate previews in advance (#2206). **This feature is currently experimental.** - `OSC 8` escape codes to render clickable hyperlinks are now supported (#2243). ### Fixed - `shell-pipe` commands no longer wait for output if kept open after the command has finished running (#2155). - Natural sorting now compares string lengths when dealing with equivalent numbers (e.g. `0` is ordered before `00`) (#2177). - A bug where the copy progress indicator only displayed the first time a file was copied is now fixed (#2181). - A bug where the `source` command does not show an error message upon failure is now fixed (#2189). - The `addcustominfo` (#2198) and `setlocal` (#2254) (#2259) commands, as well as the `cleaner` and `previewer` options (#2211) now support filename completions. - A bug where an empty `custom` info property would still take up space is now fixed (#2208). - A bug where setting `drawbox` could lead to scrolling outside the view is now fixed (#2210) (#2218). - The preview cache is now not cleared when setting `ratios` to its current value (#2218). - Filtering is fixed when using special characters in the search pattern if the `filtermethod` is `text` (#2231). - Custom commands that output messages (e.g. `cmd greet echo 'hello world'`) now display properly (#2245). - Errors in config files are now displayed properly (#2246). ## [r38](https://github.com/gokcehan/lf/releases/tag/r38) ### Changed - The deprecated `globfilter` and `globsearch` options are now removed (#2108). - Sixel image support is now enabled by default, and the `sixel` option has been removed as it is no longer required (#2109). - The `dircache` option has now been removed (#2110). This was previously used as a workaround to disable the directory cache since at the time changes to files were not detected reliably, but this is no longer the case. - The experimental `locale` option has been removed in favor of the recommendation to use `addcustominfo`/`set sortby custom` for custom sorting (#2111). - The existing `doc` command has been renamed to `help` so that it is more natural for users (#2125). - Text previews are no longer displayed with a padding of two spaces by default (#2131). Instead, a custom padding can be added in the `previewer` script, for example by piping to `sed 's/^/ /'`. ### Added - Sixel image previews can now display multiple images as well as text (#2109). - A new option `sizeunits` is added to allow displaying file sizes in either binary or decimal units (#2118). - `XDG_CONFIG_HOME` and `XDG_DATA_HOME` are now taken into account when looking up config/data files on Windows (#2119). - Three new options `menufmt`, `menuheaderfmt`, and `menuselectfmt` are added to customize the appearance of the menu (#2123). ### Fixed - Error messages are now cleared after running interactive commands such as `invert`/`unselect`/`tag-toggle` (#2117). - The menu is now drawn over sixel images instead of being hidden behind it if they overlap (#2122). - A bug which prevents the user from quitting when copying files with a size of 0 has been fixed (#2130). - `lf -remote` should no longer busy wait and cause high CPU usage if its output is not being read (#2138). - The parameter types for command line options shown by `lf -help` now match the synopsis in the documentation (#2153). ## [r37](https://github.com/gokcehan/lf/releases/tag/r37) ### Changed - The default paths of files read by `lf` is changed on Windows, to separate configuration files from data files (#2051). - Configuration files (`lfrc`/`colors`/`icons`) are now stored in `%APPDATA%`, which can be overridden by `%LF_CONFIG_HOME%`. - Data files (`files`/`marks`/`tags`/`history`) are now stored in `%LOCALAPPDATA%`, which can be overridden by `%LF_DATA_HOME%`. - The change for following symbolic links when tagging files from the previous release has been reverted (#2055). The previous change made it impossible to tag symbolic links separately from their targets, and also caused `lf` to run slowly in some cases. - The existing `globfilter` and `globsearch` options are now deprecated in favor of the new `filtermethod` and `searchmethod` options, which support regex patterns (#2058). - `set globfilter true` should be replaced by `set filtermethod glob`. - `set globsearch true` should be replaced by `set searchmethod glob`. - File sizes are now displayed using binary units (e.g. `1.0K` means 1024 bytes, not 1000 bytes) (#2062). The maximum width for displaying the file size has been increased from four to five characters. ### Added - `dircounts` are now respected when sorting by size (#2025). - The `info` and `sortby` options now support `btime` (file creation time) (#2042). This depends on support for file creation times from the underlying system. - The selection in Visual mode now follows wrapping when `wrapscan`/`wrapscroll` is enabled (#2056). - Input pasted from the terminal is now ignored while in Normal mode (#2059). This prevents pasted content from being treated as keybindings, which can result in dangerous unintended behavior. - The Command-line mode completion now supports keywords for the `selmode` and `sortby` options (#2061), as well as the `info` and `preserve` options (#2071). - Command line options are now exported as environment variables in the form `lf_flag_{flag}` (#2079). - Support is added for terminal escape sequences that disable text styles (#2101). ### Fixed - `dircounts` are now automatically populated after enabling it (#2049). - A bug where directories are unsorted after reloading when `dircache` is disabled is now fixed (#2050). - Filenames are now escaped when completing shell commands (#2071). - A bug where completion menu entries are misaligned when containing fullwidth characters is now fixed (#2071). - The `on-load` command now passes all files in the directory as arguments, not just files visible to the user (#2077). - Failure to move files across different filesystems is now shown as an error instead of a success in the UI (#2085). - Errors are now logged correctly when there are multiple errors during move/copy operations (#2089). - The progress for copy operations is now displayed immediately in the UI, even if it takes time to calculate the total size of files to be copied (#2093). ## [r36](https://github.com/gokcehan/lf/releases/tag/r36) ### Changed - Tagging symbolic links now affects the target instead of the symbolic link itself. This mimics the behavior in `ranger` (#1997). - The experimental command `invert-below` has been removed in favor of the newly added support for Visual mode (#2021). ### Added - A new placeholder `%P` representing the scroll percentage is added to the `rulerfmt` option (#1985). - A new `on-load` hook command is added, which is triggered when files in a directory are loaded in `lf` (#2010). - The `info` option now supports `custom`, allowing users to display custom information for each file (#2012). The custom information should be added by the user via the `addcustominfo` command. Sorting by the custom information is also supported (#2019). - Support for `visual-mode` has now been added (#2021) (#2035). This includes the following changes: - A new command `visual` (default `V`) can be used to enter Visual mode. - A new command `visual-change` (default `o` in Visual mode) can be used to swap the positions of the cursor and anchor (start of the visual selection). - A new command `visual-accept` (default `V` in Visual mode) can be used to exit Visual mode, adding the visual selection to the selection list. - A new command `visual-discard` (default `` in Visual mode) can be used to exit Visual mode, without adding the visual selection to the selection list. - A new command `visual-unselect` can be used to exit Visual mode, removing the visual selection from the selection list. - The existing `map` command now adds keybindings for both Normal and Visual modes. Two new commands `nmap` and `vmap` are added which can be used to add keybindings for only Normal or Visual mode respectively. - Two new commands `nmaps` and `vmaps` are added to display the list of keybindings in Normal and Visual mode respectively. These, along with the existing `maps` and `cmaps` commands, now display an extra column indicating the mode for which the keybindings apply to. - A new option `visualfmt` is added to customize the appearance of the visual selection. - Two new placeholders `%m` and `%M` are added to `statfmt` to display the mode in the status line. Both will display `VISUAL` when in Visual mode, however in Normal mode `%m` will display as a blank string while `%M` will display `NORMAL`. - A new placeholder `%v` is added to `rulerfmt` which displays the number of files in the Visual selection. This is included in the default setting for `rulerfmt`. - The `lf_mode` environment variable will now be set to `visual` while in Visual mode. - The environment variable `$fv` is now exported to shell commands, which lists the files in the visual selection. - A `CHANGELOG.md` file has been added to the repo (#2027). This will be updated to describe `Changed`, `Added` and `Fixed` functionality for each new release. ### Fixed - Displaying sixel images now uses the screen locking API in Tcell, which reduces flickering in the UI (#1943). - The `cmd-history` command is now ignored outside of Normal or Command-line mode, to prevent accidentally escaping out of other modes (#1971). - A potential crash when using the `cmd-delete-word-back` command is fixed (#1976). - The `preserve` option now applies to directories in addition to files when copying. This includes preserving `timestamps` (#1979) and `mode` (#1981). - The `lfrc.ps1.example` example config file is updated to include PowerShell equivalents for the default commands and keybindings (#1989). - Quoting for the `lf` environment variable is fixed for PowerShell users (#1990). - `tempmarks` are no longer cleared after the `sync` command is called (#1996). - The file stat information is no longer displayed during the execution of a `shell-pipe` command even if the file is updated (#2002). - Directories are now reloaded properly if any component in the current path is renamed (#2005). - Write updates for the log file are now ignored when `watch` is enabled. This helps to reduce notification spam and potential of infinite loops (#2015). - Attempting to `cut`/`copy` files into a directory without execute permissions no longer causes `lf` to crash, and an error message will be displayed instead (#2024). ## [r35](https://github.com/gokcehan/lf/releases/tag/r35) ### Added - Support is added for displaying underline styles (#1896). - Support is added for displaying underline colors (#1933). - A new subcommand `files` is added to the `query` server command to list the files in the current directory as displayed in `lf` (#1949). - A new `tty-write` command is added for sending escape sequences to the terminal (#1961). **Writing directly to `/dev/tty` is not recommended as it not synchronized and can interfere with drawing the UI.** ### Fixed - The `trash` command in `lfrc.example` now verifies if the trash directory exists before moving files there (#1918). - `lf` should no longer crash if it fails to open the log file (#1937). - Arrow keys are now handled properly when waiting for a key press after executing a `shell-wait` (`!`) command (#1956). - The `previewer` script is now only invoked for the current directory (instead of all directories), when starting `lf` with `dirpreviews` enabled (#1958). ## [r34](https://github.com/gokcehan/lf/releases/tag/r34) ### Changed - The `autoquit` option is now enabled by default (#1839). ### Added - A new option `locale` is added to sort files based on the collation rules of the provided locale (#1818). **This feature is currently experimental.** - A new `on-init` hook command is added to allow triggering custom actions when `lf` has finished initializing and connecting to the server (#1838). ### Fixed - The background color now renders properly when displaying filenames (#1849). - A bug where the `on-quit` hook command causes an infinite loop has been fixed (#1856). - File sizes now display correctly after being copied when `watch` is enabled (#1881). - Files are now automatically unselected when removed by an external process, when `watch` is enabled (#1901). ## [r33](https://github.com/gokcehan/lf/releases/tag/r33) ### Changed - The `globsearch` option, which previously affected both searching and filtering, now affects only searching. A new `globfilter` option is introduced to enable globs when filtering, and acts independently from `globsearch` (#1650). - The `hidecursorinactive` option is replaced by the `on-focus-gained` and `on-focus-lost` hook commands. These commands can be used to invoke custom behavior when the terminal gains or loses focus (#1763). - The `ruler` option (deprecated in favor of `rulerfmt`) is now removed (#1766). - Line numbers from the `number` and `relativenumber` options are displayed in the main window only, instead of all windows (#1789). ### Added - Support for Unix domain sockets (for communicating with the `lf` server) is added for Windows (#1637). - Color and icon configurations now support the `target` keyword for symbolic links (#1644). - A new option `roundbox` is added to use rounded corners when `drawbox` is enabled (#1653). - A new option `watch` is added to allow using filesystem notifications to detect and display changes to files. This is an alternative to the `period` option, which polls the filesystem periodically for changes (#1667). - Icons can now be colored independently of the filename (#1674). - The `info` option now supports `perm`, `user` and `group` to display the permissions, user and group respectively for each file (#1799). - A new option `showbinds` is added to toggle whether the keybinding hints are shown when a keybinding is partially typed (#1815). ### Fixed - Sorting by extension is fixed for hidden files (#1670). - The `on-quit` hook command is now triggered when the terminal is closed (#1681). - Previews no longer flicker when deleting files (#1691). - Previews no longer flicker when directories are being reloaded (#1697). - `lfcd.nu` now runs properly without raising errors (#1728). - Image previews (composed of ASCII art) containing long lines should now display properly (#1737). - The performance is improved when copying files (#1749). - `lfcd.cmd` now handles directories with special characters (#1772). - Icon colors are no longer clipped when displaying in Windows Terminal (#1777). - The file stat info is now cleared when changing to an empty directory (#1808). - Error messages are cleared when opening files (#1809). ## [r32](https://github.com/gokcehan/lf/releases/tag/r32) ### Changed - The example script `etc/lfcd.cmd` is updated to use the `-print-last-dir` option instead of `-last-dir-path` (#1444). Similar changes have been made for `etc/lfcd.ps1` (#1491), `etc/lfcd.fish` (#1503), and `etc/lfcd.nu` (#1575). - The documentation from `lf -doc` and the `doc` command is now generated from Markdown using `pandoc` (#1474). ### Added - A new option `hidecursorinactive` is added to hide the cursor when the terminal is not focused (#965). - A new special command `on-redraw` is added to be able to run a command when the screen is redrawn or when the terminal is resized (#1479). - Options `cutfmt`, `copyfmt` and `selectfmt` are added to configure the indicator color for cut/copied/selected files respectively (#1540). - `zsh` completion is added for the `lfcd` command (#1564). - The file stat information now falls back to displaying user/group ID if looking up the user/group name fails (#1590). - A new environment variable `lf_mode` is now exported to indicate which mode `lf` is currently running in (#1594). - Default icons are added for Docker Compose files (#1626). ### Fixed - Default value of `rulerfmt` option is now left-padded with spaces to visually separate it from the file stat information (#1437). - Previews should now work for files containing lines with 65536 characters or more (#1447). - Sixel previews should now work when using `lfcd` scripts (#1451). - Colors and icons should now display properly for character device files (#1469). - The selection file is now immediately synced to physical storage after writing to it (#1480). - Timestamps are preserved when moving files across devices (#1482). - Fix crash for `high` and `low` commands when `scrolloff` is set to a large value (#1504). - Documentation is updated with various spelling and grammar fixes (#1518). - Files beginning with a dot (e.g. `.gitignore`) are named correctly after `paste` if another file with the same name already exists (#1525). - Prevent potential race condition when sorting directory contents (#1526). - Signals are now handled properly even after receiving and ignoring `SIGINT` (#1549). - The file stat information should now update properly after using the `cd` command to change to a directory for the first time (#1536). - Previous error messages should now be cleared after a `mark-save`/`mark-remove` operation (#1544). - Fix high CPU usage issue when viewing CryFS filesystems (#1607). - Invalid entries in the `marks` and `tags` files now raise an error message instead of crashing (#1614). - Startup time is improved on Windows (#1617). - Sixel previews are now resized properly when the horizontal size of the preview window changes (#1629). - The cut buffer is only cleared if the `paste` operation succeeds (#1652). - The extension after `.` is ignored to set the cursor position when renaming a directory (#1664). - The option `period` should not cause flickers in sixel previews anymore (#1666). ## [r31](https://github.com/gokcehan/lf/releases/tag/r31) ### Changed - There has been some changes in the server protocol. Make sure to kill the old server process when you update to avoid errors (i.e. `lf -remote 'quit!'`). - A new server command `query` is added to expose internal state to users (#1384). A new builtin command `cmds` is added to display the commands. The old builtin command `jumps` is now removed. The builtin commands `maps` and `cmaps` now use the new server command. - Environment variable exporting for files and options are not performed anymore while previewing and cleaning to avoid various issues with race conditions (#1354). Cleaning program should now instead receive an additional sixth argument for the next file path to be previewed to allow comparisons with the previous file path. User options (i.e. `user_{option}`) are now exported whenever they are changed (#1418). - Command outputs are now exclusively attached to `stderr` to allow printing the last directory or selection to `stdout` (#1399) (#1402). Two new command line options `-print-last-dir` and `-print-selection` are added to print the last directory and selection to `stdout`. The example script `etc/lfcd.sh` is updated to use `-print-last-dir` instead. Other `lfcd` scripts are also likely to be updated in the future to use the new method (patches are welcome). - The option `ruler` is now deprecated in favor of its replacement `rulerfmt` (#1386). The new `rulerfmt` option is more capable (i.e. displays option values, supports colors and attributes, and supports optional fields) and more consistent with the rest of our options. See the documentation for more information. ### Added - Modifier keys (i.e. control, shift, alt) with special keys (e.g. arrows, enter) are now supported for most combinations (#1248). - A new option `borderfmt` is added to configure colors for pane borders (#1251). - New `lf` specific environment variables, `LF_CONFIG_HOME` on Windows and `LF_CONFIG/DATA_HOME` on Unix, are now supported to set the configuration directory (#1253). - Tilde (i.e. `~`) expansion is performed during completion to be able to use expanded tilde paths as command arguments (#1246). - A new option `preserve` is added to preserve attributes (i.e. mode and timestamps) while copying (#1026). - The file `etc/icons.example` is updated for nerd-fonts v3.0.0 (#1271). - A new builtin command `clearmaps` is added to clear all default keybindings except for `read` (i.e. `:`) and `cmap` keybindings to be able to `:quit` (#1286). - A new option `statfmt` is added to configure the status line at the bottom (#1288). - A new option `truncatepct` is added to determine the location of truncation from the beginning in terms of percentage (#1029). - A new option `dupfilefmt` is added to configure the names of duplicate files while copying (#1315). - Shell scripts `etc/lf.nu` and `etc/lfcd.nu` are added to the repository to allow completion and directory change with Nushell (#1341). - Sixels are now supported for previewing (#1211). A new option `sixel` is added to enable this behavior. - A new configuration keyword `setlocal` is added to configure directory specific options (#1381). - A new command line command `cmd-delete-word-back` (default `a-backspace` and `a-backspace2`) is added to use word boundaries when deleting a word backwards (#1409). ### Fixed - Cursor positions in the directory should now be preserved after file operations that changes the directory (e.g. create or delete) (#1247). - Option `reverse` should now respect to sort stability requirements (#1261). - Backspace should not exit `filter` mode anymore (#1269). - Truncated double width characters should not cause misalignment for the file information (#1272). - Piping shell commands should not refresh the preview anymore (#1281). - Cursor position should now update properly after a terminal resize (#1290). - Directories should now be reloaded properly after a `delete` operation (#1292). - Executable file completion should not add entries to the log file anymore (#1307). - Blank input lines are now allowed in piping shell commands (#1308). - Shell commands arguments on Windows should now be quoted properly to fix various issues (#1309). - Reloading in a symlink directory should not follow the symlink anymore (#1327). - Command `load` should not flicker image previews anymore (#1335). - Asynchronous shell commands should now trigger `load` automatically when they are finished (#1345). - Changing the value of `preview` option should now clear image previews (#1350). - Cursor position in the status line at the bottom should now consider double width characters properly (#1348). - Filenames should only be quoted for `cmd` on Windows to avoid quoting issues for PowerShell (#1371). - Inaccessible files should now be included in the directory list and display their `lstat` errors in the status line at the bottom (#1382). - Command line command `cmd-delete-word` should now add the deleted text to the yank buffer (#1409). ## [r30](https://github.com/gokcehan/lf/releases/tag/r30) ### Added - A new builtin command `jumps` is added to display the jump list (#1233). - A new possible field `filter` is added to `ruler` option to display the filter indicator (#1223). ### Fixed - Broken mappings for `bottom` command due to recent changes are fixed (#1240). - Selecting a file does not scroll to bottom anymore (#1222). - Broken builds on some platforms due to recent changes are fixed (#1168). ## [r29](https://github.com/gokcehan/lf/releases/tag/r29) ### Changed - Three new options `cursoractivefmt`, `cursorparentfmt` and `cursorpreviewfmt` have been added (#1086) (#1106). The default style for the preview cursor is changed to underline. You can revert back to the old style with `set cursorpreviewfmt "\033[7m"`. - An alternative boolean option syntax `set option true/false` is added in addition to the previous syntax `set option/nooption` (#758). If you have `set option true` in your configuration, then there is no need for any changes as it was already working as expected accidentally. If you have `set option false` in your configuration, then previously it was enabling the option instead accidentally but now it is disabling the option as intended. Any other syntax including `set option on/off` are now considered errors and result in error messages. Boolean option toggling `set option!` remains unchanged with no new alternative syntax added. - Cursor is now placed at the file extension by default in rename prompts (#1162). - The environment variable `VISUAL` is checked before `EDITOR` for the default editor choice (#1197). ### Added - Mouse wheel events with the Control modifier have been bound to scrolling by default (#1051). - Option values for `tagfmt` and `errorfmt` have been simplified to be able to avoid the reset sequence (#1086). - Two default command line bindings for `` and `` have been added for `cmd-history-next` and `cmd-history-prev` respectively (#1112). - A new command `invert-below` is added to invert all selections below the cursor (#1101). **This feature is currently experimental.** - Two new commands `maps` and `cmaps` have been added to display the current list of bindings (#1146) (#1201). - A new option `numberfmt` is added to customize line numbers (#1177). - A new environment variable `lf_count` is now exported to use the count in shell commands (#1187). - A new environment variable `lf` is now exported to be used as the executable path (#1176). - An example `mkdir` binding is added to the example configuration (#1188). - An example binding to show execution results is added to the example configuration (#1188). - Commands `top` and `bottom` now accepts counts to move to a specific line (#1196). - A new option `ruler` is added to customize the ruler information with a new addition for free disk space (#1168) (#1205). ### Fixed - Example `lfcd` files have been made safer to be able to alias the commands as `lf` (#1049). - Backspace should not exit from `rename:` mode anymore (#1060). - Preview is now refreshed even if the selection does not change (#1074). - Stale directory cache entry is now deleted during rename (#1138). - File information is now updated properly after reloading (#1149). - Window widths are now calculated properly when `drawbox` is enabled (#1150). - Line number widths are now calculated properly when there are exactly 10 entries (#1151). - Preview is not redrawn in async shell commands (#1164). - A small delay is added before showing loading text in preview pane to avoid flickering (#1154). - Hard-coded box drawing characters are replaced with Tcell constants to enable the fallback mechanism (#1170). - Option `relativenumber` now shows zero in the current line (#1171). - Completion is not stuck in an infinite loop anymore when a match is longer than the window width (#1183). - Completion now inserts the longest match even if there is no word before the cursor (#1184). - Command `doc` should now work even if `lf` is not in the `PATH` variable (#1176). - Directory option changes should not crash the program anymore (#1204). - Option `selmode` is now validated for the accepted values (#1206). ## [r28](https://github.com/gokcehan/lf/releases/tag/r28) ### Changed - Extension matching for colors and icons are now case insensitive (#908). ### Added - Three new commands `high`, `middle`, and `low` are added to move the current selection relative to the screen (#824). - Backspace on empty prompt now switches to Normal mode (#836). - A new `history` option is now added to be able to disable history (#866). - A new special expansion `%S` spacer is added for `promptfmt` to be able to right align parts (#867). - A new command-line command `cmd-menu-accept` is now added to accept the currently selected match (#934). - Command-line commands should now be shown in completion for `map` and `cmap` (#934). - Italic escape codes should now be working in previews (#936). - Position and size information are now also passed to the `cleaner` script as arguments (#945). - A new option `dirpreviews` is now added to also pass directories to the `previewer` script (#842). - A new option `selmode` is now added to be able to limit the selection to the current directory (#849). - User defined options with `user_` prefix are now supported (#865). - Adding or removing `$`/`%`/`!`/`&` characters in `:` mode should now change the mode accordingly (#960). - A new special command `on-select` is now added to be able to run a command after the selection changes (#864). - Mouse support is extended to be able to click filenames for selection and opening (#963). - Two new environment variables `lf_width` and `lf_height` are now exported for shell commands. ### Fixed - Option `tagfmt` can now be changed properly. - User name, group name, and link count should now be displayed as before where available (#829). - Tagging files with colons in their names should now work as expected (#857). - Some multibyte characters should now be handled properly for completion (#934). - Menu completion for a file in a subdirectory should now be working properly (#934). - File completion should now be escaped properly in menu completion (#934). - First use of `cmd-menu-complete-back` should now select the last completion as expected (#934). - Broken symlinks should now be working properly in completion (#934). - Files with stat errors should now be skipped properly in completion (#934). - Empty search with `incsearch` option should now be handled properly (#944). - History position is now also reset when leaving the command line (#953). - Mouse drag events are now ignored properly to avoid command repetition (#962). - Environment variables `HOME` and `USER` should now be used as fallback for locations on some systems (#972). - File information is now displayed in the status line at first launch when there are no errors in the configuration file (#994). ## [r27](https://github.com/gokcehan/lf/releases/tag/r27) ### Changed - Creation of log files are now disabled by default. Instead, a new command line option `-log` is provided. - `copy` selections are now kept after `paste` (#745). You can use `map p :paste; clear` to get the old behavior. - The socket file is now created in `XDG_RUNTIME_DIR` when set, with a fallback to the temporary directory otherwise. - Directory counting with `dircounts` option is moved from UI drawing to directory reading to be run asynchronously without locking the UI. With this change, manual `reload` commands might be necessary when `dircounts` is changed at runtime. Indicators for errors are changed to `!` instead of `?` to distinguish them from missing values. - The default icons are now replaced with ASCII characters to avoid font issues. ### Added - Files and options are now exported for `previewer` and `cleaner` scripts. For `cleaner` scripts, this can be used to detect if the file selection is changed or not (e.g. `$1 == $f`) and act accordingly (e.g. skip cleaning). - A new `tempmarks` option is added to set some marks as temporary (#744). - The pattern `*filename` is added for colors and icons. - A new `calcdirsize` command is added to calculate directory sizes (#750). - Two new options `infotimefmtnew` and `infotimefmtold` are added to configure the time format used in `info` (#751). - Two new commands `jump-next` (default `]`) and `jump-prev` (default `[`) are added to navigate the jumplist (#755). - Colors and icons file support is now added to be able to configure without environment variables. Example colors and icons files are added to the repository under `etc` directory. See the documentation for more information. - For Windows, an example `open` command is now provided in the PowerShell example configuration (#765). - Two new commands `scroll-up` (default ``) and `scroll-down` (default ``) are added to be able to scroll the file list without moving (#764). - A new special command `on-quit` is added to be able to run a command before quitting. - Two new commands `tag` and `tag-toggle` (default `t`) are now added to be able to tag files (#791). ### Fixed - `Chmod` calls in the codebase are now removed to avoid TOC/TOU exploits. Instead, file permissions are now set at file creation. - Socket and log files are now created with only user permissions. - On Windows, `PWD` variable is now quoted properly. - Shell commands `%` and `&` are now run in a separate process group (#753). - Navigation initialization is now delayed after the evaluation of configuration files to avoid startup races and redundant loadings (#759). - The error message shown when the current working directory does not exist at startup is made more clear. - Trailing slashes in `PWD` variable are now handled properly. - Files with `stat` errors are now skipped while reading directories. ## [r26](https://github.com/gokcehan/lf/releases/tag/r26) ### Fixed - On Windows, input handling is properly resumed after shell commands. ## [r25](https://github.com/gokcehan/lf/releases/tag/r25) ### Added - A new `dironly` option is added to only show directories and hide regular files (#669). - A new `dircache` option is added to disable caching of directories (#673). - Two new commands `filter` and `setfilter` is added along with a new option `incfilter` and a `promptfmt` expansion `%F` to implement directory filtering feature (#667). - A new special command `pre-cd` is added to run a command before a directory is changed (#685). - `cmap` command now accepts all expressions similar to `map` (#686). ### Fixed - Marking a symlink directory should now save the symlink path instead of the target path (#659). - A number of crashes have been fixed when the `hidden` option is changed. ## [r24](https://github.com/gokcehan/lf/releases/tag/r24) ### Fixed - Data directory is automatically created before the selection file is written. - An error is returned for remote commands when the given ID is not connected to the server. - Prompts longer than the width should not crash the program anymore. ## [r23](https://github.com/gokcehan/lf/releases/tag/r23) ### Changed - There has been some changes in the server protocol. Make sure to kill the old server process when you update to avoid errors. - Server `load` and `save` commands are now removed. Instead a local file is used to record file selections (e.g. `~/.local/share/lf/files`). See the documentation for more information. - Clients are now disconnected from server on quit. The old server `quit` command is renamed to `quit!` to act as a force quit by closing connected client connections first. A new `quit` command is added to only quit when there are no connected clients left. ### Added - A new `autoquit` option is added to automatically quit the server when there are no connected clients left. This option is disabled by default to keep the old behavior. This is added as an option to avoid respawning server repeatedly when there is often a single client involved but more clients are spawned from time to time. - A new `-single` command line flag is added to avoid spawning and/or connecting to server on startup. Remote commands would not work in this case as the client does not connect to a server. Local versions of internal `load` and `sync` commands are implemented properly. - Errors for remote commands are now also shown in the output in addition to the server log file. - Bright ANSI color escape codes (i.e. 90-97 and 100-107) are now supported. ### Fixed - Lookahead size for escape codes are increased to recognize longer escape codes used in some image previewers. - The file preview cache is invalidated when the terminal height changes to fill the screen properly. - The file preview cache is invalidated when the `drawbox` option changes and true image previews should be triggered to be drawn at updated positions. - A crash scenario is fixed when `hidden` option is changed. - Pane widths should now be calculated properly when big numbers are used in `ratios` (#622). - The special bookmark `'` is now preserved properly after `sync` commands (#624). - On some platforms, a bug has been fixed on the Tcell side to avoid an extra key press after terminal suspend/resume and the Tcell version used in `lf` is bumped accordingly to include the fix. - The prompt line should now scroll accordingly when the text is wider than the screen. - Text width in the prompt line should now be calculated properly when non-ASCII characters are involved. - Erase line escape codes (i.e. `\033[K`) used in some command outputs should now be ignored properly. ## [r22](https://github.com/gokcehan/lf/releases/tag/r22) ### Added - A new `-config` command line flag is added to use a custom config file path (#587). - The current working directory is now exported as `PWD` environment variable (#591). Subshells in symlink directories should now start in their own paths properly. - The initial working directory is now exported as `OLDPWD` environment variable. - A new `shellflag` option is added to customize the shell flag used for passing commands (i.e. default `-c` for Unix and `/c` for Windows). - Using the command `cmd-enter` during `find` and `find-back` now jumps to the first match (#605). - A new `waitmsg` option is added to customize the prompt message after `shell-wait` commands (i.e. default `Press any key to continue`) (#604). ### Fixed - A regression bug is fixed to print a newline in the prompt message properly after `shell-wait` commands. - A regression bug is fixed to avoid CPU stuck at 100% when the terminal is closed unexpectedly. - A regression bug is fixed to make shell commands use the alternate screen properly and keep the terminal history after quitting. - Enter keypad terminfo sequence is now sent on startup so the `delete` key should be recognized properly in `st` terminal. ## [r21](https://github.com/gokcehan/lf/releases/tag/r21) ### Changed - `cut` and `copy` do not follow symlinks anymore. Broken symlinks can now be selected for the `cut` and `copy` commands (#581). ### Added - User name, group name, and hard link counts are now shown in the status line at the bottom when available. - Number of selected, copied, and cut files are now shown in the ruler at the bottom when they are non-zero. - Hard-coded shell commands with `stty` (Unix) and `pause` (Windows) to implement the `Press any key to continue` behavior are now implemented properly with a Go terminal handling library. With this change, the requirement for a POSIX compatible shell for `shell` option is now dropped and other shells can be used. ### Fixed - A longstanding issue regarding UI suspend/resume for shell commands in macOS is now fixed in Tcell. - Renaming a symlink to its target or a symlink to another with the same target should now be handled properly (#581). - Autocompletion in a directory containing a broken symlink should now work as intended (#581). - Setting `shellopts` to empty in the configuration file should not pass an extra empty argument to shell commands anymore. - Previously given tip to trap `SIGPIPE` in the preview script to enable caching is now updated in the documentation. Trapping the signal in the preview script avoids sending the signal to the program when enough lines are read. This may result in reading redundant lines especially for big files. The recommended method is now to add a trailing `|| true` to each command exiting with a non-zero return code after a `SIGPIPE`. ## [r20](https://github.com/gokcehan/lf/releases/tag/r20) ### Added - A new `mouse` option is added to enable mouse events. This option is disabled by default to leave mouse events to the terminal. Also unbound mouse events when `mouse` is enabled should now show an `unknown mapping` error in the message line. ### Fixed - Newline characters in the output of `%` commands should no longer shift the content up which was a bug introduced in the previous release due to a fix to handle combining characters in texts. - Redundant preview loadings for the `search` and `find` commands are now avoided (#569). - Scanner now only considers ASCII characters for spaces and digits which should avoid unexpected splits in some non-ASCII inputs. ## [r19](https://github.com/gokcehan/lf/releases/tag/r19) ### Changed - Changes have been made to enable the use of true image previews. See the documentation and the previews wiki page for more information. - Non-zero exit codes should now make the preview volatile to avoid caching. Programs that may not behave well to `SIGPIPE` may trigger this behavior unintentionally. You may trap `SIGPIPE` in your preview script to get the old behavior. - Preview scripts should now get as arguments the current file path, width, height, horizontal position, and vertical position. Note that height is still passed as an argument but its order is changed. - A new `cleaner` option is added to set the path to a file to be executed when the preview is changed. - Redundant preview loadings for movement commands are now avoided. - Expansion `%w` in `promptfmt` is changed back to its old behavior without a trailing separator. Instead, a new expansion `%d` is added with a trailing separator (#545). Expansion `%w` is meant to be used to display the current working directory, whereas `%d%f` is meant to be used to display the current file. - A new `LF_COLORS` environment variable is now checked to be able to make `lf` specific configurations. Also, environment variables for colors are now read cumulatively starting from the default behavior (i.e. default, `LSCOLORS`, `LS_COLORS`, `LF_COLORS`). ### Added - Full path, dir name, file name, and base name matching patterns are added to colors and icons. See the updated documentation for more information. - PowerShell keybinding example has been added to `etc/lfcd.ps1` (#532). - PowerShell autocompletion script has been added as `etc/lf.ps1` (#535). - Multiple `-command` flags can now be given (#552). - Basic mouse support has been added. Mouse buttons (e.g. `` for primary button, `` for secondary button, `` for middle button etc.) and mouse wheels (e.g. `` for wheel up, `` for wheel down etc.) can be used in bindings. - Commands `top` and `bottom` are now allowed in `cmap` mappings in addition to movement commands. ### Fixed - Extension sorting should now handle extensions with different lengths properly (#539). - Heuristic used to show `info` should now take into account the `number` and `icons` options properly. - The environment variable `id` is now set to the process ID instead to avoid two clients getting the same ID when launched at the same time (#550). - Unicode combining characters in texts should now be displayed properly. ## [r18](https://github.com/gokcehan/lf/releases/tag/r18) ### Changed - The `ignorecase` and `ignoredia` options should now also apply to sorting in addition to searching. - The `ignoredia` option is now enabled by default to be consistent with `ignorecase`. - The terminal UI library Tcell has been updated to version 2. This version highlights adding 24-bit true colors on Windows and better support for colors on Unix. The environment variable `TCELL_TRUECOLOR` is not required anymore so that terminal themes and true colors can be used at the same time. - The deprecated option `color256` is now removed. ### Added - Two new command line commands `cmd-menu-complete` and `cmd-menu-complete-back` are added for completion menu cycling (#482). - Simple configuration files for Windows `etc/lfrc.cmd.example` and `etc/lfrc.ps1.example` are now added to the repository. - Bash completion script `etc/lf.bash` is now added to the repository. - Time formats in `info` option should now show the year instead of `hh:mm` for times older than the current year. ### Fixed - Signals `SIGHUP`, `SIGQUIT`, and `SIGTERM` should now quit the program properly. - Setting `info` to an empty value should not print errors to the log file anymore. - Natural sorting is optimized to work faster using less memory. - Files and directories that incorrectly show modification times in the future (e.g. Linux builtin exFAT driver) should not cause CPU hogging anymore. - The keybinding example in `etc/lfcd.fish` is now updated to avoid hanging in shell commands. - Using the `bottom` command immediately after startup should not crash the program anymore. - Changing sorting options during sorting operations should not crash the program anymore. - Output in `shell-pipe` commands now uses lazy redrawing so that verbose commands should not block the program anymore. - The server is now daemonized properly on Unix so that it is not killed anymore when the controlling terminal is killed (#517). ## [r17](https://github.com/gokcehan/lf/releases/tag/r17) ### Changed - The terminal UI library has been changed from Termbox to Tcell as the former has been unmaintained for a while (#439). Some of the changes are listed below, though the list may not be complete as this is a relatively big change. - Some special key names are changed to be consistent with the Tcell documentation (e.g. `` renamed to ``). On the other hand, there are also additional keybindings that were not available before (e.g. `` for Shift+Tab). You can either check the Tcell documentation for the list of keys or hit the key combination in `lf` to read the name of the key from the `unknown mapping` error message. - 24-bit true colors are now supported on Unix systems. See the updated documentation for more information. There is an ongoing version 2.0 of Tcell in development that we plan to switch to once it becomes stable and it is expected to add support for true colors in Windows consoles as well. - Additional platforms are now supported and the list of pre-built binaries provided are updated accordingly. - Wide characters are now displayed properly in Windows consoles. ### Added - Descriptions of commands and options are now added to the documentation. Undocumented behaviors should now be considered documentation bugs and they can be reported. - Keys are now evaluated with a lazy drawing approach so `push` commands to set the prompt and pasting something to the command line should feel instantaneous. ### Fixed - Corrupted history files should no longer crash the program. - The server now only listens connections from `localhost` on Windows so firewall permissions are not required anymore. - `push` commands that change the operation mode should now work consistently as expected. - Loading directories should now display the previous file list if any, which was a regression due to a bug fix in a previous release. - `shell-pipe` commands should now automatically update previews when necessary. - Errors from failed shell commands should not be overwritten by file information anymore. - The server can now also be started automatically when the program is called with a relative path, which was a regression due to a bug fix in a previous release (#463). - Environment variables are now exported automatically for preview scripts without having to call a shell command first (#468). - The `` key can now be bound to be used on its own, instead of escaping a keybinding combination, which was a regression due to a bug fix in a previous release (#475). - Changing the `hiddenfiles` option should now automatically trigger directory updates when necessary. ## [r16](https://github.com/gokcehan/lf/releases/tag/r16) ### Added - Option values are now available in shell commands as environment variables with a prefix of `lf_` (e.g. `$lf_hidden`, `$lf_ratios`) (#448). ### Fixed - Directories containing internal Windows links that show permission denied errors should now display properly. ## [r15](https://github.com/gokcehan/lf/releases/tag/r15) ### Changed - The `toggle` command does not move the selection down anymore. The default binding for `` is now assigned to `:toggle; down` instead to keep the default behavior same as before. - The expansion `%w` in option `promptfmt` should now have a trailing slash. The default value of `promptfmt` is now changed accordingly, and should not display double slashes in the root directory anymore. - The key `` is now used as the escape key. It should not display an error message when used to cancel a keybinding menu as before. However, it is not possible to bind `` key to another command anymore. ### Added - Symbolic link destinations are now shown in the bottom status line (#374). - A new `hiddenfiles` option which takes a list of globs is implemented to customize which files should be `hidden` (#372). - Expressions consisting of multiple commands can now use counts (#394). - Moving operations now fall back to copy and then delete strategy automatically for cross-device linking. - The `hidden` option now works in Windows. - The `toggle` command can now take optional arguments to toggle given names instead of the current file (#409). - A new option `truncatechar` is implemented to customize the truncate character used in long filenames (#417). - Copy and move operations now display a success message when they are finished (#427). ### Fixed - `SIGHUP` and `SIGTERM` signals are now properly handled. Log files should not remain when terminals are directly closed (#305). - The `info` option should now align properly when used with the `number` and `relativenumber` options (#373). - Tilde (`~`) is now only expanded at the beginning of the path for the `cd` and `select` commands (#373). - The `rename` command should now work properly with names differing only cases on case-insensitive filesystems. - Tab characters are now expanded to spaces in Windows. - The `incsearch` option now respects the search direction accordingly. - The server is now started in the home folder and will not hold mounted filesystems busy. - Trailing spaces in configuration files do not confuse the parser anymore. - Termbox version is updated to fix a keyboard problem in FreeBSD (#404). - Async commands do not leave zombie processes anymore (#407). - The `hidden` option now works consistently as expected when set at the initial launch. - The `rename` command should now select the new file after the operation. - The `rename` command should now handle absolute paths properly. - The `select` command should now work properly on loading directories. Custom commands that select a file after an operation should now work properly without an explicit `load` operation beforehand. - Previous errors in the bottom message line should not persist through the prompt usage anymore. - The `push` command should not fail with non-ASCII characters anymore. - The `select` command should not fail with broken links anymore. - The `load` command should not clear toggled broken links anymore. - Copy and move operations do not overwrite broken links anymore. ## [r14](https://github.com/gokcehan/lf/releases/tag/r14) ### Added - The `delete` command now shows a prompt with the current filename or the number of selected files (#206). - Backslash can now be escaped with a backslash even without quotes. - A new desktop entry file `lf.desktop` is added (#222). - Three new `sortby` types are added, access time (i.e. `atime`), change time (i.e. `ctime`) (#226), and extension (i.e. `ext`) (#230). New default keybindings are added for these sorts correspondingly (i.e. `sa`, `sc`, and `se`). The `info` option can now also contain `atime` and `ctime` values accordingly. - A new shell completion for `zsh` is added to `etc/lf.zsh` (#239). - The `delete` command now works asynchronously and shows the progress (#238). - Completion and directory change scripts are added for `csh` and `tcsh` as `etc/lf.csh` and `etc/lfcd.csh` respectively (#264). - A new special command `on-cd` is added to run a shell command when the directory is changed. See the documentation for more information (#291). ### Fixed - Some directories with special permissions that previously show a file icon now shows a directory icon properly. - The `etc/lfcd.cmd` script can now also change to a different volume drive (#221). - The proper use of `setsid` for opening files is now added to the example configuration and the documentation. - The home directory abbreviation `~` is now only applied properly to paths starting with the home directory (#241). - The `rename` command now cancels the operation if the old and new paths are the same (#266). - Autocompletion and word movements should now work properly with all Unicode characters. - The `shell-pipe` command which was broken some time ago should now work as expected. - The `$TERM` environment variable can now work with values containing `tmux` with custom `$TERMINFO` values. @doronbehar now maintains a Termbox fork for `lf` (https://github.com/doronbehar/termbox-go). ## [r13](https://github.com/gokcehan/lf/releases/tag/r13) ### Added - A new `wrapscroll` option is added to wrap top and bottom while scrolling (#166). - The `up`, `down` movement commands and their variants, `updir`, and `open` are now allowed in `cmap` mappings. - Two new `glob-select` and `glob-unselect` commands are added to use globbing for toggling files (#184). - A new `mark-remove` (default `"`) command is added to allow removing marks (#190). - Icon support is added with the `icon` option. See the wiki page for more details. - A new builtin `rename` command is added (#197). ### Fixed - The `cmd-history-next` command now remains in Command-line mode after the last item (#168). - The `select` command does not change directories anymore when used on a directory. - The working directory is now changed to the first argument when it is a directory. - The `ratios` option is now checked before `preview` to avoid crashes (#174). - Previous error messages are now cleared after successful commands (#192). - Symlink to directories are now colored as symlinks (#195). - Permission errors for directories are now displayed properly instead of showing as empty (#203). ## [r12](https://github.com/gokcehan/lf/releases/tag/r12) ### Added - Go modules replaced `godep` for dependency management. Package maintainers may need to update accordingly. - A new `errorfmt` option is added to customize the colors and attributes of error messages. ### Fixed - Autocompletion for searches now complete filenames instead of commands. - Permanent environment variables (e.g. `$id`, `$EDITOR`, `$LF_LEVEL`) are now exported on startup so they can be used in preview scripts without running a shell command first. - On Windows, quotes are added to the exported values `$f`, `$fs`, and `$fx` to handle filenames with spaces properly. - On Windows, filenames starting with `.` characters are now shown to avoid crashes when filenames show up as empty. ## [r11](https://github.com/gokcehan/lf/releases/tag/r11) ### Changed - Copy and move operations are now implemented as builtins instead of using the underlying shell primitives (i.e. `cp` and `mv`). Users who want the old behavior can define a custom `paste` command. See the updated documentation for more information. Please report bugs regarding this change. - Preview messages (i.e. `empty`, `binary`, and `loading...`) are now shown with the reverse attribute. ### Added - Copy and move operations now run asynchronously and the progress is shown in the bottom ruler. - Two new commands `echomsg` and `echoerr` are added to print a message to the message line and to the log file at the same time. ### Fixed - Terminal initialization errors are now shown in the terminal instead of the log file. ## [r10](https://github.com/gokcehan/lf/releases/tag/r10) ### Changed - The ability to map Normal mode commands in `cmap` is removed. This has caused a number of bugs in the previous release. A different mechanism for a similar functionality is planned. ### Added - A new command line flag `-command` has been added to execute a command on client initialization (#135). - A `select` command is now executed after initialization if the first command line argument is a file. - A prompting mechanism has been added to the builtin `delete` command. ### Fixed - Input and output in `shell-pipe` commands were broken with the `cmap` patch. This should now work as before. - Some `push` commands were broken with the `cmap` patch and sometimes ignored Command-line mode for some keys to execute as in Normal mode. This should now work as before. - `read` and shell commands should now also work when typed manually (e.g. typing `:shell` should switch the prefix to `$`). - Configuration files are now read after initialization. - Background colors are removed from defaults to avoid confusion with selection highlighting. ## [r9](https://github.com/gokcehan/lf/releases/tag/r9) ### Changed - The default number of colors is set to 8 to have better defaults in some terminals. A new option `color256` is added to use 256 colors instead. Users who want the old behavior should enable this option in their configuration files. ### Added - A new `incsearch` option is added to enable incremental matching while searching. - Two new options `ignoredia` and `smartdia` are added to ignore diacritics in Latin letters for `search` and `find` (#118). - A new builtin `delete` command is added for file deletion (#121). This command is not assigned to a key by default to prevent accidental deletions. In the future, a prompting mechanism may be added to this command for more safety. - Normal mode commands can now be used in `cmap` which can be used to immediately finish Command-line mode and execute a Normal mode command afterwards. - A new `fish` completion script is added to the `etc` folder (#131). - Two new options `number` and `relativenumber` are added to enable line numbers in directories (#133). ### Fixed - Autocompletion should now show only a single match for redefined builtin commands. ## [r8](https://github.com/gokcehan/lf/releases/tag/r8) ### Added - Four new commands `find`, `find-back`, `find-next`, and `find-prev` are added to implement file finding. Two options `anchorfind` and `findlen` are added to customize the behavior of these commands. - A new `quit` command is added to the server protocol to quit the server. - A new `$LF_LEVEL` environment variable is added to show the nesting level. ### Fixed - The `load` and `reload` commands now work properly when the current directory is deleted. Also `lf` does not start in deleted directories anymore. - The server is now started as a detached process in Windows so its lifetime is not tied to the command line window anymore. - Clients now try to reconnect to the server at startup with exponentially increasing intervals when they fail. This is to avoid connection failures due to the server not being ready for the first client that automatically starts the server. - The old index is now kept when the current selection is deleted. - The `shell-pipe` command now triggers `load` instead of `reload`. - Error messages are now more informative when `lf` fails to start due to either `$HOME` or `$USER` variables being empty or not set. - Searching for the next/previous item is now based on the direction of the initial search. ## [r7](https://github.com/gokcehan/lf/releases/tag/r7) ### Changed - The system-wide configuration path on Unix is changed from `/etc/lfrc` to `/etc/lf/lfrc`. ### Added - A man page is now automatically generated from the documentation which can be installed to make the documentation available with the `man` command. On a related note, there is now a packaging guide section in packages wiki page. - A new `doc` command (default ``) is added to view the documentation in a pager. - Commands `mark-save` (default `m`) and `mark-load` (default `'`) are added to implement builtin bookmarks. Marks are saved in a file in the data folder which can be found in the documentation. - The history is now saved in a file in the data folder which can be found in the documentation. ## [r6](https://github.com/gokcehan/lf/releases/tag/r6) ### Changed - The `yank`, `delete`, and `put` commands are renamed to `copy`, `cut`, and `paste` respectively. In the example configuration, the `remove` command is renamed to `delete`. - The special command `open-file` to configure file opening is renamed to `open`. ### Added - A new option `shellopts` is added to be able to pass command line arguments to the shell interpreter (i.e. ` -c -- `) which is useful to set safety options for all shell commands (i.e. `sh -eu ...`). See the example configuration file for more information. - The special keys ``, ``, ``, and `` are mapped to the `top`, `bottom`, `page-up`, and `page-down` commands respectively by default. - A new command `source` is added to read a configuration file. - Support is added to read a system-wide configuration file on startup located in `/etc/lfrc` on Unix and `C:\ProgramData\lf\lfrc` on Windows. The documentation is updated to show the locations of all configuration files. - Environment variables used for configuration (i.e. `$EDITOR`, `$PAGER`, `$SHELL`) are set to their default values when they are not set or empty and they are exported to shell commands. - A new environment variable `$OPENER` is added to configure the default file opener using the previous default values and it is exported to shell commands. ### Fixed - Executable completion now works on Windows as well. ## [r5](https://github.com/gokcehan/lf/releases/tag/r5) ### Added - The server is automatically restarted on startup if it does not work anymore. - A new option `period` is added to set time duration in seconds for periodic refreshes. Setting the value of this option to zero disables periodic refreshes which is the default behavior. - A new command `load` is added to refresh only modified files and directories which is more efficient than `reload` command. ### Fixed - `cmd-word-back` does not change the command line anymore. - Modified files and directories are automatically detected and refreshed when they are loaded from cache. - All clients are now refreshed when the `put` command is used. - The correct hidden parent is selected when the `hidden` option is changed. - The preview is properly updated when the `hidden` option is changed. ## [r4](https://github.com/gokcehan/lf/releases/tag/r4) ### Changed - The following commands are renamed for clarity and consistency: - `bot` is renamed to `bottom` - `cmd-delete-word` is renamed to `cmd-delete-unix-word` - `cmd-beg` is renamed to `cmd-home` - `cmd-delete-beg` is renamed to `cmd-delete-home` - `cmd-comp` is renamed to `cmd-complete` - `cmd-hist-next` is renamed to `cmd-history-next` - `cmd-hist-prev` is renamed to `cmd-history-prev` - `cmd-put` is renamed to `cmd-yank` ### Added - Support for alt key bindings have been added using the commonly used escape delaying mechanism. The delay value is set to 100ms which is also used for other escape codes in Termbox. Keys are named with an `a` prefix, as in `` for the `alt` and `f` keys. Also note that the old mechanism for alt keybindings on 8-bit terminals still works as before. - The following command line commands and their default alt keybindings have been added: - `cmd-word` with `` - `cmd-word-back` with `` - `cmd-capitalize-word` with `` - `cmd-delete-word` with `` - `cmd-uppercase-word` with `` - `cmd-lowercase-word` with `` - `cmd-transpose-word` with `` ### Fixed - The default editor, pager, and opener commands should now work in Windows. Opener still only works with paths without spaces though. - 8-bit color codes and attributes are not confused anymore. - History selection is disabled when a `shell-pipe` command is running. - Searches are now excluded from the history. ## [r3](https://github.com/gokcehan/lf/releases/tag/r3) ### Changed - Command counts are now only applied for the `up`/`down` (and variants), `updir`, `toggle`, `search-next`, and `search-prev` commands. These commands are now handled more efficiently when used with counts. ### Added - Pressed keys are now shown in the ruler when they are not matched yet. - A new builtin `draw` command has been added which is more efficient than the `redraw` command. The latter is replaced with the former in many places to prevent flickers on the screen. - Support for the `$LS_COLORS` and `$LSCOLORS` environment variables are added for color customization (#96). See the updated documentation for more information. - A new option `drawbox` is added to draw a box around panes. ### Fixed - Resize events that change the height are now handled properly. - Changes in sorting methods and options are checked for cached directories and these directories are sorted again if necessary while loading. - A `~` character is added as a suffix to file names when they do not fit in the window. ## [r2](https://github.com/gokcehan/lf/releases/tag/r2) ### Changed - Shell command names are shortened (e.g. `read-shell-wait` is renamed to `shell-wait`). ### Added - A new shell command type named `shell-pipe` is introduced that runs with the UI. See the updated documentation for the motivation and some example use cases. - A new command named `cmd-interrupt` (default ``) is introduced to interrupt the current `shell-pipe` command. - A new command named `select` is introduced that changes the current file selection to its argument. ### Fixed - Running `cmd-hist-prev` in Normal mode now always starts with the last item to avoid confusion. Running `cmd-hist-next` in Normal mode now has no effect for consistency. ## [r1](https://github.com/gokcehan/lf/releases/tag/r1) ### Added - Initial release ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing Code contributions are always welcomed in lf. If you are going to introduce a new feature, it is best to open an issue first for discussion. If your feature can be implemented as a configuration option **please add it to the [wiki](https://github.com/gokcehan/lf/wiki)**. For bug fixes, you can simply send a pull request. ## Code conventions In addition to `gofmt` and friends (e.g. [go vet](https://pkg.go.dev/cmd/vet), [staticcheck](https://staticcheck.dev/), [golangci-lint](https://golangci-lint.run/)), we have a few conventions: - Global variables are best avoided except when they are not. Global variable names are prefixed with `g` as in `gFooBar`. Exceptions are variables holding values of environmental variables which are prefixed with `env` as in `envFooBar` and regular expressions which are prefixed with `re` as in `reFooBar` when they are global. - Type and function names are small case as in `fooBar` since we don't use exporting. - For file name variables, `name`, `fname`, or `filename` should refer to the base name of the file as in `baz.txt`, and `path`, `fpath`, or `filepath` should refer to the full path of the file as in `/foo/bar/baz.txt`. - Run `go fmt` to ensure that files are formatted correctly. - Consider using [conventional](https://www.conventionalcommits.org/) commit messages. Use the surrounding code as reference when in doubt as usual. ## Adding a new option Adding a new option usually requires the following steps: - Add option name/type to `gOpts` struct in `opts.go` - Add default option value to `init` function in `opts.go` - Add option evaluation logic to `setExpr.eval` in `eval.go` - Implement the option somewhere in the code - Add option name and its default value to `Quick Reference` and `Options` sections in `doc.md` - Run `gen/doc.sh` to update the documentation (optional as it requires `docker`/`podman`, but appreciated) - Commit your changes and send a pull request Options should be defined in alphabetical order, but note that boolean options are defined first in `eval.go` as they require special handling. ## Adding a new builtin command Adding a new command usually requires the following steps: - Add default key if any to `init` function in `opts.go` - Add command evaluation logic to `callExpr.eval` in `eval.go` - Implement the command somewhere in the code - Add command name to `gCmdWords` in `complete.go` for tab completion - Add command name to `Quick Reference` and `Commands` sections in `doc.md` - Run `gen/doc.sh` to update the documentation (optional as it requires `docker`/`podman`, but appreciated) - Commit your changes and send a pull request Commands should be defined in alphabetical order, but note that commands are first organized roughly into the following sections in `eval.go` for clarity: - Navigation - Selection - File-related operations - Shell commands - Finding and searching - Filtering - Marks - Tags - Echoing - Miscellaneous commands - Visual mode - Command-line mode commands - Hook commands ## Platform specific code There are two files named `os.go` and `os_windows.go` for Unix and Windows specific code respectively. If you add something to either of these files but not the other, you probably break the build for the other platform. If your addition works the same in both platforms, your addition probably belongs to `main.go` instead. There are also different variants of the `df` functionality provided by `df_openbsd.go`, `df_statfs.go`, `df_statvfs.go` and `df_windows.go`. Where applicable, ensure that any changes you make are reflected across all of these files for consistency. ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2016 Gökçehan Kara Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # LF [Doc](doc.md) | [Wiki](https://github.com/gokcehan/lf/wiki) | [#lf:matrix.org](https://matrix.to/#/#lf:matrix.org) (with IRC bridge) [![Go Build](https://github.com/gokcehan/lf/actions/workflows/go.yml/badge.svg)](https://github.com/gokcehan/lf/actions/workflows/go.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/gokcehan/lf)](https://goreportcard.com/report/github.com/gokcehan/lf) `lf` (as in "list files") is a terminal file manager written in Go with a heavy inspiration from [`ranger`](https://github.com/ranger/ranger) file manager. See [faq](https://github.com/gokcehan/lf/wiki/FAQ) for more information and [tutorial](https://github.com/gokcehan/lf/wiki/Tutorial) for a gentle introduction with screencasts. ![icons-and-border](https://github.com/user-attachments/assets/d5623462-05ab-4921-aeb3-d377d4732f9e) ![image-preview](https://github.com/user-attachments/assets/9ff42a21-19dd-42fa-8407-5d055aa9d561) ![new-features](https://github.com/user-attachments/assets/2a9a32aa-a764-438f-a810-e687e979dcee) ## Features - Cross-platform (Linux, macOS, BSDs, Windows) - Single binary without any runtime dependencies - Fast startup and low memory footprint due to native code and static binaries - Asynchronous IO operations to avoid UI locking - Server/client architecture and remote commands to manage multiple instances - Extendable and configurable with shell commands - Customizable keybindings (vi and readline defaults) - A reasonable set of other features (see the [documentation](doc.md)) ## Non-Features - Tabs or windows (better handled by window manager or terminal multiplexer) - Builtin pager/editor (better handled by your pager/editor of choice) - Builtin commands for file operations (better handled by the underlying shell tools including but not limited to `mkdir`, `touch`, `chmod`, `chown`, `chgrp`, and `ln`) ## Installation See [packages](https://github.com/gokcehan/lf/wiki/Packages) for community maintained packages. See [releases](https://github.com/gokcehan/lf/releases) for pre-built binaries. Building from the source requires [Go](https://go.dev/). On Unix: ```bash env CGO_ENABLED=0 go install -ldflags="-s -w" github.com/gokcehan/lf@latest ``` On Windows `cmd`: ```cmd set CGO_ENABLED=0 go install -ldflags="-s -w" github.com/gokcehan/lf@latest ``` On Windows `PowerShell`: ```powershell $env:CGO_ENABLED = '0' go install -ldflags="-s -w" github.com/gokcehan/lf@latest ``` ## Usage After the installation `lf` command should start the application in the current directory. Run `lf -help` to see [command line options](doc.md#options). Run `lf -doc` to see the [documentation](doc.md). See [etc](etc) directory to integrate `lf` to your shell and/or editor. Example configuration files along with example colors and icons files can also be found in this directory. See [integrations](https://github.com/gokcehan/lf/wiki/Integrations) to integrate `lf` to other tools. See [tips](https://github.com/gokcehan/lf/wiki/Tips) for more examples. ## Contributing See [contributing](CONTRIBUTING.md) for guidelines. ================================================ FILE: app.go ================================================ package main import ( "bufio" "cmp" "errors" "fmt" "io" "io/fs" "log" "os" "os/exec" "os/signal" "path/filepath" "slices" "strings" "syscall" "time" ) type app struct { ui *ui // ui state (screen, windows, input) nav *nav // navigation state (dirs, cursor, selections, preview, caches) ticker *time.Ticker // refresh ticker if `period` > 0 quitChan chan struct{} // signals main loop to exit cmd *exec.Cmd // currently running % (shell-pipe) command cmdIn io.WriteCloser // stdin writer for running % command cmdOutBuf []byte // output of running % command cmdHistory []string // command history entries cmdHistoryBeg int // index where commands from this session start in cmdHistory cmdHistoryInd int // history navigation offset from most recent cmdHistoryInput *string // initial input used as prefix filter while browsing history menuCompActive bool // whether completion cycling is active menuCompTmp []string // token snapshot taken when completion cycling starts, used for `cmd-menu-discard` menuComps []compMatch // completion candidates for active prompt menuCompInd int // index of selected completion candidate (-1: none selected) selectionOut []string // paths to output on exit, used for `-print-selection` and `-selection-path` watch *watch // fs watcher if `watch` is enabled quitting bool // guard to prevent re-entering quit logic } func newApp(ui *ui, nav *nav) *app { quitChan := make(chan struct{}, 1) app := &app{ ui: ui, nav: nav, ticker: new(time.Ticker), quitChan: quitChan, watch: newWatch(nav.dirChan, nav.fileChan, nav.delChan), } sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM) go func() { for { switch <-sigChan { case os.Interrupt: case syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM: app.quit() os.Exit(3) return } } }() return app } func (app *app) quit() { // Using synchronous shell commands for `on-quit` can cause this to be // called again, so a guard variable is introduced here to prevent an // infinite loop. if app.quitting { return } app.quitting = true onQuit(app) if gOpts.history { if err := app.writeHistory(); err != nil { log.Printf("writing history file: %s", err) } } if !gSingleMode { if _, err := remote(fmt.Sprintf("drop %d", gClientID)); err != nil { log.Printf("dropping connection: %s", err) } if gOpts.autoquit { if _, err := remote("quit"); err != nil { log.Printf("auto quitting server: %s", err) } } } } func (app *app) readFile(path string) { log.Printf("reading file: %s", path) f, err := os.Open(path) if err != nil { app.ui.echoerrf("opening file: %s", err) return } defer f.Close() p := newParser(f) for p.parse() { p.expr.eval(app, nil) } if p.err != nil { app.ui.echoerrf("%s", p.err) } } func loadFiles() (clipboard clipboard, err error) { files, err := os.Open(gFilesPath) if os.IsNotExist(err) { err = nil return } if err != nil { err = fmt.Errorf("opening file selections file: %w", err) return } defer files.Close() s := bufio.NewScanner(files) if !s.Scan() { err = fmt.Errorf("scanning file list: %w", cmp.Or(s.Err(), io.EOF)) return } switch s.Text() { case "copy": clipboard.mode = clipboardCopy case "move": clipboard.mode = clipboardCut default: err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text()) return } for s.Scan() && s.Text() != "" { clipboard.paths = append(clipboard.paths, s.Text()) } if s.Err() != nil { err = fmt.Errorf("scanning file list: %w", s.Err()) return } log.Printf("loading clipboard: %v", clipboard.paths) return } func saveFiles(clipboard clipboard) error { if err := os.MkdirAll(filepath.Dir(gFilesPath), os.ModePerm); err != nil { return fmt.Errorf("creating data directory: %w", err) } files, err := os.Create(gFilesPath) if err != nil { return fmt.Errorf("opening file selections file: %w", err) } defer files.Close() log.Printf("saving files: %v", clipboard.paths) var clipboardModeStr string if clipboard.mode == clipboardCopy { clipboardModeStr = "copy" } else { clipboardModeStr = "move" } if _, err := fmt.Fprintln(files, clipboardModeStr); err != nil { return fmt.Errorf("write clipboard mode to file: %w", err) } for _, path := range clipboard.paths { if _, err := fmt.Fprintln(files, path); err != nil { return fmt.Errorf("write path to file: %w", err) } } return files.Sync() } func (app *app) readHistory() error { f, err := os.Open(gHistoryPath) if os.IsNotExist(err) { return nil } if err != nil { return fmt.Errorf("opening history file: %w", err) } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { cmd := scanner.Text() if len(cmd) < 1 || !slices.Contains([]string{":", "$", "!", "%", "&"}, cmd[:1]) { continue } app.cmdHistory = append(app.cmdHistory, cmd) } app.cmdHistoryBeg = len(app.cmdHistory) if err := scanner.Err(); err != nil { return fmt.Errorf("reading history file: %w", err) } return nil } func (app *app) writeHistory() error { if len(app.cmdHistory) == 0 { return nil } local := slices.Clone(app.cmdHistory[app.cmdHistoryBeg:]) app.cmdHistory = nil if err := app.readHistory(); err != nil { return fmt.Errorf("reading history file: %w", err) } app.cmdHistory = append(app.cmdHistory, local...) if len(app.cmdHistory) > 1000 { app.cmdHistory = app.cmdHistory[len(app.cmdHistory)-1000:] } if err := os.MkdirAll(filepath.Dir(gHistoryPath), os.ModePerm); err != nil { return fmt.Errorf("creating data directory: %w", err) } f, err := os.Create(gHistoryPath) if err != nil { return fmt.Errorf("creating history file: %w", err) } defer f.Close() for _, cmd := range app.cmdHistory { if _, err = fmt.Fprintln(f, cmd); err != nil { return fmt.Errorf("writing history file: %w", err) } } return nil } // loop is the main event loop of the application. Expressions are read from // the client and the server on separate goroutines and sent here over channels // for evaluation. Similarly directories and regular files are also read in // separate goroutines and sent here for update. func (app *app) loop() { go app.nav.preloadLoop(app.ui) go app.nav.previewLoop(app.ui) var serverChan <-chan expr if !gSingleMode { serverChan = readExpr() } go app.ui.readEvents() if gConfigPath != "" { if _, err := os.Stat(gConfigPath); !os.IsNotExist(err) { app.readFile(gConfigPath) } else { log.Printf("config file does not exist: %s", err) } } else { for _, path := range gConfigPaths { if _, err := os.Stat(path); !os.IsNotExist(err) { app.readFile(path) } } } for _, cmd := range gCommands { p := newParser(strings.NewReader(cmd)) for p.parse() { p.expr.eval(app, nil) } if p.err != nil { app.ui.echoerrf("%s", p.err) } } app.nav.addJumpList() if gSelect != "" { go func() { lstat, err := os.Lstat(gSelect) if err != nil { app.ui.exprChan <- &callExpr{"echoerr", []string{err.Error()}, 1} } else if lstat.IsDir() { app.ui.exprChan <- &callExpr{"cd", []string{gSelect}, 1} } else { app.ui.exprChan <- &callExpr{"select", []string{gSelect}, 1} } }() } for { select { case <-app.quitChan: if app.nav.copyJobs > 0 { app.ui.echoerr("quit: copy operation in progress") continue } if app.nav.moveTotal > 0 { app.ui.echoerr("quit: move operation in progress") continue } if app.nav.deleteTotal > 0 { app.ui.echoerr("quit: delete operation in progress") continue } app.quit() app.nav.previewChan <- "" log.Printf("*************** closing client, PID: %d ***************", gClientID) return case n := <-app.nav.copyJobsChan: app.nav.copyJobs += n app.ui.draw(app.nav) case n := <-app.nav.copyBytesChan: app.nav.copyBytes += n // n is usually 32*1024B (default io.Copy() buffer) so update roughly per 32KB x 128 = 4MB copied if app.nav.copyUpdate++; app.nav.copyUpdate >= 128 { app.nav.copyUpdate = 0 app.ui.draw(app.nav) } case n := <-app.nav.copyTotalChan: app.nav.copyTotal += n if n < 0 { app.nav.copyBytes += n } if app.nav.copyTotal == 0 { app.nav.copyUpdate = 0 } app.ui.draw(app.nav) case n := <-app.nav.moveCountChan: app.nav.moveCount += n if app.nav.moveUpdate++; app.nav.moveUpdate >= 1000 { app.nav.moveUpdate = 0 app.ui.draw(app.nav) } case n := <-app.nav.moveTotalChan: app.nav.moveTotal += n if n < 0 { app.nav.moveCount += n } if app.nav.moveTotal == 0 { app.nav.moveUpdate = 0 } app.ui.draw(app.nav) case n := <-app.nav.deleteCountChan: app.nav.deleteCount += n if app.nav.deleteUpdate++; app.nav.deleteUpdate >= 1000 { app.nav.deleteUpdate = 0 app.ui.draw(app.nav) } case n := <-app.nav.deleteTotalChan: app.nav.deleteTotal += n if n < 0 { app.nav.deleteCount += n } if app.nav.deleteTotal == 0 { app.nav.deleteUpdate = 0 } app.ui.draw(app.nav) case d := <-app.nav.dirChan: var oldCurrPath string if curr := app.nav.currFile(); curr != nil { oldCurrPath = curr.path } prev, ok := app.nav.dirCache[d.path] if ok { d.ind = prev.ind d.pos = prev.pos d.visualAnchor = min(prev.visualAnchor, len(d.files)-1) d.visualWrap = prev.visualWrap d.filter = prev.filter d.sort() d.sel(prev.name(), app.nav.height) } app.nav.dirCache[d.path] = d app.nav.position() if curr := app.nav.currFile(); curr != nil { if curr.path != oldCurrPath { app.ui.loadFile(app, true) } } app.watchDir(d) // Avoid flickering UI and multiple, unnecessary `on-load` calls // triggered by Git commands executed inside the users `on-load` // command (often used to add git symbols using `addcustominfo`). // TODO: Should `watch` also ignore `.git` directories? if filepath.Base(d.path) != ".git" { paths := make([]string, len(d.allFiles)) for i, file := range d.allFiles { paths[i] = file.path } onLoad(app, paths) } if d.path == app.nav.currDir().path { app.nav.preload() } app.ui.draw(app.nav) case r := <-app.nav.regChan: app.nav.regCache[r.path] = r if curr := app.nav.currFile(); curr != nil { if r.path == curr.path { app.ui.sxScreen.forceClear = true if gOpts.preload && r.volatile { app.ui.loadFile(app, true) } } } app.ui.draw(app.nav) case f := <-app.nav.fileChan: for _, dir := range app.nav.dirCache { if dir.path != filepath.Dir(f.path) { continue } for i := range dir.allFiles { if dir.allFiles[i].path == f.path { dir.allFiles[i] = f break } } name := dir.name() dir.sort() dir.sel(name, app.nav.height) } delete(app.nav.regCache, f.path) app.ui.loadFile(app, false) onLoad(app, []string{f.path}) app.ui.draw(app.nav) case path := <-app.nav.delChan: deletePathRecursive(app.nav.selections, path) if len(app.nav.selections) == 0 { app.nav.selectionInd = 0 } deletePathRecursive(app.nav.regCache, path) deletePathRecursive(app.nav.dirCache, path) for _, dirPath := range app.nav.dirPaths { if dirPath == path { if err := app.nav.cd(filepath.Dir(path)); err != nil { log.Print(err) } break } } case ev := <-app.ui.evChan: e := app.ui.readEvent(ev, app.nav) if e == nil { continue } e.eval(app, nil) loop: for { select { case ev := <-app.ui.evChan: e = app.ui.readEvent(ev, app.nav) if e == nil { continue } e.eval(app, nil) default: break loop } } app.ui.draw(app.nav) case e := <-app.ui.exprChan: e.eval(app, nil) app.ui.draw(app.nav) case e := <-serverChan: e.eval(app, nil) app.ui.draw(app.nav) case <-app.ticker.C: app.nav.renew() app.ui.loadFile(app, false) case <-app.nav.previewTimer.C: app.ui.draw(app.nav) case <-app.nav.preloadTimer.C: app.nav.preload() } } } func (app *app) runCmdSync(cmd *exec.Cmd, pauseAfter bool) { app.nav.previewChan <- "" if err := app.ui.suspend(); err != nil { log.Printf("suspend: %s", err) } defer func() { if err := app.ui.resume(); err != nil { app.quit() os.Exit(3) } }() if err := cmd.Run(); err != nil { app.ui.echoerrf("running shell: %s", err) } if pauseAfter { anyKey() } app.ui.loadFile(app, true) app.nav.renew() } // runShell is used to run a shell command. Modes are as follows: // // Prefix Wait Async Stdin Stdout Stderr UI action // $ No No Yes Yes Yes Pause and then resume // % No No Yes Yes Yes Statline for input/output // ! Yes No Yes Yes Yes Pause and then resume // & No Yes No No No Do nothing func (app *app) runShell(s string, args []string, prefix string) { app.nav.exportFiles() app.ui.exportSizes() app.exportMode() exportLfPath() exportOpts() gState.mutex.Lock() gState.data["maps"] = listBinds(map[string]map[string]expr{ "n": gOpts.nkeys, "v": gOpts.vkeys, }) gState.data["nmaps"] = listBinds(map[string]map[string]expr{ "n": gOpts.nkeys, }) gState.data["vmaps"] = listBinds(map[string]map[string]expr{ "v": gOpts.vkeys, }) gState.data["cmaps"] = listBinds(map[string]map[string]expr{ "c": gOpts.cmdkeys, }) gState.data["cmds"] = listCmds(gOpts.cmds) gState.data["jumps"] = listJumps(app.nav.jumpList, app.nav.jumpListInd) gState.data["history"] = listHistory(app.cmdHistory) gState.data["files"] = listFilesInCurrDir(app.nav) gState.mutex.Unlock() cmd := shellCommand(s, args) switch prefix { case "$", "!": cmd.Stdin = os.Stdin cmd.Stdout = os.Stderr cmd.Stderr = os.Stderr app.runCmdSync(cmd, prefix == "!") return } // We are running the command asynchronously var inReader, inWriter, outReader, outWriter *os.File if prefix == "%" { if app.ui.cmdPrefix == ">" { return } // [exec.Cmd.StdoutPipe] cannot be used as it requires the output to be fully // read before calling [exec.Cmd.Wait], however in this case Cmd.Wait should // only wait for the command to finish executing regardless of whether the // output has been fully read or not. inReader, inWriter, err := os.Pipe() if err != nil { log.Printf("creating input pipe: %s", err) return } cmd.Stdin = inReader app.cmdIn = inWriter outReader, outWriter, err = os.Pipe() if err != nil { log.Printf("creating output pipe: %s", err) return } cmd.Stdout = outWriter cmd.Stderr = outWriter } shellSetPG(cmd) if err := cmd.Start(); err != nil { app.ui.echoerrf("running shell: %s", err) } switch prefix { case "%": normal(app) app.cmd = cmd app.cmdOutBuf = nil app.ui.cmdPrefix = ">" app.ui.echo("") go func() { reader := bufio.NewReader(outReader) for { b, err := reader.ReadByte() if err != nil { if !errors.Is(err, io.EOF) && !errors.Is(err, fs.ErrClosed) { log.Printf("reading command output: %s", err) } break } app.cmdOutBuf = append(app.cmdOutBuf, b) if reader.Buffered() == 0 { app.ui.exprChan <- &callExpr{"echo", []string{string(app.cmdOutBuf)}, 1} } if b == '\n' || b == '\r' { app.cmdOutBuf = nil } } }() go func() { if err := cmd.Wait(); err != nil { log.Printf("running shell: %s", err) } inReader.Close() inWriter.Close() outReader.Close() outWriter.Close() app.cmd = nil app.ui.cmdPrefix = "" app.ui.exprChan <- &callExpr{"load", nil, 1} }() case "&": go func() { if err := cmd.Wait(); err != nil { log.Printf("running shell: %s", err) } app.ui.exprChan <- &callExpr{"load", nil, 1} }() } } func (app *app) doComplete() (matches []compMatch) { var longest string switch app.ui.cmdPrefix { case ":": matches, longest = completeCmd(app.ui.cmdAccLeft) case "$", "%", "!", "&": matches, longest = completeShell(app.ui.cmdAccLeft) case "/", "?": matches, longest = completeSearch(app.ui.cmdAccLeft) } app.ui.cmdAccLeft = longest app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1) return } func (app *app) menuComplete(direction int) { if !app.menuCompActive { app.menuCompTmp = tokenize(app.ui.cmdAccLeft) app.menuComps = app.doComplete() if len(app.menuComps) > 1 { app.menuCompInd = -1 app.menuCompActive = true } } else { app.menuCompInd += direction if app.menuCompInd == len(app.menuComps) { app.menuCompInd = 0 } else if app.menuCompInd < 0 { app.menuCompInd = len(app.menuComps) - 1 } toks := slices.Clone(app.menuCompTmp) toks[len(toks)-1] = app.menuComps[app.menuCompInd].result app.ui.cmdAccLeft = strings.Join(toks, " ") } app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, app.menuComps, app.menuCompInd) } func (app *app) watchDir(dir *dir) { if !gOpts.watch { return } app.watch.add(dir.path) // ensure dircounts are updated for child directories for _, file := range dir.allFiles { if file.IsDir() { app.watch.add(file.path) } } } func (app *app) exportMode() { getMode := func() string { if app.menuCompActive { return "compmenu" } if strings.HasPrefix(app.ui.cmdPrefix, "delete") { return "delete" } if strings.HasPrefix(app.ui.cmdPrefix, "replace") || strings.HasPrefix(app.ui.cmdPrefix, "create") { return "rename" } switch app.ui.cmdPrefix { case "filter: ": return "filter" case "find: ", "find-back: ": return "find" case "mark-save: ", "mark-load: ", "mark-remove: ": return "mark" case "rename: ": return "rename" case "/", "?": return "search" case ":": return "command" case "$", "%", "!", "&": return "shell" case ">": return "pipe" case "": if app.nav.isVisualMode() { return "visual" } return "normal" default: return "unknown" } } os.Setenv("lf_mode", getMode()) } ================================================ FILE: client.go ================================================ package main import ( "bufio" "fmt" "io" "log" "net" "os" "strings" "sync" "time" "github.com/gdamore/tcell/v3" ) type State struct { mutex sync.Mutex data map[string]string } var gState State func init() { gState.data = make(map[string]string) } func run() { if gLogPath != "" { f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600) if err != nil { log.Fatalf("failed to open log file: %s", err) } defer f.Close() log.SetOutput(f) } else { log.SetOutput(io.Discard) } log.Printf("*************** starting client, PID: %d ***************", gClientID) var screen tcell.Screen var err error if screen, err = tcell.NewScreen(); err != nil { log.Fatalf("creating screen: %s", err) } else if err = screen.Init(); err != nil { log.Fatalf("initializing screen: %s", err) } if gOpts.mouse { screen.EnableMouse() } screen.EnablePaste() ui := newUI(screen) nav := newNav(ui) app := newApp(ui, nav) if err := nav.sync(); err != nil { app.ui.echoerrf("sync: %s", err) } if err := app.readHistory(); err != nil { app.ui.echoerrf("reading history file: %s", err) } app.loop() app.ui.screen.Fini() if gLastDirPath != "" { writeLastDir(gLastDirPath, app.nav.currDir().path) } if gSelectionPath != "" && len(app.selectionOut) > 0 { writeSelection(gSelectionPath, app.selectionOut) } if gPrintLastDir { fmt.Println(app.nav.currDir().path) } if gPrintSelection && len(app.selectionOut) > 0 { for _, file := range app.selectionOut { fmt.Println(file) } } } func writeLastDir(filename, lastDir string) { f, err := os.Create(filename) if err != nil { log.Printf("opening last dir file: %s", err) return } defer f.Close() _, err = f.WriteString(lastDir) if err != nil { log.Printf("writing last dir file: %s", err) } } func writeSelection(filename string, selection []string) { f, err := os.Create(filename) if err != nil { log.Printf("opening selection file: %s", err) return } defer f.Close() _, err = f.WriteString(strings.Join(selection, "\n")) if err != nil { log.Printf("writing selection file: %s", err) } } func readExpr() <-chan expr { ch := make(chan expr) go func() { duration := 100 * time.Millisecond c, err := net.Dial(gSocketProt, gSocketPath) for err != nil { log.Printf("connecting server: %s", err) time.Sleep(duration) duration *= 2 c, err = net.Dial(gSocketProt, gSocketPath) } if _, err := fmt.Fprintf(c, "conn %d\n", gClientID); err != nil { log.Fatalf("registering with server: %s", err) } ch <- &callExpr{"sync", nil, 1} ch <- &callExpr{"on-init", nil, 1} s := bufio.NewScanner(c) for s.Scan() { log.Printf("recv: %s", s.Text()) // `query` has to be handled outside of the main thread, which is // blocked when running a synchronous shell command ("$" or "!"). // This is important since `query` is often the result of the user // running `$lf -remote "query $id "`. if word, rest := splitWord(s.Text()); word == "query" { gState.mutex.Lock() state := gState.data[rest] gState.mutex.Unlock() if _, err := fmt.Fprintln(c, state); err != nil { log.Fatalf("sending response to server: %s", err) } } else { p := newParser(strings.NewReader(s.Text())) if p.parse() { ch <- p.expr } } } if err := s.Err(); err != nil { log.Printf("reading from server: %s", err) } c.Close() }() return ch } func remote(req string) (string, error) { c, err := net.Dial(gSocketProt, gSocketPath) if err != nil { return "", fmt.Errorf("connecting to server: %w", err) } defer c.Close() if _, err := fmt.Fprintln(c, req); err != nil { return "", fmt.Errorf("sending command to server: %w", err) } // XXX: Standard net.Conn interface does not include a CloseWrite method // but net.UnixConn and net.TCPConn implement it so the following should be // safe as long as we do not use other types of connections. We need // CloseWrite to notify the server that this is not a persistent connection // and it should be closed after the response. switch c := c.(type) { case *net.TCPConn: c.CloseWrite() case *net.UnixConn: c.CloseWrite() } resp, err := io.ReadAll(c) if err != nil { return "", fmt.Errorf("reading response from server: %w", err) } return string(resp), nil } ================================================ FILE: colors.go ================================================ package main import ( "fmt" "log" "os" "path/filepath" "strconv" "strings" "github.com/gdamore/tcell/v3" ) type styleMap struct { styles map[string]tcell.Style useLinkTarget bool } func parseStyles() styleMap { sm := styleMap{ styles: make(map[string]tcell.Style), useLinkTarget: false, } // Default values from dircolors // // no* NORMAL 00 // fi FILE 00 // rs* RESET 0 // di DIR 01;34 // ln LINK 01;36 // mh* MULTIHARDLINK 00 // pi FIFO 40;33 // so SOCK 01;35 // do* DOOR 01;35 // bd BLK 40;33;01 // cd CHR 40;33;01 // or ORPHAN 40;31;01 // mi* MISSING 00 // su SETUID 37;41 // sg SETGID 30;43 // ca* CAPABILITY 30;41 // tw STICKY_OTHER_WRITABLE 30;42 // ow OTHER_WRITABLE 34;42 // st STICKY 37;44 // ex EXEC 01;32 // // (Entries marked with * are not implemented in lf) // default values from dircolors with background colors removed defaultColors := []string{ "fi=00", "di=01;34", "ln=01;36", "pi=33", "so=01;35", "bd=33;01", "cd=33;01", "or=31;01", "su=01;32", "sg=01;32", "tw=01;34", "ow=01;34", "st=01;34", "ex=01;32", } sm.parseGNU(strings.Join(defaultColors, ":")) if env := os.Getenv("LSCOLORS"); env != "" { sm.parseBSD(env) } if env := os.Getenv("LS_COLORS"); env != "" { sm.parseGNU(env) } if env := os.Getenv("LF_COLORS"); env != "" { sm.parseGNU(env) } for _, path := range gColorsPaths { if _, err := os.Stat(path); !os.IsNotExist(err) { sm.parseFile(path) } } return sm } func parseColor(toks []string) (tcell.Color, int, error) { if len(toks) == 0 { return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) } if toks[0] == "5" && len(toks) >= 2 { n, err := strconv.Atoi(toks[1]) if err != nil { return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) } return tcell.PaletteColor(n), 2, nil } if toks[0] == "2" && len(toks) >= 4 { r, err := strconv.Atoi(toks[1]) if err != nil { return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) } g, err := strconv.Atoi(toks[2]) if err != nil { return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) } b, err := strconv.Atoi(toks[3]) if err != nil { return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) } return tcell.NewRGBColor(int32(r), int32(g), int32(b)), 4, nil } return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) } func (sm styleMap) parseFile(path string) { log.Printf("reading file: %s", path) f, err := os.Open(path) if err != nil { log.Printf("opening colors file: %s", err) return } defer f.Close() pairs, err := readPairs(f) if err != nil { log.Printf("reading colors file: %s", err) return } for _, pair := range pairs { sm.parsePair(pair) } } // parseGNU parses the $LS_COLORS environment variable. func (sm *styleMap) parseGNU(env string) { for entry := range strings.SplitSeq(env, ":") { if entry == "" { continue } pair := strings.Split(entry, "=") if len(pair) != 2 { log.Printf("invalid $LS_COLORS entry: %s", entry) return } sm.parsePair(pair) } } func (sm *styleMap) parsePair(pair []string) { key, val := pair[0], pair[1] key = replaceTilde(key) if filepath.IsAbs(key) { key = filepath.Clean(key) } if key == "ln" && val == "target" { sm.useLinkTarget = true } sm.styles[key] = applySGR(val, tcell.StyleDefault) } // parseBSD parses the $LSCOLORS environment variable. func (sm styleMap) parseBSD(env string) { if len(env) != 22 { log.Printf("invalid $LSCOLORS variable: %s", env) return } colorNames := []string{"di", "ln", "so", "pi", "ex", "bd", "cd", "su", "sg", "tw", "ow"} getStyle := func(r1, r2 byte) tcell.Style { st := tcell.StyleDefault switch { case r1 == 'x': st = st.Foreground(tcell.ColorDefault) case 'A' <= r1 && r1 <= 'H': st = st.Foreground(tcell.PaletteColor(int(r1 - 'A'))).Bold(true) case 'a' <= r1 && r1 <= 'h': st = st.Foreground(tcell.PaletteColor(int(r1 - 'a'))) default: log.Printf("invalid $LSCOLORS entry: %c", r1) return tcell.StyleDefault } switch { case r2 == 'x': st = st.Background(tcell.ColorDefault) case 'a' <= r2 && r2 <= 'h': st = st.Background(tcell.PaletteColor(int(r2 - 'a'))) default: log.Printf("invalid $LSCOLORS entry: %c", r2) return tcell.StyleDefault } return st } for i, key := range colorNames { sm.styles[key] = getStyle(env[i*2], env[i*2+1]) } } func (sm styleMap) get(f *file) tcell.Style { if val, ok := sm.styles[f.path]; ok { return val } if f.IsDir() { if val, ok := sm.styles[f.Name()+"/"]; ok { return val } } var key string switch { case f.linkState == working && !sm.useLinkTarget: key = "ln" case f.linkState == broken: key = "or" case f.IsDir() && f.Mode()&os.ModeSticky != 0 && f.Mode()&0o002 != 0: key = "tw" case f.IsDir() && f.Mode()&0o002 != 0: key = "ow" case f.IsDir() && f.Mode()&os.ModeSticky != 0: key = "st" case f.IsDir(): key = "di" case f.Mode()&os.ModeNamedPipe != 0: key = "pi" case f.Mode()&os.ModeSocket != 0: key = "so" case f.Mode()&os.ModeCharDevice != 0: key = "cd" case f.Mode()&os.ModeDevice != 0: key = "bd" case f.Mode()&os.ModeSetuid != 0: key = "su" case f.Mode()&os.ModeSetgid != 0: key = "sg" case isExecutable(f.FileInfo): key = "ex" } if val, ok := sm.styles[key]; ok { return val } if val, ok := sm.styles[f.Name()+"*"]; ok { return val } if val, ok := sm.styles["*"+f.Name()]; ok { return val } if val, ok := sm.styles[filepath.Base(f.Name())+".*"]; ok { return val } if val, ok := sm.styles["*"+strings.ToLower(f.ext)]; ok { return val } if val, ok := sm.styles["fi"]; ok { return val } return tcell.StyleDefault } ================================================ FILE: colors_test.go ================================================ package main import ( "testing" "github.com/gdamore/tcell/v3" ) func TestParseColor(t *testing.T) { tests := []struct { toks []string color tcell.Color offset int success bool }{ {[]string{}, tcell.ColorDefault, 0, false}, {[]string{"foo"}, tcell.ColorDefault, 0, false}, {[]string{"5"}, tcell.ColorDefault, 0, false}, {[]string{"5", "foo"}, tcell.ColorDefault, 0, false}, {[]string{"5", "42"}, tcell.PaletteColor(42), 2, true}, {[]string{"2"}, tcell.ColorDefault, 0, false}, {[]string{"2", "foo"}, tcell.ColorDefault, 0, false}, {[]string{"2", "42", "foo"}, tcell.ColorDefault, 0, false}, {[]string{"2", "42", "43", "foo"}, tcell.ColorDefault, 0, false}, {[]string{"2", "42", "43", "44"}, tcell.NewRGBColor(42, 43, 44), 4, true}, } for _, test := range tests { color, offset, err := parseColor(test.toks) success := err == nil if color != test.color || offset != test.offset || success != test.success { t.Errorf("at input %v expected (%v, %v, %v) but got (%v, %v, %v)", test.toks, test.color, test.offset, test.success, color, offset, success) } } } ================================================ FILE: complete.go ================================================ package main import ( "log" "maps" "os" "path/filepath" "reflect" "slices" "sort" "strings" ) var ( gCmdWords = []string{ "set", "setlocal", "map", "nmap", "vmap", "cmap", "cmd", "addcustominfo", "bottom", "calcdirsize", "cd", "clear", "clearmaps", "copy", "cut", "down", "delete", "draw", "echo", "echoerr", "echomsg", "filter", "find", "find-back", "find-next", "find-prev", "glob-select", "glob-unselect", "half-down", "half-up", "high", "invert", "jump-next", "jump-prev", "load", "low", "mark-load", "mark-remove", "mark-save", "middle", "open", "page-down", "page-up", "paste", "push", "quit", "read", "redraw", "reload", "rename", "scroll-down", "scroll-up", "search", "search-back", "search-next", "search-prev", "select", "setfilter", "shell", "shell-async", "shell-pipe", "shell-wait", "source", "sync", "tag", "tag-toggle", "toggle", "top", "tty-write", "unselect", "up", "updir", "visual", "visual-accept", "visual-change", "visual-discard", "visual-unselect", "cmd-capitalize-word", "cmd-complete", "cmd-delete", "cmd-delete-back", "cmd-delete-end", "cmd-delete-home", "cmd-delete-unix-word", "cmd-delete-word", "cmd-delete-word-back", "cmd-end", "cmd-enter", "cmd-escape", "cmd-history-next", "cmd-history-prev", "cmd-home", "cmd-interrupt", "cmd-left", "cmd-lowercase-word", "cmd-menu-accept", "cmd-menu-complete", "cmd-menu-complete-back", "cmd-menu-discard", "cmd-right", "cmd-transpose", "cmd-transpose-word", "cmd-uppercase-word", "cmd-word", "cmd-word-back", "cmd-yank", } gOptWords = getOptWords(gOpts) gLocalOptWords = getLocalOptWords(gLocalOpts) ) func getOptWords(opts any) (optWords []string) { t := reflect.TypeOf(opts) for i := range t.NumField() { field := t.Field(i) switch field.Type.Kind() { case reflect.Map: continue case reflect.Bool: name := field.Name optWords = append(optWords, name, "no"+name, name+"!") default: optWords = append(optWords, field.Name) } } sort.Strings(optWords) return } func getLocalOptWords(localOpts any) (localOptWords []string) { t := reflect.TypeOf(localOpts) for i := range t.NumField() { field := t.Field(i) name := field.Name if field.Type.Kind() != reflect.Map { continue } if field.Type.Elem().Kind() == reflect.Bool { localOptWords = append(localOptWords, name, "no"+name, name+"!") } else { localOptWords = append(localOptWords, name) } } sort.Strings(localOptWords) return } func getLongest(s1, s2 string) string { r1 := []rune(s1) r2 := []rune(s2) i := 0 for ; i < len(r1) && i < len(r2); i++ { if r1[i] != r2[i] { break } } return string(r1[:i]) } type compMatch struct { name string // display name in completion menu result string // result when cycling through completion menu } func matchWord(s string, words []string) (matches []compMatch, longest string) { for _, w := range words { if !strings.HasPrefix(w, s) { continue } matches = append(matches, compMatch{w, w}) if len(matches) == 1 { longest = w } else { longest = getLongest(longest, w) } } switch len(matches) { case 0: longest = s case 1: longest += " " } return } func matchList(s string, words []string) (matches []compMatch, longest string) { toks := strings.Split(s, ":") for _, w := range words { if slices.Contains(toks[:len(toks)-1], w) || !strings.HasPrefix(w, toks[len(toks)-1]) { continue } matchResult := strings.Join(append(slices.Clone(toks[:len(toks)-1]), w), ":") matches = append(matches, compMatch{w, matchResult}) if len(matches) == 1 { longest = matchResult } else { longest = getLongest(longest, matchResult) } } switch len(matches) { case 0: longest = s case 1: if longest == s { longest += " " } } return } func matchCmd(s string) (matches []compMatch, longest string) { words := slices.Concat(gCmdWords, slices.Collect(maps.Keys(gOpts.cmds))) slices.Sort(words) matches, longest = matchWord(s, slices.Compact(words)) return } func matchFile(s string, dirOnly bool, escape, unescape func(string) string) (matches []compMatch, longest string) { dir, file := filepath.Split(unescape(replaceTilde(s))) d := dir if dir == "" { d = "." } files, err := os.ReadDir(d) if err != nil { log.Printf("reading directory: %s", err) longest = s return } var longestName string for _, f := range files { isDir := false if f.IsDir() { isDir = true } else if f.Type()&os.ModeSymlink != 0 { if stat, err := os.Stat(filepath.Join(d, f.Name())); err == nil && stat.IsDir() { isDir = true } } if !isDir && dirOnly { continue } if !strings.HasPrefix(strings.ToLower(f.Name()), strings.ToLower(file)) { continue } name := f.Name() if isDir { name += string(filepath.Separator) } matches = append(matches, compMatch{name, escape(dir + name)}) if len(matches) == 1 { longestName = name } else { // Match case-insensitively without changing the prefix's case. p := getLongest(strings.ToLower(longestName), strings.ToLower(name)) longestName = string([]rune(longestName)[:len([]rune(p))]) } } switch len(matches) { case 0: longest = s case 1: longest = escape(dir + longestName) if !strings.HasSuffix(longestName, string(filepath.Separator)) { longest += " " } default: longest = escape(dir + longestName) } return } func matchCmdFile(s string, dirOnly bool) (matches []compMatch, longest string) { matches, longest = matchFile(s, dirOnly, cmdEscape, cmdUnescape) return } func matchShellFile(s string) (matches []compMatch, longest string) { matches, longest = matchFile(s, false, shellEscape, shellUnescape) return } func matchExec(s string) (matches []compMatch, longest string) { var words []string for p := range strings.SplitSeq(envPath, string(filepath.ListSeparator)) { files, err := os.ReadDir(p) if err != nil { if !os.IsNotExist(err) { log.Printf("reading path: %s", err) } continue } for _, f := range files { if !strings.HasPrefix(f.Name(), s) { continue } finfo, err := f.Info() if err != nil { log.Printf("getting file information: %s", err) continue } if finfo.Mode().IsRegular() && isExecutable(finfo) { words = append(words, f.Name()) } } } slices.Sort(words) matches, longest = matchWord(s, slices.Compact(words)) return } func matchSearch(s string) (matches []compMatch, longest string) { files, err := os.ReadDir(".") if err != nil { log.Printf("reading directory: %s", err) longest = s return } for _, f := range files { if !strings.HasPrefix(strings.ToLower(f.Name()), strings.ToLower(s)) { continue } matches = append(matches, compMatch{f.Name(), f.Name()}) if len(matches) == 1 { longest = f.Name() } else { p := getLongest(strings.ToLower(longest), strings.ToLower(f.Name())) longest = string([]rune(longest)[:len([]rune(p))]) } } if len(matches) == 0 { longest = s } return } func completeCmd(s string) (matches []compMatch, longest string) { f := tokenize(s) if len(f) == 1 { matches, longest = matchCmd(s) return } longest = f[len(f)-1] switch f[0] { case "set": if len(f) == 2 { matches, longest = matchWord(f[1], gOptWords) break } if len(f) != 3 { break } switch f[1] { case "cleaner", "previewer", "rulerfile": matches, longest = matchCmdFile(f[2], false) case "borderstyle": matches, longest = matchWord(f[2], []string{"box", "roundbox", "outline", "roundoutline", "separators"}) case "filtermethod", "searchmethod": matches, longest = matchWord(f[2], []string{"glob", "regex", "text"}) case "info": matches, longest = matchList(f[2], []string{"atime", "btime", "ctime", "custom", "group", "perm", "size", "time", "user"}) case "preserve": matches, longest = matchList(f[2], []string{"mode", "timestamps"}) case "selmode": matches, longest = matchWord(f[2], []string{"all", "dir"}) case "sizeunits": matches, longest = matchWord(f[2], []string{"binary", "decimal"}) case "sortby": matches, longest = matchWord(f[2], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"}) case "terminalcursor": matches, longest = matchWord(f[2], []string{"default", "block", "underline", "bar", "blinkblock", "blinkunderline", "blinkbar"}) default: if slices.Contains(gOptWords, f[1]+"!") { matches, longest = matchWord(f[2], []string{"false", "true"}) } } case "setlocal": if len(f) == 2 { matches, longest = matchCmdFile(f[1], true) break } if len(f) == 3 { matches, longest = matchWord(f[2], gLocalOptWords) break } if len(f) != 4 { break } switch f[2] { case "info": matches, longest = matchList(f[3], []string{"atime", "btime", "ctime", "custom", "group", "perm", "size", "time", "user"}) case "sortby": matches, longest = matchWord(f[3], []string{"atime", "btime", "ctime", "custom", "ext", "name", "natural", "size", "time"}) default: if slices.Contains(gLocalOptWords, f[2]+"!") { matches, longest = matchWord(f[3], []string{"false", "true"}) } } case "map", "nmap", "vmap", "cmap": if len(f) == 3 { matches, longest = matchCmd(f[2]) } case "cmd": case "cd": if len(f) == 2 { matches, longest = matchCmdFile(f[1], true) } case "addcustominfo", "select", "source": if len(f) == 2 { matches, longest = matchCmdFile(f[1], false) } case "toggle": matches, longest = matchCmdFile(f[len(f)-1], false) default: if !slices.Contains(gCmdWords, f[0]) { matches, longest = matchCmdFile(f[len(f)-1], false) } } f[len(f)-1] = longest longest = strings.Join(f, " ") return } func completeShell(s string) (matches []compMatch, longest string) { f := tokenize(s) switch len(f) { case 1: matches, longest = matchExec(f[0]) default: matches, longest = matchShellFile(f[len(f)-1]) } f[len(f)-1] = longest longest = strings.Join(f, " ") return } func completeSearch(s string) (matches []compMatch, longest string) { matches, longest = matchSearch(s) return } ================================================ FILE: complete_test.go ================================================ package main import ( "reflect" "testing" ) func TestGetOptWords(t *testing.T) { tests := []struct { opts any exp []string }{ {struct{ feature bool }{}, []string{"feature", "feature!", "nofeature"}}, {struct{ feature int }{}, []string{"feature"}}, {struct{ feature string }{}, []string{"feature"}}, {struct{ feature []string }{}, []string{"feature"}}, } for _, test := range tests { result := getOptWords(test.opts) if !reflect.DeepEqual(result, test.exp) { t.Errorf("at input '%#v' expected '%s' but got '%s'", test.opts, test.exp, result) } } } func TestGetLocalOptWords(t *testing.T) { tests := []struct { localOpts any exp []string }{ {struct{ feature map[string]bool }{}, []string{"feature", "feature!", "nofeature"}}, {struct{ feature map[string]int }{}, []string{"feature"}}, {struct{ feature map[string]string }{}, []string{"feature"}}, {struct{ feature map[string][]string }{}, []string{"feature"}}, } for _, test := range tests { result := getLocalOptWords(test.localOpts) if !reflect.DeepEqual(result, test.exp) { t.Errorf("at input '%#v' expected '%s' but got '%s'", test.localOpts, test.exp, result) } } } func TestGetLongest(t *testing.T) { tests := []struct { s1 string s2 string exp string }{ {"", "", ""}, {"", "foo", ""}, {"foo", "", ""}, {"foo", "bar", ""}, {"foo", "foobar", "foo"}, {"foo", "barfoo", ""}, {"foobar", "foobaz", "fooba"}, {"год", "гол", "го"}, } for _, test := range tests { if got := getLongest(test.s1, test.s2); got != test.exp { t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.s1, test.s2, test.exp, got) } } } func TestMatchWord(t *testing.T) { tests := []struct { s string words []string matches []compMatch longest string }{ {"", nil, nil, ""}, {"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""}, {"f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, {"b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, {"fo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, {"ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, {"fo", []string{"bar", "baz"}, nil, "fo"}, } for _, test := range tests { matches, longest := matchWord(test.s, test.words) if !reflect.DeepEqual(matches, test.matches) { t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches) } if longest != test.longest { t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, longest) } } } func TestMatchList(t *testing.T) { tests := []struct { s string words []string matches []compMatch longest string }{ {"", nil, nil, ""}, {"", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}, {"bar", "bar"}, {"baz", "baz"}}, ""}, {"f", []string{"bar", "baz"}, nil, "f"}, {"f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo"}, {"b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, {"ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "bar"}, {"baz", "baz"}}, "ba"}, {"foo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "foo"}}, "foo "}, {"foo:", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "foo:bar"}, {"baz", "foo:baz"}}, "foo:ba"}, {"foo:f", []string{"foo", "bar", "baz"}, nil, "foo:f"}, {"foo:b", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "foo:bar"}, {"baz", "foo:baz"}}, "foo:ba"}, {"foo:ba", []string{"foo", "bar", "baz"}, []compMatch{{"bar", "foo:bar"}, {"baz", "foo:baz"}}, "foo:ba"}, {"bar:b", []string{"foo", "bar", "baz"}, []compMatch{{"baz", "bar:baz"}}, "bar:baz"}, {"bar:f", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "bar:foo"}}, "bar:foo"}, {"bar:foo", []string{"foo", "bar", "baz"}, []compMatch{{"foo", "bar:foo"}}, "bar:foo "}, } for _, test := range tests { matches, longest := matchList(test.s, test.words) if !reflect.DeepEqual(matches, test.matches) { t.Errorf("at input '%s' with '%s' expected '%v' but got '%v'", test.s, test.words, test.matches, matches) } if longest != test.longest { t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, longest) } } } ================================================ FILE: copy.go ================================================ package main import ( "fmt" "io" "os" "path/filepath" "slices" "strconv" "strings" "github.com/djherbis/times" ) type ProgressWriter struct { writer io.Writer nums chan<- int64 } func NewProgressWriter(writer io.Writer, nums chan<- int64) *ProgressWriter { return &ProgressWriter{ writer: writer, nums: nums, } } func (progressWriter *ProgressWriter) Write(b []byte) (int, error) { n, err := progressWriter.writer.Write(b) progressWriter.nums <- int64(n) return n, err } func copySize(srcs []string) (int64, error) { var total int64 for _, src := range srcs { _, err := os.Lstat(src) if os.IsNotExist(err) { return total, fmt.Errorf("src does not exist: %q", src) } err = filepath.Walk(src, func(_ string, info os.FileInfo, err error) error { if err != nil { return fmt.Errorf("walk: %w", err) } total += info.Size() return nil }) if err != nil { return total, err } } return total, nil } func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan<- int64, errs chan<- error) { r, err := os.Open(src) if err != nil { errs <- err return } defer r.Close() var dstMode os.FileMode = 0o666 if slices.Contains(preserve, "mode") { dstMode = info.Mode() } w, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, dstMode) if err != nil { errs <- err return } if _, err := io.Copy(NewProgressWriter(w, nums), r); err != nil { errs <- err w.Close() if err = os.Remove(dst); err != nil { errs <- err } return } if err := w.Close(); err != nil { errs <- err if err = os.Remove(dst); err != nil { errs <- err } return } if slices.Contains(preserve, "timestamps") { atime := times.Get(info).AccessTime() mtime := info.ModTime() if err := os.Chtimes(dst, atime, mtime); err != nil { errs <- err if err = os.Remove(dst); err != nil { errs <- err } return } } } func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64, errs chan error) { nums = make(chan int64, 1024) errs = make(chan error, 1024) go func() { dirInfos := make(map[string]os.FileInfo) for _, src := range srcs { file := filepath.Base(src) dst := filepath.Join(dstDir, file) if lstat, err := os.Lstat(dst); err == nil { ext := getFileExtension(lstat) basename := file[:len(file)-len(ext)] var newPath string for i := 1; !os.IsNotExist(err); i++ { file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext) file = strings.ReplaceAll(file, "%b", basename) file = strings.ReplaceAll(file, "%e", ext) file = strings.ReplaceAll(file, "%n", strconv.Itoa(i)) newPath = filepath.Join(dstDir, file) _, err = os.Lstat(newPath) } dst = newPath } err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error { if err != nil { errs <- fmt.Errorf("walk: %w", err) return nil } rel, err := filepath.Rel(src, path) if err != nil { errs <- fmt.Errorf("relative: %w", err) return nil } newPath := filepath.Join(dst, rel) switch { case info.IsDir(): dstMode := os.ModePerm if slices.Contains(preserve, "mode") { dstMode = info.Mode() } if err := os.MkdirAll(newPath, dstMode); err != nil { errs <- fmt.Errorf("mkdir: %w", err) } if slices.Contains(preserve, "timestamps") { dirInfos[newPath] = info } nums <- info.Size() case info.Mode()&os.ModeSymlink != 0: if rlink, err := os.Readlink(path); err != nil { errs <- fmt.Errorf("symlink: %w", err) } else { if err := os.Symlink(rlink, newPath); err != nil { errs <- fmt.Errorf("symlink: %w", err) } } nums <- info.Size() default: copyFile(path, newPath, preserve, info, nums, errs) } return nil }) if err != nil { errs <- fmt.Errorf("walk: %w", err) } } for path, info := range dirInfos { atime := times.Get(info).AccessTime() mtime := info.ModTime() if err := os.Chtimes(path, atime, mtime); err != nil { errs <- fmt.Errorf("chtimes: %w", err) } } close(errs) }() return nums, errs } ================================================ FILE: df_openbsd.go ================================================ package main import ( "log" "golang.org/x/sys/unix" ) func diskFree(wd string) string { var stat unix.Statfs_t if err := unix.Statfs(wd, &stat); err != nil { log.Printf("diskfree: %s", err) return "" } // Available blocks * size per block = available space in bytes return "df: " + humanize(int64(stat.F_bavail)*int64(stat.F_bsize)) } ================================================ FILE: df_statfs.go ================================================ //go:build darwin || dragonfly || freebsd || linux package main import ( "log" "golang.org/x/sys/unix" ) func diskFree(wd string) string { var stat unix.Statfs_t if err := unix.Statfs(wd, &stat); err != nil { log.Printf("diskfree: %s", err) return "" } // Available blocks * size per block = available space in bytes return "df: " + humanize(int64(stat.Bavail)*int64(stat.Bsize)) } ================================================ FILE: df_statvfs.go ================================================ //go:build illumos || netbsd || solaris package main import ( "log" "golang.org/x/sys/unix" ) func diskFree(wd string) string { var stat unix.Statvfs_t if err := unix.Statvfs(wd, &stat); err != nil { log.Printf("diskfree: %s", err) return "" } // Available blocks * size per block = available space in bytes return "df: " + humanize(int64(stat.Bavail)*int64(stat.Bsize)) } ================================================ FILE: df_windows.go ================================================ package main import ( "log" "golang.org/x/sys/windows" ) func diskFree(wd string) string { var free uint64 pathPtr, err := windows.UTF16PtrFromString(wd) if err != nil { log.Printf("diskfree: %s", err) return "" } err = windows.GetDiskFreeSpaceEx(pathPtr, &free, nil, nil) // cwd, free, total, available if err != nil { log.Printf("diskfree: %s", err) return "" } return "df: " + humanize(int64(free)) } ================================================ FILE: diacritics.go ================================================ package main var normMap = map[rune]rune{ // lowercase (not only) european 'ě': 'e', 'ř': 'r', 'ů': 'u', 'ø': 'o', 'ĉ': 'c', 'ĝ': 'g', 'ĥ': 'h', 'ĵ': 'j', 'ŝ': 's', 'ŭ': 'u', 'è': 'e', 'ù': 'u', 'ÿ': 'y', 'ė': 'e', 'į': 'i', 'ų': 'u', 'ā': 'a', 'ē': 'e', 'ī': 'i', 'ū': 'u', 'ļ': 'l', 'ķ': 'k', 'ņ': 'n', 'ģ': 'g', 'ő': 'o', 'ű': 'u', 'ë': 'e', 'ï': 'i', 'ą': 'a', 'ć': 'c', 'ę': 'e', 'ł': 'l', 'ń': 'n', 'ś': 's', 'ź': 'z', 'ż': 'z', 'õ': 'o', 'ș': 's', 'ț': 't', 'č': 'c', 'ď': 'd', 'ĺ': 'l', 'ľ': 'l', 'ň': 'n', 'ŕ': 'r', 'š': 's', 'ť': 't', 'ý': 'y', 'ž': 'z', 'é': 'e', 'í': 'i', 'ñ': 'n', 'ó': 'o', 'ú': 'u', 'ü': 'u', 'å': 'a', 'ä': 'a', 'ö': 'o', 'ç': 'c', 'î': 'i', 'ş': 's', 'û': 'u', 'ğ': 'g', 'ă': 'a', 'â': 'a', 'đ': 'd', 'ê': 'e', 'ô': 'o', 'ơ': 'o', 'ư': 'u', 'á': 'a', 'à': 'a', 'ã': 'a', 'ả': 'a', 'ạ': 'a', // uppercase (not only) european 'Ě': 'E', 'Ř': 'R', 'Ů': 'U', 'Ø': 'O', 'Ĉ': 'C', 'Ĝ': 'G', 'Ĥ': 'H', 'Ĵ': 'J', 'Ŝ': 'S', 'Ŭ': 'U', 'È': 'E', 'Ù': 'U', 'Ÿ': 'Y', 'Ė': 'E', 'Į': 'I', 'Ų': 'U', 'Ā': 'A', 'Ē': 'E', 'Ī': 'I', 'Ū': 'U', 'Ļ': 'L', 'Ķ': 'K', 'Ņ': 'N', 'Ģ': 'G', 'Ő': 'O', 'Ű': 'U', 'Ë': 'E', 'Ï': 'I', 'Ą': 'A', 'Ć': 'C', 'Ę': 'E', 'Ł': 'L', 'Ń': 'N', 'Ś': 'S', 'Ź': 'Z', 'Ż': 'Z', 'Õ': 'O', 'Ș': 'S', 'Ț': 'T', 'Č': 'C', 'Ď': 'D', 'Ĺ': 'L', 'Ľ': 'L', 'Ň': 'N', 'Ŕ': 'R', 'Š': 'S', 'Ť': 'T', 'Ý': 'Y', 'Ž': 'Z', 'É': 'E', 'Í': 'I', 'Ñ': 'N', 'Ó': 'O', 'Ú': 'U', 'Ü': 'U', 'Å': 'A', 'Ä': 'A', 'Ö': 'O', 'Ç': 'C', 'Î': 'I', 'Ş': 'S', 'Û': 'U', 'Ğ': 'G', 'Ă': 'A', 'Â': 'A', 'Đ': 'D', 'Ê': 'E', 'Ô': 'O', 'Ơ': 'O', 'Ư': 'U', 'Á': 'A', 'À': 'A', 'Ã': 'A', 'Ả': 'A', 'Ạ': 'A', // lowercase Vietnamese 'ắ': 'a', 'ặ': 'a', 'ằ': 'a', 'ẳ': 'a', 'ẵ': 'a', 'ấ': 'a', 'ậ': 'a', 'ầ': 'a', 'ẩ': 'a', 'ẫ': 'a', 'ẹ': 'e', 'ẻ': 'e', 'ẽ': 'e', 'ế': 'e', 'ệ': 'e', 'ề': 'e', 'ể': 'e', 'ễ': 'e', 'i': 'i', 'ị': 'i', 'ì': 'i', 'ỉ': 'i', 'ĩ': 'i', 'o': 'o', 'ọ': 'o', 'ò': 'o', 'ỏ': 'o', 'ố': 'o', 'ộ': 'o', 'ồ': 'o', 'ổ': 'o', 'ỗ': 'o', 'ớ': 'o', 'ợ': 'o', 'ờ': 'o', 'ở': 'o', 'ỡ': 'o', 'ụ': 'u', 'ủ': 'u', 'ũ': 'u', 'ứ': 'u', 'ự': 'u', 'ừ': 'u', 'ử': 'u', 'ữ': 'u', 'y': 'y', 'ỵ': 'y', 'ỳ': 'y', 'ỷ': 'y', 'ỹ': 'y', // uppercase Vietnamese 'Ắ': 'A', 'Ặ': 'A', 'Ằ': 'A', 'Ẳ': 'A', 'Ẵ': 'A', 'Ấ': 'A', 'Ậ': 'A', 'Ầ': 'A', 'Ẩ': 'A', 'Ẫ': 'A', 'Ẹ': 'E', 'Ẻ': 'E', 'Ẽ': 'E', 'Ế': 'E', 'Ệ': 'E', 'Ề': 'E', 'Ể': 'E', 'Ễ': 'E', 'I': 'I', 'Ị': 'I', 'Ì': 'I', 'Ỉ': 'I', 'Ĩ': 'I', 'O': 'O', 'Ọ': 'O', 'Ò': 'O', 'Ỏ': 'O', 'Ố': 'O', 'Ộ': 'O', 'Ồ': 'O', 'Ổ': 'O', 'Ỗ': 'O', 'Ớ': 'O', 'Ợ': 'O', 'Ờ': 'O', 'Ở': 'O', 'Ỡ': 'O', 'Ụ': 'U', 'Ủ': 'U', 'Ũ': 'U', 'Ứ': 'U', 'Ự': 'U', 'Ừ': 'U', 'Ử': 'U', 'Ữ': 'U', 'Y': 'Y', 'Ỵ': 'Y', 'Ỳ': 'Y', 'Ỷ': 'Y', 'Ỹ': 'Y', } func removeDiacritics(baseString string) string { normalizedRunes := make([]rune, 0, len(baseString)) for _, baseRune := range baseString { if normRune, ok := normMap[baseRune]; ok { normalizedRunes = append(normalizedRunes, normRune) } else { normalizedRunes = append(normalizedRunes, baseRune) } } return string(normalizedRunes) } ================================================ FILE: diacritics_test.go ================================================ package main import ( "testing" ) // typical czech test sentence ;-) const baseTestString = "Příliš žluťoučký kůň příšerně úpěl ďábelské ódy" func TestRemoveDiacritics(t *testing.T) { testStr := baseTestString expStr := "Prilis zlutoucky kun priserne upel dabelske ody" checkRemoveDiacritics(testStr, expStr, t) // other accents (non complete, but all I found) testStr = "áéíóúýčďěňřšťžůåøĉĝĥĵŝŭšžõäöüàâçéèêëîïôùûüÿžščćđáéíóúąęėįųūčšžāēīūčšžļķņģáéíóúöüőűäöüëïąćęłńóśźżáàãâçéêíóõôăâîșțáäčďéíĺľňóôŕšťúýžáéíñóúüåäöâçîşûğăâđêôơưáàãảạ" expStr = "aeiouycdenrstzuaocghjsuszoaouaaceeeeiiouuuyzsccdaeiouaeeiuucszaeiucszlkngaeiouououaoueiacelnoszzaaaaceeioooaaistaacdeillnoorstuyzaeinouuaaoacisugaadeoouaaaaa" checkRemoveDiacritics(testStr, expStr, t) testStr = "ÁÉÍÓÚÝČĎĚŇŘŠŤŽŮÅØĈĜĤĴŜŬŠŽÕÄÖÜÀÂÇÉÈÊËÎÏÔÙÛÜŸŽŠČĆĐÁÉÍÓÚĄĘĖĮŲŪČŠŽĀĒĪŪČŠŽĻĶŅĢÁÉÍÓÚÖÜŐŰÄÖÜËÏĄĆĘŁŃÓŚŹŻÁÀÃÂÇÉÊÍÓÕÔĂÂÎȘȚÁÄČĎÉÍĹĽŇÓÔŔŠŤÚÝŽÁÉÍÑÓÚÜÅÄÖÂÇÎŞÛĞĂÂĐÊÔƠƯÁÀÃẢẠ" expStr = "AEIOUYCDENRSTZUAOCGHJSUSZOAOUAACEEEEIIOUUUYZSCCDAEIOUAEEIUUCSZAEIUCSZLKNGAEIOUOUOUAOUEIACELNOSZZAAAACEEIOOOAAISTAACDEILLNOORSTUYZAEINOUUAAOACISUGAADEOOUAAAAA" checkRemoveDiacritics(testStr, expStr, t) testStr = "áạàảãăắặằẳẵâấậầẩẫéẹèẻẽêếệềểễiíịìỉĩoóọòỏõôốộồổỗơớợờởỡúụùủũưứựừửữyýỵỳỷỹđ" expStr = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiioooooooooooooooooouuuuuuuuuuuyyyyyyd" checkRemoveDiacritics(testStr, expStr, t) testStr = "ÁẠÀẢÃĂẮẶẰẲẴÂẤẬẦẨẪÉẸÈẺẼÊẾỆỀỂỄÍỊÌỈĨÓỌÒỎÕÔỐỘỒỔỖƠỚỢỜỞỠÚỤÙỦŨƯỨỰỪỬỮÝỴỲỶỸĐ" expStr = "AAAAAAAAAAAAAAAAAEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYD" checkRemoveDiacritics(testStr, expStr, t) } func checkRemoveDiacritics(testStr, expStr string, t *testing.T) { resultStr := removeDiacritics(testStr) if resultStr != expStr { t.Errorf("at input '%v' expected '%v' but got '%v'", testStr, expStr, resultStr) } } func TestSearchSettings(t *testing.T) { runSearch(t, true, false, true, true, "Veřejný", "vere", true) runSearch(t, true, false, true, false, baseTestString, "Zlutoucky", true) runSearch(t, true, false, true, false, baseTestString, "zlutoucky", true) runSearch(t, true, true, true, false, baseTestString, "Zlutoucky", false) runSearch(t, true, true, true, true, baseTestString, "zlutoucky", true) runSearch(t, false, false, true, false, baseTestString, "žlutoucky", true) runSearch(t, false, false, true, false, baseTestString, "Žlutoucky", false) runSearch(t, false, false, true, true, baseTestString, "žluťoučký", true) runSearch(t, false, false, true, false, baseTestString, "žluťoučký", true) runSearch(t, false, false, false, false, baseTestString, "žluťoučký", true) runSearch(t, false, false, false, false, baseTestString, "zlutoucky", false) runSearch(t, false, false, true, true, baseTestString, "zlutoucky", true) } func runSearch(t *testing.T, ignorecase, smartcase, ignorediacritics, smartdiacritics bool, base, pattern string, expected bool) { gOpts.ignorecase = ignorecase gOpts.smartcase = smartcase gOpts.ignoredia = ignorediacritics gOpts.smartdia = smartdiacritics matched, _ := searchMatch(base, pattern, textSearch) if matched != expected { t.Errorf("False search for ignorecase = %t, smartcase = %t, ignoredia = %t, smartdia = %t", gOpts.ignorecase, gOpts.smartcase, gOpts.ignoredia, gOpts.smartdia) } } ================================================ FILE: doc.md ================================================ # NAME lf - terminal file manager # SYNOPSIS **lf** [**-command** *command*] [**-config** *path*] [**-cpuprofile** *path*] [**-doc**] [**-help**] [**-last-dir-path** *path*] [**-log** *path*] [**-memprofile** *path*] [**-print-last-dir**] [**-print-selection**] [**-remote** *command*] [**-selection-path** *path*] [**-server**] [**-single**] [**-version**] [*cd-or-select-path*] # DESCRIPTION lf is a terminal file manager. The source code can be found in the repository at https://github.com/gokcehan/lf This documentation can either be read from the terminal using `lf -doc` or online at https://github.com/gokcehan/lf/blob/master/doc.md You can also use the `help` command (default ``) inside lf to view the documentation in a pager. A man page with the same content is also available in the repository at https://github.com/gokcehan/lf/blob/master/lf.1 # OPTIONS ## POSITIONAL ARGUMENTS **cd-or-select-path** Set the starting location. If *path* is a directory, start in there. If it's a file, start in the file's parent directory and select the file. When no *path* is supplied, lf uses the current directory. Only accepts one argument. ## META OPTIONS **-doc** Show lf's documentation (same content as this file) and exit. **-help** Show command-line usage and exit. **-version** Show version information and exit. ## STARTUP & CONFIGURATION **-command** *command* Execute *command* during client initialization (i.e. after reading configuration, before `on-init`). To execute more than one command, you can either use the **-command** flag multiple times or pass multiple commands at once by chaining them with ";". **-config** *path* Use the config file at *path* instead of the normal search locations. This only affects which `lfrc` is read at startup. ## SHELL INTEGRATION **-print-last-dir** Print the last directory to stdout when lf exits. This can be used to let lf change your shells working directory. See `CHANGING DIRECTORY` for more details. **-last-dir-path** *path* Same as **-print-last-dir**, but write the directory to *path* instead of stdout. **-print-selection** Print selected files to stdout when opening a file in lf. This can be used to use lf as an "open file" dialog. First, select the files you want to pass to another program. Then, confirm the selection by opening a file. This causes lf to quit and print out the selection. Quitting lf prematurely discards the selection. **-selection-path** *path* Same as **-print-selection**, but write the newline-separated list to *path* instead of stdout. ## SERVER **-remote** *command* Send *command* to the running server (i.e. `send`, `query`, `list`, `quit`, or `quit!`). See `REMOTE COMMANDS` for more details. **-server** Start the (headless) server process explicitly. Runs in the foreground and writes server logs to stderr (or the file set with **-log**). Clients auto-start a server if none is running unless **-single** is used. **-single** Start a stand-alone client without a server. Disables remote control. ## DIAGNOSTICS **-log** *path* Append runtime log messages to *path*. **-cpuprofile** *path* Write a CPU profile to *path*. The profile can be used by `go tool pprof`. **-memprofile** *path* Write a memory profile to *path*. The profile can be used by `go tool pprof`. ## EXAMPLES Use `lf` to select files (while hiding certain file types): lf -command 'set nohidden' -command 'set hiddenfiles "*mp4:*pdf:*txt"' -print-selection Another sophisticated "open file" dialog focusing on design: lf -command 'set nopreview; set ratios 1; set drawbox; set promptfmt "Select files [%w] %S q: cancel, l: confirm"' -print-selection Open Downloads and set `sortby` and `info` to creation date: lf -command 'set sortby btime; set info btime' ~/Downloads Temporarily prevent `lf` from modifying the command history: lf -command 'set nohistory' Use default settings and log current session: lf -config /dev/null -log /tmp/lf.log Force-quit the server: lf -remote 'quit!' Inherit lf's working directory in your shell: cd "$(lf -print-last-dir)" # QUICK REFERENCE The following commands are provided by lf: quit (default 'q') up (default 'k' and '') half-up (default '') page-up (default '' and '') scroll-up (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') scroll-down (default '') updir (default 'h' and '') open (default 'l' and '') jump-next (default ']') jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') high (default 'H') middle (default 'M') low (default 'L') toggle invert (default 'v') unselect (default 'u') glob-select glob-unselect copy (default 'y') cut (default 'd') paste (default 'p') clear (default 'c') sync draw redraw (default '') load reload (default '') delete (modal) rename (modal) (default 'r') read (modal) (default ':') shell (modal) (default '$') shell-pipe (modal) (default '%') shell-wait (modal) (default '!') shell-async (modal) (default '&') find (modal) (default 'f') find-back (modal) (default 'F') find-next (default ';') find-prev (default ',') search (modal) (default '/') search-back (modal) (default '?') search-next (default 'n') search-prev (default 'N') filter (modal) setfilter mark-save (modal) (default 'm') mark-load (modal) (default "'") mark-remove (modal) (default '"') tag tag-toggle (default 't') echo echomsg echoerr cd select source push addcustominfo calcdirsize clearmaps tty-write visual (default 'V') The following Visual mode commands are provided by lf: visual-accept (default 'V') visual-unselect visual-discard (default '') visual-change (default 'o') The following Command-line mode commands are provided by lf: cmd-insert cmd-escape (default '') cmd-complete (default '') cmd-menu-complete cmd-menu-complete-back cmd-menu-accept cmd-menu-discard cmd-enter (default '' and '') cmd-interrupt (default '') cmd-history-next (default '' and '') cmd-history-prev (default '' and '') cmd-left (default '' and '') cmd-right (default '' and '') cmd-home (default '' and '') cmd-end (default '' and '') cmd-delete (default '' and '') cmd-delete-back (default '') cmd-delete-home (default '') cmd-delete-end (default '') cmd-delete-unix-word (default '') cmd-yank (default '') cmd-transpose (default '') cmd-transpose-word (default '') cmd-word (default '') cmd-word-back (default '') cmd-delete-word (default '') cmd-delete-word-back (default '') cmd-capitalize-word (default '') cmd-uppercase-word (default '') cmd-lowercase-word (default '') The following options can be used to customize the behavior of lf: anchorfind bool (default true) autoquit bool (default true) borderfmt string (default "\033[0m") borderstyle string (default 'box') cleaner string (default '') copyfmt string (default "\033[7;33m") cursoractivefmt string (default "\033[7m") cursorparentfmt string (default "\033[7m") cursorpreviewfmt string (default "\033[4m") cutfmt string (default "\033[7;31m") dircounts bool (default false) dirfirst bool (default true) dironly bool (default false) dirpreviews bool (default false) drawbox bool (default false) dupfilefmt string (default '%f.~%n~') errorfmt string (default "\033[7;31;47m") filesep string (default "\n") filtermethod string (default 'text') findlen int (default 1) hidden bool (default false) hiddenfiles []string (default '.*' for Unix and '' for Windows) history bool (default true) icons bool (default false) ifs string (default '') ignorecase bool (default true) ignoredia bool (default true) incfilter bool (default false) incsearch bool (default false) info []string (default '') infotimefmtnew string (default 'Jan _2 15:04') infotimefmtold string (default 'Jan _2 2006') menufmt string (default "\033[0m") menuheaderfmt string (default "\033[1m") menuselectfmt string (default "\033[7m") mergeindicators bool (default false) mouse bool (default false) number bool (default false) numbercursorfmt string (default '') numberfmt string (default "\033[33m") period int (default 0) preload bool (default false) preserve []string (default "mode") preview bool (default true) previewer string (default '') promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m") ratios []int (default '1:2:3') relativenumber bool (default false) reverse bool (default false) rulerfile string (default "") rulerfmt string (default "") scrolloff int (default 0) searchmethod string (default 'text') selectfmt string (default "\033[7;35m") selmode string (default 'all') shell string (default 'sh' for Unix and 'cmd' for Windows) shellflag string (default '-c' for Unix and '/c' for Windows) shellopts []string (default '') showbinds bool (default true) sizeunits string (default 'binary') smartcase bool (default true) smartdia bool (default false) sortby string (default 'natural') statfmt string (default "\033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l") tabstop int (default 8) tagfmt string (default "\033[31m") tempmarks string (default '') terminalcursor string (default 'default') timefmt string (default 'Mon Jan _2 15:04:05 2006') truncatechar string (default '~') truncatepct int (default 100) visualfmt string (default "\033[7;36m") waitmsg string (default 'Press any key to continue') watch bool (default false) wrapscan bool (default true) wrapscroll bool (default false) user_{option} string (default none) The following environment variables are exported for shell commands: f fs fv fx id PWD OLDPWD LF_LEVEL OPENER VISUAL EDITOR PAGER SHELL lf lf_{option} lf_user_{option} lf_flag_{flag} lf_width lf_height lf_count lf_mode The following special shell commands are used to customize the behavior of lf when defined: open paste rename delete pre-cd on-cd on-load on-focus-gained on-focus-lost on-init on-select on-redraw on-quit The following commands/keybindings are provided by default: Unix cmd open &$OPENER "$f" map e $$EDITOR "$f" map i $$PAGER "$f" map w $$SHELL cmd help $$lf -doc | $PAGER map help cmd maps $lf -remote "query $id maps" | $PAGER cmd nmaps $lf -remote "query $id nmaps" | $PAGER cmd vmaps $lf -remote "query $id vmaps" | $PAGER cmd cmaps $lf -remote "query $id cmaps" | $PAGER cmd cmds $lf -remote "query $id cmds" | $PAGER Windows cmd open &%OPENER% %f% map e $%EDITOR% %f% map i !%PAGER% %f% map w $%SHELL% cmd help !%lf% -doc | %PAGER% map help cmd maps !%lf% -remote "query %id% maps" | %PAGER% cmd nmaps !%lf% -remote "query %id% nmaps" | %PAGER% cmd vmaps !%lf% -remote "query %id% vmaps" | %PAGER% cmd cmaps !%lf% -remote "query %id% cmaps" | %PAGER% cmd cmds !%lf% -remote "query %id% cmds" | %PAGER% The defaults for Windows are using `cmd` syntax. A `PowerShell` compatible configuration file can be found at https://github.com/gokcehan/lf/blob/master/etc/lfrc.ps1.example The following additional keybindings are provided by default: map zh set hidden! map zr set reverse! map zn set info map zs set info size map zt set info time map za set info size:time map sn :set sortby natural; set info map ss :set sortby size; set info size map st :set sortby time; set info time map sa :set sortby atime; set info atime map sb :set sortby btime; set info btime map sc :set sortby ctime; set info ctime map se :set sortby ext; set info map gh cd ~ nmap :toggle; down If the `mouse` option is enabled, mouse buttons have the following default effects: Left mouse button Click on a file or directory to select it. Right mouse button Enter a directory or open a file. Also works on the preview pane. Scroll wheel Move up or down. If Ctrl is pressed, scroll up or down. # CONFIGURATION Configuration files should be located at: OS system-wide user-specific Unix /etc/lf/lfrc ~/.config/lf/lfrc Windows C:\ProgramData\lf\lfrc C:\Users\\AppData\Roaming\lf\lfrc The colors file should be located at: OS system-wide user-specific Unix /etc/lf/colors ~/.config/lf/colors Windows C:\ProgramData\lf\colors C:\Users\\AppData\Roaming\lf\colors The icons file should be located at: OS system-wide user-specific Unix /etc/lf/icons ~/.config/lf/icons Windows C:\ProgramData\lf\icons C:\Users\\AppData\Roaming\lf\icons The selection file should be located at: Unix ~/.local/share/lf/files Windows C:\Users\\AppData\Local\lf\files The marks file should be located at: Unix ~/.local/share/lf/marks Windows C:\Users\\AppData\Local\lf\marks The tags file should be located at: Unix ~/.local/share/lf/tags Windows C:\Users\\AppData\Local\lf\tags The history file should be located at: Unix ~/.local/share/lf/history Windows C:\Users\\AppData\Local\lf\history You can configure these locations with the following variables given with their order of precedences and their default values: Unix $LF_CONFIG_HOME $XDG_CONFIG_HOME ~/.config $LF_DATA_HOME $XDG_DATA_HOME ~/.local/share Windows %LF_CONFIG_HOME% %XDG_CONFIG_HOME% %APPDATA% %LF_DATA_HOME% %XDG_DATA_HOME% %LOCALAPPDATA% A sample configuration file can be found at https://github.com/gokcehan/lf/blob/master/etc/lfrc.example # COMMANDS This section shows information about built-in commands. Modal commands do not take any arguments, but instead change the operation mode to read their input conveniently, and so they are meant to be assigned to keybindings. ## quit (default `q`) Quit lf and return to the shell. ## up (default `k` and ``), half-up (default ``), page-up (default `` and ``), scroll-up (default ``), down (default `j` and ``), half-down (default ``), page-down (default `` and ``), scroll-down (default ``) Move/scroll the current file selection upwards/downwards by one/half a page/full page. ## updir (default `h` and ``) Change the current working directory to the parent directory. ## open (default `l` and ``) If the current file is a directory, then change the current directory to it, otherwise, execute the `open` command. A default `open` command is provided to call the default system opener asynchronously with the current file as the argument. A custom `open` command can be defined to override this default. ## jump-next (default `]`), jump-prev (default `[`) Change the current working directory to the next/previous jumplist item. ## top (default `gg` and ``), bottom (default `G` and ``) Move the current file selection to the top/bottom of the directory. A count can be specified to move to a specific line, for example, use `3G` to move to the third line. ## high (default `H`), middle (default `M`), low (default `L`) Move the current file selection to the high/middle/low of the screen. ## toggle Toggle the selection of the current file or files given as arguments. ## invert (default `v`) Reverse the selection of all files in the current directory (i.e. `toggle` all files). Selections in other directories are not affected by this command. You can define a new command to select all files in the directory by combining `invert` with `unselect` (i.e. `cmd select-all :unselect; invert`), though this will also remove selections in other directories. ## unselect (default `u`) Remove the selection of all files in all directories. ## glob-select, glob-unselect Select/unselect files that match the given glob. ## copy (default `y`) Save the paths of selected files to the clipboard as files to be copied. If there are no selected files, the path of the current file is used instead. ## cut (default `d`) Save the paths of selected files to the clipboard as files to be moved. If there are no selected files, the path of the current file is used instead. ## paste (default `p`) Copy/Move files in the clipboard to the current working directory. A custom `paste` command can be defined to override this default. ## clear (default `c`) Clear file paths in the clipboard. ## sync Synchronize copied/cut files with the server. This command is automatically called when required. ## draw Draw the screen. This command is automatically called when required. ## redraw (default ``) Synchronize the terminal and redraw the screen. ## load Load modified files and directories. This command is automatically called when required. ## reload (default ``) Flush the cache and reload all files and directories. ## delete (modal) Remove the current file or selected file(s). A custom `delete` command can be defined to override this default. ## rename (modal) (default `r`) Rename the current file using the built-in method. A custom `rename` command can be defined to override this default. ## read (modal) (default `:`) Read a command to evaluate. ## shell (modal) (default `$`) Read a shell command to execute. ## shell-pipe (modal) (default `%`) Read a shell command to execute piping its standard I/O to the bottom statline. ## shell-wait (modal) (default `!`) Read a shell command to execute and wait for a key press at the end. ## shell-async (modal) (default `&`) Read a shell command to execute asynchronously without standard I/O. ## find (modal) (default `f`), find-back (modal) (default `F`), find-next (default `;`), find-prev (default `,`) Read key(s) to find the appropriate filename match in the forward/backward direction and jump to the next/previous match. ## search (default `/`), search-back (default `?`), search-next (default `n`), search-prev (default `N`) Read a pattern to search for a filename match in the forward/backward direction and jump to the next/previous match. ## filter (modal), setfilter Command `filter` reads a pattern to filter out and only view files matching the pattern. Command `setfilter` does the same but uses an argument to set the filter immediately. You can supply an argument to `filter` to use as the starting prompt. ## mark-save (modal) (default `m`) Save the current directory as a bookmark assigned to the given key. ## mark-load (modal) (default `'`) Change the current directory to the bookmark assigned to the given key. A special bookmark `'` holds the previous directory after a `mark-load`, `cd`, or `select` command. ## mark-remove (modal) (default `"`) Remove a bookmark assigned to the given key. ## tag Tag a file with `*` or a single-width character given in the argument. You can define a new tag-clearing command by combining `tag` with `tag-toggle` (i.e. `cmd tag-clear :tag; tag-toggle`). ## tag-toggle (default `t`) Tag a file with `*` or a single-width character given in the argument if the file is untagged, otherwise remove the tag. ## echo Print the given arguments to the message line at the bottom. ## echomsg Print the given arguments to the message line at the bottom and also to the log file. ## echoerr Print given arguments to the message line at the bottom as `errorfmt` and also to the log file. ## cd Change the working directory to the given argument. ## select Change the current file selection to the given argument. ## source Read the configuration file given in the argument. ## push Simulate key pushes given in the argument. ## addcustominfo Update the `custom` info and `.Stat.CustomInfo` field of the given file with the given string. The info string may contain ANSI escape codes to further customize its appearance. If no info is provided, clear the file's info instead. ## calcdirsize Calculate the total size for each of the selected directories. Option `info` should include `size` and option `dircounts` should be disabled to show this size. If the total size of a directory is not calculated, it will be shown as `-`. ## clearmaps Remove all keybindings associated with the `map`, `nmap` and `vmap` command. This command can be used in the config file to remove the default keybindings. For safety purposes, `:` is left mapped to the `read` command, and `cmap` keybindings are retained so that it is still possible to exit `lf` using `:quit`. ## tty-write Write the given string to the tty. This is useful for sending escape sequences to the terminal to control its behavior (e.g. OSC 0 to set the window title). Using `tty-write` is preferred over directly writing to `/dev/tty` because the latter is not synchronized and can interfere with drawing the UI. ## visual (default `V`) Switch to Visual mode. If already in Visual mode, discard the visual selection and stay in Visual mode. # VISUAL MODE COMMANDS ## visual-accept (default `V`) Add the visual selection to the selection list, quit Visual mode and return to Normal mode. ## visual-unselect Remove the visual selection from the selection list, quit Visual mode and return to Normal mode. ## visual-discard (default ``) Discard the visual selection, quit Visual mode and return to Normal mode. ## visual-change (default `o`) Go to the other end of the current Visual mode selection. # COMMAND-LINE MODE COMMANDS The prompt character specifies which of the several Command-line modes you are in. For example, the `read` command takes you to the `:` mode. When the cursor is at the first character in `:` mode, pressing one of the keys `!`, `$`, `%`, or `&` takes you to the corresponding mode. You can go back with `cmd-delete-back` (`` by default). The command line commands should be mostly compatible with readline keybindings. A character refers to a Unicode code point, a word consists of letters and digits, and a Unix word consists of any non-blank characters. ## cmd-insert Insert the character given in the argument. This command is automatically called when required. ## cmd-escape (default ``) Quit Command-line mode and return to Normal mode. ## cmd-complete (default ``) Autocomplete the current word. ## cmd-menu-complete, cmd-menu-complete-back Autocomplete the current word with the menu selection. You need to assign keys to these commands (e.g. `cmap cmd-menu-complete; cmap cmd-menu-complete-back`). You can use the assigned keys to display the menu and then cycle through completion options. ## cmd-menu-accept Accept the currently selected match in menu completion and close the menu. ## cmd-menu-discard Discard the currently selected match in menu completion and close the menu. ## cmd-enter (default `` and ``) Execute the current line. ## cmd-interrupt (default ``) Interrupt the current shell-pipe command and return to the Normal mode. ## cmd-history-next (default `` and ``), cmd-history-prev (default `` and ``) Go to the next/previous entry in the command history. If part of the command is already typed, then only matching entries will be considered, and consecutive duplicate entries are skipped. ## cmd-left (default `` and ``), cmd-right (default `` and ``) Move the cursor to the left/right. ## cmd-home (default `` and ``), cmd-end (default `` and ``) Move the cursor to the beginning/end of the line. ## cmd-delete (default `` and ``) Delete the next character. ## cmd-delete-back (default ``) Delete the previous character. When at the beginning of a prompt, returns either to Normal mode or to `:` mode. ## cmd-delete-home (default ``), cmd-delete-end (default ``) Delete everything up to the beginning/end of the line. ## cmd-delete-unix-word (default ``) Delete the previous Unix word. ## cmd-yank (default ``) Paste the buffer content containing the last deleted item. ## cmd-transpose (default ``) Swap the characters before and after the cursor, then move the cursor forward. If there is no character after the cursor, swap the previous two characters instead. ## cmd-transpose-word (default ``) Swap the words before and after the cursor, then move the cursor forward. If there is no word after the cursor, swap the previous two words instead. ## cmd-word (default ``), cmd-word-back (default ``) Move the cursor by one word in the forward/backward direction. ## cmd-delete-word (default ``) Delete the next word in the forward direction. ## cmd-delete-word-back (default ``) Delete the previous word in the backward direction. ## cmd-capitalize-word (default ``), cmd-uppercase-word (default ``), cmd-lowercase-word (default ``) Capitalize/uppercase/lowercase the current word and jump to the next word. # SETTINGS This section shows information about options to customize the behavior. Character `:` is used as the separator for list options `[]int` and `[]string`. ## anchorfind (bool) (default true) When this option is enabled, the find command starts matching patterns from the beginning of filenames, otherwise, it can match at an arbitrary position. ## autoquit (bool) (default true) Automatically quit the server when there are no clients left connected. ## borderfmt (string) (default `\033[0m`) Format string of border characters. ## borderstyle (string) (default `box`) Border style used by `drawbox`. The following styles are supported: box outline around all panes and separators between them roundbox like `box`, but with rounded outer corners outline outline around all panes roundoutline like `outline`, but with rounded outer corners separators separators between panes ## cleaner (string) (default ``) (not called if empty) Set the path of a cleaner file. The file should be executable. This file is called if previewing is enabled, the previewer is set, and the previously selected file has its preview cache disabled. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position of preview pane and (6) next filename to be previewed respectively. Preview cleaning is disabled when the value of this option is left empty. ## copyfmt (string) (default `\033[7;33m`) Format string of the indicator for files to be copied. ## cursoractivefmt (string) (default `\033[7m`), cursorparentfmt (string) (default `\033[7m`), cursorpreviewfmt (string) (default `\033[4m`) Format strings for highlighting the cursor. `cursoractivefmt` applies in the current directory pane, `cursorparentfmt` applies in panes that show parents of the current directory, and `cursorpreviewfmt` applies in panes that preview directories. The default is to make the active cursor and the parent directory cursor inverted. The preview cursor is underlined. Some other possibilities to consider for the preview or parent cursors: an empty string for no cursor, `\033[7;2m` for dimmed inverted text (visibility varies by terminal), `\033[7;90m` for inverted text with grey (aka "brightblack") background. If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. For example, `\033[4m%s\033[0m` has the same effect as `\033[4m`. ## cutfmt (string) (default `\033[7;31m`) Format string of the indicator for files to be cut. ## dircounts (bool) (default false) When this option is enabled, directory sizes show the number of items inside instead of the total size of the directory, which needs to be calculated for each directory using `calcdirsize`. This information needs to be calculated by reading the directory and counting the items inside. Therefore, this option is disabled by default for performance reasons. This option only has an effect when `info` has a `size` field and the pane is wide enough to show the information. 999 items are counted per directory at most, and bigger directories are shown as `999+`. ## dirfirst (bool) (default true) Show directories first above regular files. With `dircounts` enabled, sorting by `size` always separates directories and files, regardless of `dirfirst`. ## dironly (bool) (default false) Show only directories. ## dirpreviews (bool) (default false) If enabled, directories will also be passed to the previewer script. This allows custom previews for directories. ## drawbox (bool) (default false) Draw borders around panes using box drawing characters. ## dupfilefmt (string) (default `%f.~%n~`) Format string of filename when creating duplicate files. With the default format, copying a file `abc.txt` to the same directory will result in a duplicate file called `abc.txt.~1~`. Special expansions are provided, `%f` as the file name, `%b` for the base name (file name without extension), `%e` as the extension (including the dot) and `%n` as the number of duplicates. ## errorfmt (string) (default `\033[7;31;47m`) Format string of error messages shown in the bottom message line. If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. For example, `\033[4m%s\033[0m` has the same effect as `\033[4m`. ## filesep (string) (default `\n`) File separator used in environment variables `fs`, `fv` and `fx`. ## filtermethod (string) (default `text`) How filter command patterns are treated. Currently supported methods are `text` (i.e. string literals), `glob` (i.e. shell globs) and `regex` (i.e. regular expressions). See `SEARCHING FILES` for more details. ## findlen (int) (default 1) Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left. ## hidden (bool) (default false) Show hidden files. On Unix systems, hidden files are determined by the value of `hiddenfiles`. On Windows, files with hidden attributes are also considered hidden files. ## hiddenfiles ([]string) (default `.*` for Unix and `` for Windows) List of hidden file glob patterns. Patterns can be given as relative or absolute paths. Globbing supports the usual special characters, `*` to match any sequence, `?` to match any character, and `[...]` or `[^...]` to match character sets or ranges. In addition, if a pattern starts with `!`, then its matches are excluded from hidden files. To add multiple patterns, use `:` as a separator. Example: `.*:lost+found:*.bak` ## history (bool) (default true) Save command history. ## icons (bool) (default false) Show icons before each item in the list. ## ifs (string) (default ``) Sets `IFS` variable in shell commands. It works by adding the assignment to the beginning of the command string as `IFS=...; ...`. The reason is that `IFS` variable is not inherited by the shell for security reasons. This method assumes a POSIX shell syntax so it can fail for non-POSIX shells. This option has no effect when the value is left empty. This option does not have any effect on Windows. ## ignorecase (bool) (default true) Ignore case in sorting and search patterns. ## ignoredia (bool) (default true) Ignore diacritics in sorting and search patterns. ## incfilter (bool) (default false) Apply filter pattern after each keystroke during filtering. ## incsearch (bool) (default false) Jump to the first match after each keystroke during searching. ## info ([]string) (default ``) A list of information that is shown for directory items at the right side of the pane. The following information types are supported: perm file permission user user name group group name size file size time time of last data modification atime time of last access btime time of file birth ctime time of last status (inode) change custom property defined via `addcustominfo` (empty by default) Information is only shown when the pane width is more than twice the width of information. ## infotimefmtnew (string) (default `Jan _2 15:04`) Format string of the file time shown in the info column when it matches this year. ## infotimefmtold (string) (default `Jan _2 2006`) Format string of the file time shown in the info column when it doesn't match this year. ## menufmt (string) (default `\033[0m`) Format string of the menu. ## menuheaderfmt (string) (default `\033[1m`) Format string of the header row in the menu. ## menuselectfmt (string) (default `\033[7m`) Format string of the currently selected item in the menu. ## mergeindicators (bool) (default false) When `mergeindicators` is enabled, tag and selection indicators are drawn in a single column to reduce the gap before filenames. If a file is both tagged and selected, the tag uses the selection format (e.g. `copyfmt`) instead of `tagfmt`. ## mouse (bool) (default false) Send mouse events as input. ## number (bool) (default false) Show the position number for directory items on the left side of the pane. When the `relativenumber` option is enabled, only the current line shows the absolute position and relative positions are shown for the rest. ## numberfmt (string) (default `\033[33m`), numbercursorfmt (string) (default ``) Format strings for highlighting line numbers. `numberfmt` applies to all lines. `numbercursorfmt` applies to the cursor line and falls back to `numberfmt` when left empty. ## period (int) (default 0) Set the interval in seconds for periodic checks of directory updates. This works by periodically calling the `load` command. Note that directories are already updated automatically in many cases. This option can be useful when there is an external process changing the displayed directory and you are not doing anything in lf. Periodic checks are disabled when the value of this option is set to zero. ## preload (bool) (default false) Allow previews to be generated in advance using the `previewer` script as the user navigates through the filesystem. ## preserve ([]string) (default `mode`) List of attributes that are preserved when copying files. Currently supported attributes are `mode` (i.e. access mode) and `timestamps` (i.e. modification time and access time). Note that preserving other attributes like ownership of change/birth timestamp is desirable, but not portably supported in Go. ## preview (bool) (default true) Show previews of files and directories at the rightmost pane. If the file has more lines than the preview pane, the rest of the lines are not read. Files containing the null character (U+0000) in the read portion are considered binary files and displayed as `binary`. ## previewer (string) (default ``) (not filtered if empty) Set the path of a previewer file to filter the content of regular files for previewing. The file should be executable. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode ("preview" or "preload"). SIGPIPE signal is sent when enough lines are read. If the previewer returns a non-zero exit code, then the preview cache for the given file is disabled. This means that if the file is selected in the future, the previewer is called once again. Preview filtering is disabled and files are displayed as they are when the value of this option is left empty. If the `preload` option is enabled, then this will be called with `preload` as the mode when preloading file previews. Refer to the [PREVIEWING FILES section](https://github.com/gokcehan/lf/blob/master/doc.md#previewing-files) for more information about how to configure custom previews. ## promptfmt (string) (default `\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m`) Format string of the prompt shown in the top line. The following special expansions are supported: %f file name %h host name %u user name %w working directory %d working directory (with trailing path separator) %F current filter %S spacer to right-align the following parts (can be used once) The home folder is shown as `~` in the working directory expansion. Directory names are automatically shortened to a single character starting from the leftmost parent when the prompt does not fit the screen. ## ratios ([]int) (default `1:2:3`) List of ratios of pane widths. Number of items in the list determines the number of panes in the UI. When the `preview` option is enabled, the rightmost number is used for the width of the preview pane. ## relativenumber (bool) (default false) Show the position number relative to the current line. When `number` is enabled, the current line shows the absolute position, otherwise nothing is shown. ## reverse (bool) (default false) Reverse the direction of sort. ## rulerfile (string) (default ``) Set the path of the ruler file. If not set, then a default template will be used for the ruler. Refer to the [RULER section](https://github.com/gokcehan/lf/blob/master/doc.md#ruler) for more information about how the ruler file works. ## rulerfmt (string) (default ``) Format string of the ruler shown in the bottom right corner. When set, it will be used along with `statfmt` to draw the ruler, and `rulerfile` will be ignored. However, using `rulerfile` is preferred and this option is provided for backwards compatibility. The following special expansions are supported: %a pressed keys %p progress of file operations %m number of files to be cut (moved) %c number of files to be copied %s number of selected files %v number of visually selected files %t number of shown files in the current directory %h number of hidden files in the current directory %f current filter %i cursor position %P scroll percentage %d amount of free disk space Additional expansions are provided for environment variables exported by lf, in the form `%{lf_}` (e.g. `%{lf_selmode}`). This is useful for displaying the current settings. Expansions are also provided for user-defined options, in the form `%{lf_user_}` (e.g. `%{lf_user_foo}`). The `|` character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown. ## scrolloff (int) (default 0) Minimum number of offset lines shown at all times at the top and bottom of the screen when scrolling. The current line is kept in the middle when this option is set to a large value that is bigger than half the number of lines. A smaller offset can be used when the current file is close to the beginning or end of the list to show the maximum number of items. ## searchmethod (string) (default `text`) How search command patterns are treated. Currently supported methods are `text` (i.e. string literals), `glob` (i.e. shell globs) and `regex` (i.e. regular expressions). See `SEARCHING FILES` for more details. ## selectfmt (string) (default `\033[7;35m`) Format string of the indicator for files that are selected. ## selmode (string) (default `all`) Selection mode for commands. When set to `all` it will use the selected files from all directories. When set to `dir` it will only use the selected files in the current directory. ## shell (string) (default `sh` for Unix and `cmd` for Windows) Shell executable to use for shell commands. Shell commands are executed as `shell shellopts shellflag command -- arguments`. ## shellflag (string) (default `-c` for Unix and `/c` for Windows) Command line flag used to pass shell commands. ## shellopts ([]string) (default ``) List of shell options to pass to the shell executable. ## showbinds (bool) (default true) Show bindings associated with pressed keys. ## sizeunits (string) (default `binary`) Determines whether file sizes are displayed using binary units (`1K` is 1024 bytes) or decimal units (`1K` is 1000 bytes). ## smartcase (bool) (default true) Override `ignorecase` option when the pattern contains an uppercase character. This option has no effect when `ignorecase` is disabled. ## smartdia (bool) (default false) Override `ignoredia` option when the pattern contains a character with diacritic. This option has no effect when `ignoredia` is disabled. ## sortby (string) (default `natural`) Sort type for directories. The following sort types are supported: natural file name (track_2.flac comes before track_10.flac) name file name (track_10.flac comes before track_2.flac) ext file extension size file size time time of last data modification atime time of last access btime time of file birth ctime time of last status (inode) change custom property defined via `addcustominfo` (empty by default) ## statfmt (string) (default `\033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l`) Format string of the file info shown in the bottom left corner. This option has no effect unless `rulerfmt` is also set. Using `rulerfile` is preferred and this option is provided for backwards compatibility. The following special expansions are supported: %p file permission %c link count %u user name %g group name %s file size %S file size (left-padded with spaces to a fixed width of 5 characters) %t time of last data modification %l link target %m current mode %M current mode (displaying `NORMAL` instead of a blank string in Normal mode) The `|` character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown. ## tabstop (int) (default 8) Number of space characters to show for horizontal tabulation (U+0009) character. ## tagfmt (string) (default `\033[31m`) Format string of the tags. If the format string contains the characters `%s`, it is interpreted as a format string for `fmt.Sprintf`. Such a string should end with the terminal reset sequence. For example, `\033[4m%s\033[0m` has the same effect as `\033[4m`. ## tempmarks (string) (default ``) Marks to be considered temporary (e.g. `abc` refers to marks `a`, `b`, and `c`). These marks are not synced to other clients and they are not saved in the bookmarks file. Note that the special bookmark `'` is always treated as temporary and it does not need to be specified. ## terminalcursor (string) (default `default`) Set the appearance of the terminal cursor for prompts shown in the bottom line. Currently supported values are `default`, `block`, `underline`, `bar`, `blinkblock`, `blinkunderline` and `blinkbar`. ## timefmt (string) (default `Mon Jan _2 15:04:05 2006`) Format string of the file modification time shown in the bottom line. ## truncatechar (string) (default `~`) The truncate character that is shown at the end when the filename does not fit into the pane. ## truncatepct (int) (default 100) When a filename is too long to be shown completely, the available space will be partitioned into two parts. `truncatepct` is a percentage value between 0 and 100 that determines the size of the first part, which will be shown at the beginning of the filename. The second part uses the rest of the available space, and will be shown at the end of the filename. Both parts are separated by the truncation character (`truncatechar`). Truncation is not applied to the file extension. For example, with the filename `very_long_filename.txt`: - `set truncatepct 100` -> `very_long_filena~.txt` (default) - `set truncatepct 50` -> `very_lon~filename.txt` - `set truncatepct 0` -> `~ry_long_filename.txt` ## visualfmt (string) (default `\033[7;36m`) Format string of the indicator for files that are visually selected. ## waitmsg (string) (default `Press any key to continue`) String shown after commands of shell-wait type. ## watch (bool) (default false) Watch the filesystem for changes using `fsnotify` to automatically refresh file information. FUSE is currently not supported due to limitations in `fsnotify`. ## wrapscan (bool) (default true) Searching can wrap around the file list. ## wrapscroll (bool) (default false) Scrolling can wrap around the file list. ## user_{option} (string) (default none) Any option that is prefixed with `user_` is a user-defined option and can be set to any string. Inside a user-defined command, the value will be provided in the `lf_user_{option}` environment variable. These options are not used by lf and are not persisted. # ENVIRONMENT VARIABLES The following variables are exported for shell commands: These are referred to with a `$` prefix on POSIX shells (e.g. `$f`), between `%` characters on Windows cmd (e.g. `%f%`), and with a `$env:` prefix on Windows PowerShell (e.g. `$env:f`). ## f Current file selection as a full path. ## fs Selected file(s) separated with the value of `filesep` option as full path(s). ## fv Visually selected file(s) separated with the value of `filesep` option as full path(s). ## fx Selected file(s) (i.e. `fs`, never `fv`) if there are any selected files, otherwise current file selection (i.e. `f`). ## id Id of the running client. ## PWD Present working directory. ## OLDPWD Initial working directory. ## LF_LEVEL The value of this variable is set to the current nesting level when you run lf from a shell spawned inside lf. You can add the value of this variable to your shell prompt to make it clear that your shell runs inside lf. For example, with POSIX shells, you can use `[ -n "$LF_LEVEL" ] && PS1="$PS1""(lf level: $LF_LEVEL) "` in your shell configuration file (e.g. `~/.bashrc`). ## OPENER If this variable is set in the environment, use the same value. Otherwise, this is set to `start` in Windows, `open` in macOS, `xdg-open` in others. ## EDITOR If VISUAL is set in the environment, use its value. Otherwise, use the value of the environment variable EDITOR. If neither variable is set, this is set to `vi` on Unix, `notepad` in Windows. ## PAGER If this variable is set in the environment, use the same value. Otherwise, this is set to `less` on Unix, `more` in Windows. ## SHELL If this variable is set in the environment, use the same value. Otherwise, this is set to `sh` on Unix, `cmd` in Windows. ## lf Absolute path to the currently running lf binary, if it can be found. Otherwise, this is set to the string `lf`. ## lf_{option} Value of the {option}. ## lf_user_{option} Value of the user_{option}. ## lf_flag_{flag} Value of the command line {flag}. ## lf_width, lf_height Width/Height of the terminal. ## lf_count Value of the count associated with the current command. ## lf_mode Current mode that `lf` is operating in. This is useful for customizing keybindings depending on what the current mode is. Possible values are `compmenu`, `delete`, `rename`, `filter`, `find`, `mark`, `search`, `command`, `shell`, `pipe` (when running a shell-pipe command), `normal`, `visual` and `unknown`. # SPECIAL COMMANDS This section shows information about special shell commands. ## open This shell command can be defined to override the default `open` command when the current file is not a directory. ## paste This shell command can be defined to override the default `paste` command. ## rename This shell command can be defined to override the default `rename` command. ## delete This shell command can be defined to override the default `delete` command. ## pre-cd This shell command can be defined to be executed before changing a directory. ## on-cd This shell command can be defined to be executed after changing a directory. ## on-load This shell command can be defined to be executed after loading a directory. It provides the files inside the directory as arguments. ## on-focus-gained This shell command can be defined to be executed when the terminal gains focus. ## on-focus-lost This shell command can be defined to be executed when the terminal loses focus. ## on-init This shell command can be defined to be executed after initializing and connecting to the server. ## on-select This shell command can be defined to be executed after the selection changes. ## on-redraw This shell command can be defined to be executed after the screen is redrawn or if the terminal is resized. ## on-quit This shell command can be defined to be executed before quitting. # PREFIXES The following command prefixes are used by lf: : read (default) built-in/custom command $ shell shell command % shell-pipe shell command running with the UI ! shell-wait shell command waiting for a key press & shell-async shell command running asynchronously The same evaluator is used for the command line and the configuration file for reading shell commands. The difference is that prefixes are not necessary in the command line. Instead, different modes are provided to read corresponding commands. These modes are mapped to the prefix keys above by default. Visual mode mappings are defined the same way Normal mode mappings are defined. # SYNTAX Characters from `#` to newline are comments and ignored: # comments start with `#` The following commands (`set`, `setlocal`, `map`, `nmap`, `vmap`, `cmap`, and `cmd`) are used for configuration. Command `set` is used to set an option which can be a boolean, integer, or string: set hidden # boolean enable set hidden true # boolean enable set nohidden # boolean disable set hidden false # boolean disable set hidden! # boolean toggle set scrolloff 10 # integer value set sortby time # string value without quotes set sortby 'time' # string value with single quotes (whitespace) set sortby "time" # string value with double quotes (backslash escapes) Command `setlocal` is used to set a local option for a directory which can be a boolean or string. Currently supported local options are `dircounts`, `dirfirst`, `dironly`, `hidden`, `info`, `reverse` and `sortby`. setlocal /foo/bar hidden # boolean enable setlocal /foo/bar hidden true # boolean enable setlocal /foo/bar nohidden # boolean disable setlocal /foo/bar hidden false # boolean disable setlocal /foo/bar hidden! # boolean toggle setlocal /foo/bar sortby time # string value without quotes setlocal /foo/bar sortby 'time' # string value with single quotes (whitespace) setlocal /foo/bar sortby "time" # string value with double quotes (backslash escapes) Command `map` is used to bind a key in Normal and Visual mode to a command which can be a built-in command, custom command, or shell command: map gh cd ~ # built-in command map D trash # custom command map i $less $f # shell command map U !du -csh * # waiting shell command Command `nmap` does the same but for Normal mode only. Command `vmap` does the same but for Visual mode only. Overview of which map command works in which mode: map Normal, Visual nmap Normal vmap Visual cmap Command-line Command `cmap` is used to bind a key on the command line to a command line command or any other command: cmap cmd-escape cmap set incsearch! You can delete an existing binding by leaving the expression empty: map gh # deletes 'gh' mapping in Normal and Visual mode nmap v # deletes 'v' mapping in Normal mode vmap o # deletes 'o' mapping in Visual mode cmap # deletes '' mapping Command `cmd` is used to define a custom command: cmd usage $du -h -d1 | less You can delete an existing command by leaving the expression empty: cmd trash # deletes 'trash' command If there is no prefix then `:` is assumed: map zt set info time An explicit `:` can be provided to group statements until a newline which is especially useful for `map` and `cmd` commands: map st :set sortby time; set info time If you need multiline you can wrap statements in `{{` and `}}` after the proper prefix. map st :{{ set sortby time set info time }} # KEY MAPPINGS Regular keys are assigned to a command with the usual syntax: map a down Keys combined with the Shift key simply use the uppercase letter: map A down Special keys are written in between `<` and `>` characters and always use lowercase letters: map down Angle brackets can be assigned with their special names: map down map down Function keys are prefixed with an `f` character: map down Keys combined with the Ctrl key are prefixed with a `c` character: map down Keys combined with the Alt key are assigned in two different ways depending on the behavior of your terminal. Older terminals (e.g. xterm) may set the 8th bit of a character when the Alt key is pressed. On these terminals, you can use the corresponding byte for the mapping: map á down Newer terminals (e.g. gnome-terminal) may prefix the key with an escape character when the Alt key is pressed. lf uses the escape delaying mechanism to recognize Alt keys in these terminals (delay is 100ms). On these terminals, keys combined with the Alt key are prefixed with an `a` character: map down It is possible to combine special keys with modifiers: map down Combining multiple modifiers (e.g. `Ctrl+Shift+Space`) is not supported. Note that lf's key mapping syntax is similar to Vim's, but not identical. Some special keys and modifiers use different names and separators, and key names are matched literally (i.e. no case-folding, no aliases), so some familiar forms will not work: map down # not , or map down # not map down # not or (Meta) map down # not map down # not WARNING: Some key combinations will likely be intercepted by your OS, window manager, or terminal. Other key combinations cannot be recognized by lf due to the way terminals work (e.g. `Ctrl+h` combination sends a backspace key instead). The easiest way to find out the name of a key combination and whether it will work on your system is to press the key while lf is running and read the name from the `unknown mapping` error. Mouse buttons are prefixed with an `m` character: map down # primary map down # secondary map down # middle map down # thumb next map down # thumb prev map down map down map down Mouse wheel events are also prefixed with an `m` character: map down map down map down map down # PUSH MAPPINGS The usual way to map a key sequence is to assign it to a named or unnamed command. While this provides a clean way to remap built-in keys as well as other commands, it can be limiting at times. For this reason, the `push` command is provided by lf. This command is used to simulate key pushes given as its arguments. You can `map` a key to a `push` command with an argument to create various keybindings. This is mainly useful for two purposes. First, it can be used to map a command with a command count: map push 10j Second, it can be used to avoid typing the name when a command takes arguments: map r push :rename One thing to be careful of is that since the `push` command works with keys instead of commands it is possible to accidentally create recursive bindings: map j push 2j These types of bindings create a deadlock when executed. # SHELL COMMANDS Regular shell commands are the most basic command type that is useful for many purposes. For example, we can write a shell command to move the selected file(s) to trash. A first attempt to write such a command may look like this: cmd trash ${{ mkdir -p ~/.trash if [ -z "$fs" ]; then mv "$f" ~/.trash else IFS="$(printf '\n\t')"; mv $fs ~/.trash fi }} We check `$fs` to see if there are any selected files. Otherwise, we just delete the current file. Since this is such a common pattern, a separate `$fx` variable is provided. We can use this variable to get rid of the conditional: cmd trash ${{ mkdir -p ~/.trash IFS="$(printf '\n\t')"; mv $fx ~/.trash }} The trash directory is checked each time the command is executed. We can move it outside of the command so it would only run once at startup: ${{ mkdir -p ~/.trash }} cmd trash ${{ IFS="$(printf '\n\t')"; mv $fx ~/.trash }} Since these are one-liners, we can drop `{{` and `}}`: $mkdir -p ~/.trash cmd trash $IFS="$(printf '\n\t')"; mv $fx ~/.trash Finally, note that we set the `IFS` variable manually in these commands. Instead, we could use the `ifs` option to set it for all shell commands (i.e. `set ifs "\n"`). This can be especially useful for interactive use (e.g. `$rm $f` or `$rm $fs` would simply work). This option is not set by default as it can behave unexpectedly for new users. However, use of this option is highly recommended and it is assumed in the rest of the documentation. # PIPING SHELL COMMANDS Regular shell commands have some limitations in some cases. When an output or error message is given and the command exits afterwards, the UI is immediately resumed and there is no way to see the message without dropping to shell again. Also, even when there is no output or error, the UI still needs to be paused while the command is running. This can cause flickering on the screen for short commands and similar distractions for longer commands. Instead of pausing the UI, piping shell commands connect stdin, stdout, and stderr of the command to the statline at the bottom of the UI. This can be useful for programs following the Unix philosophy to give no output in the success case, and brief error messages or prompts in other cases. For example, the following rename command prompts for overwrite in the statline if there is an existing file with the given name: cmd rename %mv -i $f $1 You can also output error messages in the command and they will show up in the statline. For example, an alternative rename command may look like this: cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1 Note that input is line buffered and output and error are byte buffered. # WAITING SHELL COMMANDS Waiting shell commands are similar to regular shell commands except that they wait for a key press when the command is finished. These can be useful to see the output of a program before the UI is resumed. Waiting shell commands are more appropriate than piping shell commands when the command is verbose and the output is best displayed as multiline. # ASYNCHRONOUS SHELL COMMANDS Asynchronous shell commands are used to start a command in the background and then resume operation without waiting for the command to finish. Stdin, stdout, and stderr of the command are neither connected to the terminal nor the UI. # REMOTE COMMANDS One of the more advanced features in lf is remote commands. All clients connect to a server on startup. It is possible to send commands to all or any of the connected clients over the common server. This is used internally to notify file selection changes to other clients. To use this feature, you need to use a client which supports communicating with a Unix domain socket. OpenBSD implementation of netcat (nc) is one such example. You can use it to send a command to the socket file: echo 'send echo hello world' | nc -U ${XDG_RUNTIME_DIR:-/tmp}/lf.${USER}.sock Since such a client may not be available everywhere, lf comes bundled with a command line flag to be used as such. When using lf, you do not need to specify the address of the socket file. This is the recommended way of using remote commands since it is shorter and immune to socket file address changes: lf -remote 'send echo hello world' In this command `send` is used to send the rest of the string as a command to all connected clients. You can optionally give it an ID number to send a command to a single client: lf -remote 'send 1234 echo hello world' All clients have a unique ID number but you may not be aware of the ID number when you are writing a command. For this purpose, an `$id` variable is exported to the environment for shell commands. The value of this variable is set to the process ID of the client. You can use it to send a remote command from a client to the server which in return sends a command back to itself. So now you can display a message in the current client by calling the following in a shell command: lf -remote "send $id echo hello world" Since lf does not have control flow syntax, remote commands are used for such needs. For example, you can configure the number of columns in the UI with respect to the terminal width as follows: cmd recol %{{ if [ $lf_width -le 80 ]; then lf -remote "send $id set ratios 1:2" elif [ $lf_width -le 160 ]; then lf -remote "send $id set ratios 1:2:3" else lf -remote "send $id set ratios 1:2:3:5" fi }} In addition, the `query` command can be used to obtain information about a specific lf instance by providing its ID: lf -remote "query $id maps" The following types of information are supported: maps list of mappings created by the 'map', 'nmap' and 'vmap' command nmaps list of mappings created by the 'nmap' and 'map' command vmaps list of mappings created by the 'vmap' and 'map' command cmaps list of mappings created by the 'cmap' command cmds list of commands created by the 'cmd' command jumps contents of the jump list, showing previously visited locations history list of previously executed commands on the command line files list of files in the currently open directory as displayed by lf, empty if dir is still loading When listing mappings the characters in the first column are: n Normal v Visual c Command-line This is useful for scripting actions based on the internal state of lf. For example, to select a previous command using fzf and execute it: map ${{ clear cmd=$( lf -remote "query $id history" | awk -F'\t' 'NR > 1 { print $NF}' | sort -u | fzf --reverse --prompt='Execute command: ' ) lf -remote "send $id $cmd" }} The `list` command prints the IDs of all currently connected clients: lf -remote 'list' There is also a `quit` command to quit the server when there are no connected clients left, and a `quit!` command to force quit the server by closing client connections first: lf -remote 'quit' lf -remote 'quit!' Lastly, the commands `conn` and `drop` connect or disconnect ID to/from the server: lf -remote 'conn $id' lf -remote 'drop $id' These are internal and generally not needed by users. # FILE OPERATIONS lf uses its own built-in copy and move operations by default. These are implemented as asynchronous operations and progress is shown in the bottom ruler. These commands do not overwrite existing files or directories with the same name. Instead, a suffix that is compatible with the `--backup=numbered` option in GNU cp is added to the new files or directories. Only file modes and (some) timestamps can be preserved (see `preserve` option), all other attributes are ignored including ownership, context, and xattr. Special files such as character and block devices, named pipes, and sockets are skipped and links are not followed. Moving is performed using the rename operation of the underlying OS. For cross-device moving, lf falls back to copying and then deletes the original files if there are no errors. Operation errors are shown in the message line as well as the log file and they do not prematurely terminate the corresponding file operation. File operations can be performed on the currently selected file or on multiple files by selecting them first. When you `copy` a file, lf doesn't actually copy the file on the disk, but only records its name to a file. The actual file copying takes place when you `paste`. Similarly `paste` after a `cut` operation moves the file. You can customize copy and move operations by defining a `paste` command. This is a special command that is called when it is defined instead of the built-in implementation. You can use the following example as a starting point: cmd paste %{{ load=$(cat ~/.local/share/lf/files) mode=$(echo "$load" | sed -n '1p') list=$(echo "$load" | sed '1d') if [ $mode = 'copy' ]; then cp -R $list . elif [ $mode = 'move' ]; then mv $list . rm ~/.local/share/lf/files lf -remote 'send clear' fi }} Some useful things to be considered are to use the backup (`--backup`) and/or preserve attributes (`-a`) options with `cp` and `mv` commands if they support it (i.e. GNU implementation), change the command type to asynchronous, or use `rsync` command with progress bar option for copying and feed the progress to the client periodically with remote `echo` calls. By default, lf does not assign `delete` command to a key to protect new users. You can customize file deletion by defining a `delete` command. You can also assign a key to this command if you like. An example command to move selected files to a trash folder and remove files completely after a prompt is provided in the example configuration file. # SEARCHING FILES There are two mechanisms implemented in lf to search a file in the current directory. Searching is the traditional method to move the selection to a file matching a given pattern. Finding is an alternative way to search for a pattern possibly using fewer keystrokes. The searching mechanism is implemented with commands `search` (default `/`), `search-back` (default `?`), `search-next` (default `n`), and `search-prev` (default `N`). You can set `searchmethod` to `glob` to match using a glob pattern. Globbing supports `*` to match any sequence, `?` to match any character, and `[...]` or `[^...]` to match character sets or ranges. You can set `searchmethod` to `regex` to match using a regex pattern. For a full overview of Go's RE2 syntax, see https://pkg.go.dev/regexp/syntax. You can enable `incsearch` option to jump to the current match at each keystroke while typing. In this mode, you can either use `cmd-enter` to accept the search or use `cmd-escape` to cancel the search. You can also map some other commands with `cmap` to accept the search and execute the command immediately afterwards. For example, you can use the right arrow key to finish the search and open the selected file with the following mapping: cmap :cmd-enter; open The finding mechanism is implemented with commands `find` (default `f`), `find-back` (default `F`), `find-next` (default `;`), `find-prev` (default `,`). You can disable `anchorfind` option to match a pattern at an arbitrary position in the filename instead of the beginning. You can set the number of keys to match using `findlen` option. If you set this value to zero, then the keys are read until there is only a single match. The default values of these two options are set to jump to the first file with the given initial. Some options affect both searching and finding. You can disable `wrapscan` option to prevent searches from being wrapped around at the end of the file list. You can disable `ignorecase` option to match cases in the pattern and the filename. This option is already automatically overridden if the pattern contains uppercase characters. You can disable `smartcase` option to disable this behavior. Two similar options `ignoredia` and `smartdia` are provided to control matching diacritics in Latin letters. # OPENING FILES You can define an `open` command (default `l` and ``) to configure file opening. This command is only called when the current file is not a directory, otherwise, the directory is entered instead. You can define it just as you would define any other command: cmd open $vi $fx It is possible to use different command types: cmd open &xdg-open $f You may want to use either file extensions or MIME types from `file` command: cmd open ${{ case $(file --mime-type -Lb $f) in text/*) vi $fx;; *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} You may want to use `setsid` before your opener command to have persistent processes that continue to run after lf quits. Regular shell commands (i.e. `$`) drop to the terminal which results in a flicker for commands that finish immediately (e.g. `xdg-open` in the above example). If you want to use asynchronous shell commands (i.e. `&`) but also want to use the terminal when necessary (e.g. `vi` in the above example), you can use a remote command: cmd open &{{ case $(file --mime-type -Lb $f) in text/*) lf -remote "send $id \$vi \$fx";; *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} Note that asynchronous shell commands run in their own process group by default so they do not require the manual use of `setsid`. The following command is provided by default: cmd open &$OPENER $f You may also use any other existing file openers as you like. Possible options are `libfile-mimeinfo-perl` (executable name is `mimeopen`), `rifle` (ranger's default file opener), or `mimeo` to name a few. # PREVIEWING FILES lf previews files on the preview pane by printing the file until the end or until the preview pane is filled. This output can be enhanced by providing a custom preview script for filtering. This can be used to highlight source code, list contents of archive files or view PDF or image files to name a few. For coloring lf recognizes ANSI escape codes. To use this feature, you need to set the value of `previewer` option to the path of an executable file. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode ("preview" or "preload"). The output of the execution is printed in the preview pane. Different types of files can be handled by matching by extension (or MIME type from the `file` command): #!/bin/sh case "$1" in *.tar*) tar tf "$1";; *.zip) unzip -l "$1";; *.rar) unrar l "$1";; *.7z) 7z l "$1";; *.pdf) pdftotext "$1" -;; *) highlight -O ansi "$1";; esac Because files can be large, lf automatically closes the previewer script output pipe with a SIGPIPE when enough lines are read. Note that some programs may not respond well to SIGPIPE and will exit with a non-zero return code, which avoids caching. You may add a trailing `|| true` command to avoid such errors: highlight -O ansi "$1" || true You may also want to use the same script in your pager mapping as well: set previewer ~/.config/lf/pv.sh map i $~/.config/lf/pv.sh $f | less -R For `less` pager, you may instead utilize `LESSOPEN` mechanism so that useful information about the file such as the full path of the file can still be displayed in the statusline below: set previewer ~/.config/lf/pv.sh map i $LESSOPEN='| ~/.config/lf/pv.sh %s' less -R $f Since the preview script is called for each file selection change, it may not generate previews fast enough if the user scrolls through files quickly. To deal with this, the `preload` option can be set to enable file previews to be preloaded in advance. If enabled, the preview script will be run on files in advance as the user navigates through them. In this case, if the exit code of the preview script is zero, then the output will be cached in memory and displayed by lf (useful for text or sixel previews). Otherwise, it will fallback to calling the preview script again when the file is actually selected (useful for previews managed by an external program). # CHANGING DIRECTORY lf changes the working directory of the process to the current directory so that shell commands always work in the displayed directory. After quitting, it returns to the original directory where it is first launched like all shell programs. If you want to stay in the current directory after quitting, you can use one of the example lfcd wrapper shell scripts provided in the repository at https://github.com/gokcehan/lf/tree/master/etc There is a special command `on-cd` that runs a shell command when it is defined and the directory is changed. You can define it just as you would define any other command: cmd on-cd &{{ bash -c ' # display git repository status in your prompt source /usr/share/git/completion/git-prompt.sh GIT_PS1_SHOWDIRTYSTATE=auto GIT_PS1_SHOWSTASHSTATE=auto GIT_PS1_SHOWUNTRACKEDFILES=auto GIT_PS1_SHOWUPSTREAM=auto git=$(__git_ps1 " (%s)") fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f$git\033[0m" lf -remote "send $id set promptfmt \"$fmt\"" ' }} If you want to send escape sequences to the terminal, you can use the `tty-write` command to do so. The following xterm-specific escape sequence sets the terminal title to the working directory: cmd on-cd &{{ lf -remote "send $id tty-write \"\033]0;$PWD\007\"" }} This command runs whenever you change the directory but not on startup. You can add an extra call to make it run on startup as well: cmd on-cd &{{ ... }} on-cd Note that all shell commands are possible but `%` and `&` are usually more appropriate as `$` and `!` causes flickers and pauses respectively. There is also a `pre-cd` command, that works like `on-cd`, but is run before the directory is actually changed. Another related command is `on-load` which gets executed when loading a directory. # LOADING DIRECTORY Similar to `on-cd` there also is `on-load` that when defined runs a shell command after loading a directory. It works well when combined with `addcustominfo`. The following example can be used to display git indicators in the info column: cmd on-load &{{ cd "$(dirname "$1")" || exit 1 [ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" = false ] || exit 0 cmds="" for file in "$@"; do case "$file" in */.git|*/.git/*) continue;; esac status=$(git status --porcelain --ignored -- "$file" | cut -c1-2 | head -n1) if [ -n "$status" ]; then cmds="${cmds}addcustominfo \"${file}\" \"$status\"; " else cmds="${cmds}addcustominfo \"${file}\" ''; " fi done if [ -n "$cmds" ]; then lf -remote "send $id :$cmds" fi }} Another use case could be showing the dimensions of images and videos: cmd on-load &{{ cmds="" for file in "$@"; do mime=$(file --mime-type -Lb -- "$file") case "$mime" in # vector images cause problems image/svg+xml) ;; image/*|video/*) dimensions=$(exiftool -s3 -imagesize -- "$file") cmds="${cmds}addcustominfo \"${file}\" \"$dimensions\"; " ;; esac done if [ -n "$cmds" ]; then lf -remote "send $id :$cmds" fi }} # COLORS lf tries to automatically adapt its colors to the environment. It starts with a default color scheme and updates colors using values of existing environment variables possibly by overwriting its previous values. Colors are set in the following order: 1. default 2. LSCOLORS (macOS/BSD ls) 3. LS_COLORS (GNU ls) 4. LF_COLORS (lf specific) 5. colors file (lf specific) Please refer to the corresponding man pages for more information about `LSCOLORS` and `LS_COLORS`. `LF_COLORS` is provided with the same syntax as `LS_COLORS` in case you want to configure colors only for lf but not ls. This can be useful since there are some differences between ls and lf, though one should expect the same behavior for common cases. The colors file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)) is provided for easier configuration without environment variables. This file should consist of whitespace-separated pairs with a `#` character to start comments until the end of the line. You can configure lf colors in two different ways. First, you can only configure 8 basic colors used by your terminal and lf should pick up those colors automatically. Depending on your terminal, you should be able to select your colors from a 24-bit palette. This is the recommended approach as colors used by other programs will also match each other. Second, you can set the values of environment variables or colors file mentioned above for fine-grained customization. Note that `LS_COLORS/LF_COLORS` are more powerful than `LSCOLORS` and they can be used even when GNU programs are not installed on the system. You can combine this second method with the first method for the best results. Lastly, you may also want to configure the colors of the prompt line to match the rest of the colors. Colors of the prompt line can be configured using the `promptfmt` option which can include hardcoded colors as ANSI escapes. See the default value of this option to have an idea about how to color this line. It is worth noting that lf uses as many colors advertised by your terminal's entry in terminfo or infocmp databases on your system. If an entry is not present, it falls back to an internal database. If your terminal supports 24-bit colors but either does not have a database entry or does not advertise all capabilities, you can enable support by setting the `$COLORTERM` variable to `truecolor` or ensuring `$TERM` is set to a value that ends with `-truecolor`. Default lf colors are mostly taken from GNU dircolors defaults. These defaults use 8 basic colors and bold attribute. Default dircolors entries with background colors are simplified to avoid confusion with current file selection in lf. Similarly, there are only file type matchings and extension matchings are left out for simplicity. Default values are as follows given with their matching order in lf: ln 01;36 or 31;01 tw 01;34 ow 01;34 st 01;34 di 01;34 pi 33 so 01;35 bd 33;01 cd 33;01 su 01;32 sg 01;32 ex 01;32 fi 00 Note that lf first tries matching file names and then falls back to file types. The full order of matchings from most specific to least are as follows: 1. Full Path (e.g. `~/.config/lf/lfrc`) 2. Dir Name (e.g. `.git/`) (only matches dirs with a trailing slash at the end) 3. File Type (e.g. `ln`) (except `fi`) 4. File Name (e.g. `README*`) 5. File Name (e.g. `*README`) 6. Base Name (e.g. `README.*`) 7. Extension (e.g. `*.txt`) 8. Default (i.e. `fi`) For example, given a regular text file `/path/to/README.txt`, the following entries are checked in the configuration and the first one to match is used: 1. `/path/to/README.txt` 2. (skipped since the file is not a directory) 3. (skipped since the file is of type `fi`) 4. `README.txt*` 5. `*README.txt` 6. `README.*` 7. `*.txt` 8. `fi` Given a regular directory `/path/to/example.d`, the following entries are checked in the configuration and the first one to match is used: 1. `/path/to/example.d` 2. `example.d/` 3. `di` 4. `example.d*` 5. `*example.d` 6. `example.*` 7. `*.d` 8. `fi` Note that glob-like patterns do not perform glob matching for performance reasons. For example, you can set a variable as follows: export LF_COLORS="~/Documents=01;31:~/Downloads=01;31:~/.local/share=01;31:~/.config/lf/lfrc=31:.git/=01;32:.git*=32:*.gitignore=32:*Makefile=32:README.*=33:*.txt=34:*.md=34:ln=01;36:di=01;34:ex=01;32:" Having all entries on a single line can make it hard to read. You may instead divide it into multiple lines in between double quotes by escaping newlines with backslashes as follows: export LF_COLORS="\ ~/Documents=01;31:\ ~/Downloads=01;31:\ ~/.local/share=01;31:\ ~/.config/lf/lfrc=31:\ .git/=01;32:\ .git*=32:\ *.gitignore=32:\ *Makefile=32:\ README.*=33:\ *.txt=34:\ *.md=34:\ ln=01;36:\ di=01;34:\ ex=01;32:\ " The `ln` entry supports the special value `target`, which will use the link target to select a style. Filename rules will still apply based on the link's name -- this mirrors GNU's `ls` and `dircolors` behavior. Having such a long variable definition in a shell configuration file might be undesirable. You may instead use the colors file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)) for configuration. A sample colors file can be found at https://github.com/gokcehan/lf/blob/master/etc/colors.example You may also see the wiki page for ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code # ICONS Icons are configured using `LF_ICONS` environment variable or an icons file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)). The variable uses the same syntax as `LS_COLORS/LF_COLORS`. Instead of colors, you should use single characters or symbols as values. The `ln` entry supports the special value `target`, which will use the link target to select a icon. Filename rules will still apply based on the link's name -- this mirrors GNU's `ls` and `dircolors` behavior. The icons file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)) should consist of whitespace-separated arrays with a `#` character to start comments until the end of the line. Each line should contain 1-3 columns: a file type or file name pattern, the icon, and an optional icon color. Using only one column disables all rules for that type or name. Do not forget to add `set icons true` to your `lfrc` to see the icons. Default values are listed below in the order lf matches them: ln l or l tw t ow d st t di d pi p so s bd b cd c su u sg g ex x fi - A sample icons file can be found at https://github.com/gokcehan/lf/blob/master/etc/icons.example A sample colored icons file can be found at https://github.com/gokcehan/lf/blob/master/etc/icons_colored.example # RULER The ruler can be configured using the `rulerfile` option (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)). The contents of the ruler file should be a Go template which is then rendered to create the actual output (refer to https://pkg.go.dev/text/template for more details on the syntax). The following data fields are exported: .Message string Includes internal messages, errors, and messages generated by the `echo`/`echomsg`/`echoerr` commands .Keys string Keys pressed by the user .Progress []string Progress indicators for copied, moved and deleted files .Copy []string List of files in the clipboard to be copied .Cut []string List of files in the clipboard to be moved .Select []string Selection list .Visual []string Visual selection .Index int Index of the cursor .Total int Number of visible files in the current working directory .Hidden int Number of hidden files in the current working directory .All int Number of all files in the current working directory .LinePercentage string Line percentage (analogous to `%p` for the `statusline` option in Vim) .ScrollPercentage string Scroll percentage (analogous to `%P` for the `statusline` option in Vim) .Filter []string Filter currently being applied .Mode string Current mode ("NORMAL" for Normal mode, and "VISUAL" for Visual mode) .Options map[string]string The value of options (e.g. `{{.Options.hidden}}`) .UserOptions map[string]string The value of user-defined options (e.g. `{{.UserOptions.foo}}`) .Stat.Path string Path of the current file .Stat.Name string Name of the current file .Stat.Extension string Extension of the current file .Stat.Size int64 Size of the current file .Stat.DirSize int64 Total size of the current directory if calculated via `calcdirsize` (`-1` if not calculated) .Stat.DirCount int Number of items in the current directory if the `dircounts` option is enabled (`-1` if the directory cannot be read) .Stat.Permissions string Permissions of the current file .Stat.ModTime string Last modified time of the current file (formatted based on the `timefmt` option) .Stat.AccessTime string Last access time of the current file (formatted based on the `timefmt` option) .Stat.BirthTime string Birth time of the current file (formatted based on the `timefmt` option) .Stat.ChangeTime string Last status (inode) change time of the current file (formatted based on the `timefmt` option) .Stat.LinkCount string Number of hard links for the current file .Stat.User string User of the current file .Stat.Group string Group of the current file .Stat.Target string Target if the current file is a symbolic link, otherwise a blank string .Stat.CustomInfo string Custom property if defined via `addcustominfo`, otherwise a blank string The following functions are exported: df func() string Get an indicator representing the amount of free disk space available env func(string) string Get the value of an environment variable humanize func(int64) string Express a file size in a human-readable format join func([]string, string) string Join a string array by a separator lower func(string) string Convert a string to lowercase substr func(string, int, int) string Get a substring based on starting index and length upper func(string) string Convert a string to uppercase The special identifier `{{.SPACER}}` can be used to divide the ruler into sections that are spaced evenly from each other. The default ruler file can be found at https://github.com/gokcehan/lf/blob/master/etc/ruler.default ================================================ FILE: doc.txt ================================================ NAME lf - terminal file manager SYNOPSIS lf [-command command] [-config path] [-cpuprofile path] [-doc] [-help] [-last-dir-path path] [-log path] [-memprofile path] [-print-last-dir] [-print-selection] [-remote command] [-selection-path path] [-server] [-single] [-version] [cd-or-select-path] DESCRIPTION lf is a terminal file manager. The source code can be found in the repository at https://github.com/gokcehan/lf This documentation can either be read from the terminal using lf -doc or online at https://github.com/gokcehan/lf/blob/master/doc.md You can also use the help command (default ) inside lf to view the documentation in a pager. A man page with the same content is also available in the repository at https://github.com/gokcehan/lf/blob/master/lf.1 OPTIONS POSITIONAL ARGUMENTS cd-or-select-path Set the starting location. If path is a directory, start in there. If it's a file, start in the file's parent directory and select the file. When no path is supplied, lf uses the current directory. Only accepts one argument. META OPTIONS -doc Show lf's documentation (same content as this file) and exit. -help Show command-line usage and exit. -version Show version information and exit. STARTUP & CONFIGURATION -command command Execute command during client initialization (i.e. after reading configuration, before on-init). To execute more than one command, you can either use the -command flag multiple times or pass multiple commands at once by chaining them with ";". -config path Use the config file at path instead of the normal search locations. This only affects which lfrc is read at startup. SHELL INTEGRATION -print-last-dir Print the last directory to stdout when lf exits. This can be used to let lf change your shells working directory. See CHANGING DIRECTORY for more details. -last-dir-path path Same as -print-last-dir, but write the directory to path instead of stdout. -print-selection Print selected files to stdout when opening a file in lf. This can be used to use lf as an "open file" dialog. First, select the files you want to pass to another program. Then, confirm the selection by opening a file. This causes lf to quit and print out the selection. Quitting lf prematurely discards the selection. -selection-path path Same as -print-selection, but write the newline-separated list to path instead of stdout. SERVER -remote command Send command to the running server (i.e. send, query, list, quit, or quit!). See REMOTE COMMANDS for more details. -server Start the (headless) server process explicitly. Runs in the foreground and writes server logs to stderr (or the file set with -log). Clients auto-start a server if none is running unless -single is used. -single Start a stand-alone client without a server. Disables remote control. DIAGNOSTICS -log path Append runtime log messages to path. -cpuprofile path Write a CPU profile to path. The profile can be used by go tool pprof. -memprofile path Write a memory profile to path. The profile can be used by go tool pprof. EXAMPLES Use lf to select files (while hiding certain file types): lf -command 'set nohidden' -command 'set hiddenfiles "*mp4:*pdf:*txt"' -print-selection Another sophisticated "open file" dialog focusing on design: lf -command 'set nopreview; set ratios 1; set drawbox; set promptfmt "Select files [%w] %S q: cancel, l: confirm"' -print-selection Open Downloads and set sortby and info to creation date: lf -command 'set sortby btime; set info btime' ~/Downloads Temporarily prevent lf from modifying the command history: lf -command 'set nohistory' Use default settings and log current session: lf -config /dev/null -log /tmp/lf.log Force-quit the server: lf -remote 'quit!' Inherit lf's working directory in your shell: cd "$(lf -print-last-dir)" QUICK REFERENCE The following commands are provided by lf: quit (default 'q') up (default 'k' and '') half-up (default '') page-up (default '' and '') scroll-up (default '') down (default 'j' and '') half-down (default '') page-down (default '' and '') scroll-down (default '') updir (default 'h' and '') open (default 'l' and '') jump-next (default ']') jump-prev (default '[') top (default 'gg' and '') bottom (default 'G' and '') high (default 'H') middle (default 'M') low (default 'L') toggle invert (default 'v') unselect (default 'u') glob-select glob-unselect copy (default 'y') cut (default 'd') paste (default 'p') clear (default 'c') sync draw redraw (default '') load reload (default '') delete (modal) rename (modal) (default 'r') read (modal) (default ':') shell (modal) (default '$') shell-pipe (modal) (default '%') shell-wait (modal) (default '!') shell-async (modal) (default '&') find (modal) (default 'f') find-back (modal) (default 'F') find-next (default ';') find-prev (default ',') search (modal) (default '/') search-back (modal) (default '?') search-next (default 'n') search-prev (default 'N') filter (modal) setfilter mark-save (modal) (default 'm') mark-load (modal) (default "'") mark-remove (modal) (default '"') tag tag-toggle (default 't') echo echomsg echoerr cd select source push addcustominfo calcdirsize clearmaps tty-write visual (default 'V') The following Visual mode commands are provided by lf: visual-accept (default 'V') visual-unselect visual-discard (default '') visual-change (default 'o') The following Command-line mode commands are provided by lf: cmd-insert cmd-escape (default '') cmd-complete (default '') cmd-menu-complete cmd-menu-complete-back cmd-menu-accept cmd-menu-discard cmd-enter (default '' and '') cmd-interrupt (default '') cmd-history-next (default '' and '') cmd-history-prev (default '' and '') cmd-left (default '' and '') cmd-right (default '' and '') cmd-home (default '' and '') cmd-end (default '' and '') cmd-delete (default '' and '') cmd-delete-back (default '') cmd-delete-home (default '') cmd-delete-end (default '') cmd-delete-unix-word (default '') cmd-yank (default '') cmd-transpose (default '') cmd-transpose-word (default '') cmd-word (default '') cmd-word-back (default '') cmd-delete-word (default '') cmd-delete-word-back (default '') cmd-capitalize-word (default '') cmd-uppercase-word (default '') cmd-lowercase-word (default '') The following options can be used to customize the behavior of lf: anchorfind bool (default true) autoquit bool (default true) borderfmt string (default "\033[0m") borderstyle string (default 'box') cleaner string (default '') copyfmt string (default "\033[7;33m") cursoractivefmt string (default "\033[7m") cursorparentfmt string (default "\033[7m") cursorpreviewfmt string (default "\033[4m") cutfmt string (default "\033[7;31m") dircounts bool (default false) dirfirst bool (default true) dironly bool (default false) dirpreviews bool (default false) drawbox bool (default false) dupfilefmt string (default '%f.~%n~') errorfmt string (default "\033[7;31;47m") filesep string (default "\n") filtermethod string (default 'text') findlen int (default 1) hidden bool (default false) hiddenfiles []string (default '.*' for Unix and '' for Windows) history bool (default true) icons bool (default false) ifs string (default '') ignorecase bool (default true) ignoredia bool (default true) incfilter bool (default false) incsearch bool (default false) info []string (default '') infotimefmtnew string (default 'Jan _2 15:04') infotimefmtold string (default 'Jan _2 2006') menufmt string (default "\033[0m") menuheaderfmt string (default "\033[1m") menuselectfmt string (default "\033[7m") mergeindicators bool (default false) mouse bool (default false) number bool (default false) numbercursorfmt string (default '') numberfmt string (default "\033[33m") period int (default 0) preload bool (default false) preserve []string (default "mode") preview bool (default true) previewer string (default '') promptfmt string (default "\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m") ratios []int (default '1:2:3') relativenumber bool (default false) reverse bool (default false) rulerfile string (default "") rulerfmt string (default "") scrolloff int (default 0) searchmethod string (default 'text') selectfmt string (default "\033[7;35m") selmode string (default 'all') shell string (default 'sh' for Unix and 'cmd' for Windows) shellflag string (default '-c' for Unix and '/c' for Windows) shellopts []string (default '') showbinds bool (default true) sizeunits string (default 'binary') smartcase bool (default true) smartdia bool (default false) sortby string (default 'natural') statfmt string (default "\033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l") tabstop int (default 8) tagfmt string (default "\033[31m") tempmarks string (default '') terminalcursor string (default 'default') timefmt string (default 'Mon Jan _2 15:04:05 2006') truncatechar string (default '~') truncatepct int (default 100) visualfmt string (default "\033[7;36m") waitmsg string (default 'Press any key to continue') watch bool (default false) wrapscan bool (default true) wrapscroll bool (default false) user_{option} string (default none) The following environment variables are exported for shell commands: f fs fv fx id PWD OLDPWD LF_LEVEL OPENER VISUAL EDITOR PAGER SHELL lf lf_{option} lf_user_{option} lf_flag_{flag} lf_width lf_height lf_count lf_mode The following special shell commands are used to customize the behavior of lf when defined: open paste rename delete pre-cd on-cd on-load on-focus-gained on-focus-lost on-init on-select on-redraw on-quit The following commands/keybindings are provided by default: Unix cmd open &$OPENER "$f" map e $$EDITOR "$f" map i $$PAGER "$f" map w $$SHELL cmd help $$lf -doc | $PAGER map help cmd maps $lf -remote "query $id maps" | $PAGER cmd nmaps $lf -remote "query $id nmaps" | $PAGER cmd vmaps $lf -remote "query $id vmaps" | $PAGER cmd cmaps $lf -remote "query $id cmaps" | $PAGER cmd cmds $lf -remote "query $id cmds" | $PAGER Windows cmd open &%OPENER% %f% map e $%EDITOR% %f% map i !%PAGER% %f% map w $%SHELL% cmd help !%lf% -doc | %PAGER% map help cmd maps !%lf% -remote "query %id% maps" | %PAGER% cmd nmaps !%lf% -remote "query %id% nmaps" | %PAGER% cmd vmaps !%lf% -remote "query %id% vmaps" | %PAGER% cmd cmaps !%lf% -remote "query %id% cmaps" | %PAGER% cmd cmds !%lf% -remote "query %id% cmds" | %PAGER% The defaults for Windows are using cmd syntax. A PowerShell compatible configuration file can be found at https://github.com/gokcehan/lf/blob/master/etc/lfrc.ps1.example The following additional keybindings are provided by default: map zh set hidden! map zr set reverse! map zn set info map zs set info size map zt set info time map za set info size:time map sn :set sortby natural; set info map ss :set sortby size; set info size map st :set sortby time; set info time map sa :set sortby atime; set info atime map sb :set sortby btime; set info btime map sc :set sortby ctime; set info ctime map se :set sortby ext; set info map gh cd ~ nmap :toggle; down If the mouse option is enabled, mouse buttons have the following default effects: Left mouse button Click on a file or directory to select it. Right mouse button Enter a directory or open a file. Also works on the preview pane. Scroll wheel Move up or down. If Ctrl is pressed, scroll up or down. CONFIGURATION Configuration files should be located at: OS system-wide user-specific Unix /etc/lf/lfrc ~/.config/lf/lfrc Windows C:\ProgramData\lf\lfrc C:\Users\\AppData\Roaming\lf\lfrc The colors file should be located at: OS system-wide user-specific Unix /etc/lf/colors ~/.config/lf/colors Windows C:\ProgramData\lf\colors C:\Users\\AppData\Roaming\lf\colors The icons file should be located at: OS system-wide user-specific Unix /etc/lf/icons ~/.config/lf/icons Windows C:\ProgramData\lf\icons C:\Users\\AppData\Roaming\lf\icons The selection file should be located at: Unix ~/.local/share/lf/files Windows C:\Users\\AppData\Local\lf\files The marks file should be located at: Unix ~/.local/share/lf/marks Windows C:\Users\\AppData\Local\lf\marks The tags file should be located at: Unix ~/.local/share/lf/tags Windows C:\Users\\AppData\Local\lf\tags The history file should be located at: Unix ~/.local/share/lf/history Windows C:\Users\\AppData\Local\lf\history You can configure these locations with the following variables given with their order of precedences and their default values: Unix $LF_CONFIG_HOME $XDG_CONFIG_HOME ~/.config $LF_DATA_HOME $XDG_DATA_HOME ~/.local/share Windows %LF_CONFIG_HOME% %XDG_CONFIG_HOME% %APPDATA% %LF_DATA_HOME% %XDG_DATA_HOME% %LOCALAPPDATA% A sample configuration file can be found at https://github.com/gokcehan/lf/blob/master/etc/lfrc.example COMMANDS This section shows information about built-in commands. Modal commands do not take any arguments, but instead change the operation mode to read their input conveniently, and so they are meant to be assigned to keybindings. quit (default q) Quit lf and return to the shell. up (default k and ), half-up (default ), page-up (default and ), scroll-up (default ), down (default j and ), half-down (default ), page-down (default and ), scroll-down (default ) Move/scroll the current file selection upwards/downwards by one/half a page/full page. updir (default h and ) Change the current working directory to the parent directory. open (default l and ) If the current file is a directory, then change the current directory to it, otherwise, execute the open command. A default open command is provided to call the default system opener asynchronously with the current file as the argument. A custom open command can be defined to override this default. jump-next (default ]), jump-prev (default [) Change the current working directory to the next/previous jumplist item. top (default gg and ), bottom (default G and ) Move the current file selection to the top/bottom of the directory. A count can be specified to move to a specific line, for example, use 3G to move to the third line. high (default H), middle (default M), low (default L) Move the current file selection to the high/middle/low of the screen. toggle Toggle the selection of the current file or files given as arguments. invert (default v) Reverse the selection of all files in the current directory (i.e. toggle all files). Selections in other directories are not affected by this command. You can define a new command to select all files in the directory by combining invert with unselect (i.e. cmd select-all :unselect; invert), though this will also remove selections in other directories. unselect (default u) Remove the selection of all files in all directories. glob-select, glob-unselect Select/unselect files that match the given glob. copy (default y) Save the paths of selected files to the clipboard as files to be copied. If there are no selected files, the path of the current file is used instead. cut (default d) Save the paths of selected files to the clipboard as files to be moved. If there are no selected files, the path of the current file is used instead. paste (default p) Copy/Move files in the clipboard to the current working directory. A custom paste command can be defined to override this default. clear (default c) Clear file paths in the clipboard. sync Synchronize copied/cut files with the server. This command is automatically called when required. draw Draw the screen. This command is automatically called when required. redraw (default ) Synchronize the terminal and redraw the screen. load Load modified files and directories. This command is automatically called when required. reload (default ) Flush the cache and reload all files and directories. delete (modal) Remove the current file or selected file(s). A custom delete command can be defined to override this default. rename (modal) (default r) Rename the current file using the built-in method. A custom rename command can be defined to override this default. read (modal) (default :) Read a command to evaluate. shell (modal) (default $) Read a shell command to execute. shell-pipe (modal) (default %) Read a shell command to execute piping its standard I/O to the bottom statline. shell-wait (modal) (default !) Read a shell command to execute and wait for a key press at the end. shell-async (modal) (default &) Read a shell command to execute asynchronously without standard I/O. find (modal) (default f), find-back (modal) (default F), find-next (default ;), find-prev (default ,) Read key(s) to find the appropriate filename match in the forward/backward direction and jump to the next/previous match. search (default /), search-back (default ?), search-next (default n), search-prev (default N) Read a pattern to search for a filename match in the forward/backward direction and jump to the next/previous match. filter (modal), setfilter Command filter reads a pattern to filter out and only view files matching the pattern. Command setfilter does the same but uses an argument to set the filter immediately. You can supply an argument to filter to use as the starting prompt. mark-save (modal) (default m) Save the current directory as a bookmark assigned to the given key. mark-load (modal) (default ') Change the current directory to the bookmark assigned to the given key. A special bookmark ' holds the previous directory after a mark-load, cd, or select command. mark-remove (modal) (default ") Remove a bookmark assigned to the given key. tag Tag a file with * or a single-width character given in the argument. You can define a new tag-clearing command by combining tag with tag-toggle (i.e. cmd tag-clear :tag; tag-toggle). tag-toggle (default t) Tag a file with * or a single-width character given in the argument if the file is untagged, otherwise remove the tag. echo Print the given arguments to the message line at the bottom. echomsg Print the given arguments to the message line at the bottom and also to the log file. echoerr Print given arguments to the message line at the bottom as errorfmt and also to the log file. cd Change the working directory to the given argument. select Change the current file selection to the given argument. source Read the configuration file given in the argument. push Simulate key pushes given in the argument. addcustominfo Update the custom info and .Stat.CustomInfo field of the given file with the given string. The info string may contain ANSI escape codes to further customize its appearance. If no info is provided, clear the file's info instead. calcdirsize Calculate the total size for each of the selected directories. Option info should include size and option dircounts should be disabled to show this size. If the total size of a directory is not calculated, it will be shown as -. clearmaps Remove all keybindings associated with the map, nmap and vmap command. This command can be used in the config file to remove the default keybindings. For safety purposes, : is left mapped to the read command, and cmap keybindings are retained so that it is still possible to exit lf using :quit. tty-write Write the given string to the tty. This is useful for sending escape sequences to the terminal to control its behavior (e.g. OSC 0 to set the window title). Using tty-write is preferred over directly writing to /dev/tty because the latter is not synchronized and can interfere with drawing the UI. visual (default V) Switch to Visual mode. If already in Visual mode, discard the visual selection and stay in Visual mode. VISUAL MODE COMMANDS visual-accept (default V) Add the visual selection to the selection list, quit Visual mode and return to Normal mode. visual-unselect Remove the visual selection from the selection list, quit Visual mode and return to Normal mode. visual-discard (default ) Discard the visual selection, quit Visual mode and return to Normal mode. visual-change (default o) Go to the other end of the current Visual mode selection. COMMAND-LINE MODE COMMANDS The prompt character specifies which of the several Command-line modes you are in. For example, the read command takes you to the : mode. When the cursor is at the first character in : mode, pressing one of the keys !, $, %, or & takes you to the corresponding mode. You can go back with cmd-delete-back ( by default). The command line commands should be mostly compatible with readline keybindings. A character refers to a Unicode code point, a word consists of letters and digits, and a Unix word consists of any non-blank characters. cmd-insert Insert the character given in the argument. This command is automatically called when required. cmd-escape (default ) Quit Command-line mode and return to Normal mode. cmd-complete (default ) Autocomplete the current word. cmd-menu-complete, cmd-menu-complete-back Autocomplete the current word with the menu selection. You need to assign keys to these commands (e.g. cmap cmd-menu-complete; cmap cmd-menu-complete-back). You can use the assigned keys to display the menu and then cycle through completion options. cmd-menu-accept Accept the currently selected match in menu completion and close the menu. cmd-menu-discard Discard the currently selected match in menu completion and close the menu. cmd-enter (default and ) Execute the current line. cmd-interrupt (default ) Interrupt the current shell-pipe command and return to the Normal mode. cmd-history-next (default and ), cmd-history-prev (default and ) Go to the next/previous entry in the command history. If part of the command is already typed, then only matching entries will be considered, and consecutive duplicate entries are skipped. cmd-left (default and ), cmd-right (default and ) Move the cursor to the left/right. cmd-home (default and ), cmd-end (default and ) Move the cursor to the beginning/end of the line. cmd-delete (default and ) Delete the next character. cmd-delete-back (default ) Delete the previous character. When at the beginning of a prompt, returns either to Normal mode or to : mode. cmd-delete-home (default ), cmd-delete-end (default ) Delete everything up to the beginning/end of the line. cmd-delete-unix-word (default ) Delete the previous Unix word. cmd-yank (default ) Paste the buffer content containing the last deleted item. cmd-transpose (default ) Swap the characters before and after the cursor, then move the cursor forward. If there is no character after the cursor, swap the previous two characters instead. cmd-transpose-word (default ) Swap the words before and after the cursor, then move the cursor forward. If there is no word after the cursor, swap the previous two words instead. cmd-word (default ), cmd-word-back (default ) Move the cursor by one word in the forward/backward direction. cmd-delete-word (default ) Delete the next word in the forward direction. cmd-delete-word-back (default ) Delete the previous word in the backward direction. cmd-capitalize-word (default ), cmd-uppercase-word (default ), cmd-lowercase-word (default ) Capitalize/uppercase/lowercase the current word and jump to the next word. SETTINGS This section shows information about options to customize the behavior. Character : is used as the separator for list options []int and []string. anchorfind (bool) (default true) When this option is enabled, the find command starts matching patterns from the beginning of filenames, otherwise, it can match at an arbitrary position. autoquit (bool) (default true) Automatically quit the server when there are no clients left connected. borderfmt (string) (default \033[0m) Format string of border characters. borderstyle (string) (default box) Border style used by drawbox. The following styles are supported: box outline around all panes and separators between them roundbox like `box`, but with rounded outer corners outline outline around all panes roundoutline like `outline`, but with rounded outer corners separators separators between panes cleaner (string) (default ``) (not called if empty) Set the path of a cleaner file. The file should be executable. This file is called if previewing is enabled, the previewer is set, and the previously selected file has its preview cache disabled. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position of preview pane and (6) next filename to be previewed respectively. Preview cleaning is disabled when the value of this option is left empty. copyfmt (string) (default \033[7;33m) Format string of the indicator for files to be copied. cursoractivefmt (string) (default \033[7m), cursorparentfmt (string) (default \033[7m), cursorpreviewfmt (string) (default \033[4m) Format strings for highlighting the cursor. cursoractivefmt applies in the current directory pane, cursorparentfmt applies in panes that show parents of the current directory, and cursorpreviewfmt applies in panes that preview directories. The default is to make the active cursor and the parent directory cursor inverted. The preview cursor is underlined. Some other possibilities to consider for the preview or parent cursors: an empty string for no cursor, \033[7;2m for dimmed inverted text (visibility varies by terminal), \033[7;90m for inverted text with grey (aka "brightblack") background. If the format string contains the characters %s, it is interpreted as a format string for fmt.Sprintf. Such a string should end with the terminal reset sequence. For example, \033[4m%s\033[0m has the same effect as \033[4m. cutfmt (string) (default \033[7;31m) Format string of the indicator for files to be cut. dircounts (bool) (default false) When this option is enabled, directory sizes show the number of items inside instead of the total size of the directory, which needs to be calculated for each directory using calcdirsize. This information needs to be calculated by reading the directory and counting the items inside. Therefore, this option is disabled by default for performance reasons. This option only has an effect when info has a size field and the pane is wide enough to show the information. 999 items are counted per directory at most, and bigger directories are shown as 999+. dirfirst (bool) (default true) Show directories first above regular files. With dircounts enabled, sorting by size always separates directories and files, regardless of dirfirst. dironly (bool) (default false) Show only directories. dirpreviews (bool) (default false) If enabled, directories will also be passed to the previewer script. This allows custom previews for directories. drawbox (bool) (default false) Draw borders around panes using box drawing characters. dupfilefmt (string) (default %f.~%n~) Format string of filename when creating duplicate files. With the default format, copying a file abc.txt to the same directory will result in a duplicate file called abc.txt.~1~. Special expansions are provided, %f as the file name, %b for the base name (file name without extension), %e as the extension (including the dot) and %n as the number of duplicates. errorfmt (string) (default \033[7;31;47m) Format string of error messages shown in the bottom message line. If the format string contains the characters %s, it is interpreted as a format string for fmt.Sprintf. Such a string should end with the terminal reset sequence. For example, \033[4m%s\033[0m has the same effect as \033[4m. filesep (string) (default \n) File separator used in environment variables fs, fv and fx. filtermethod (string) (default text) How filter command patterns are treated. Currently supported methods are text (i.e. string literals), glob (i.e. shell globs) and regex (i.e. regular expressions). See SEARCHING FILES for more details. findlen (int) (default 1) Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left. hidden (bool) (default false) Show hidden files. On Unix systems, hidden files are determined by the value of hiddenfiles. On Windows, files with hidden attributes are also considered hidden files. hiddenfiles ([]string) (default .* for Unix and `` for Windows) List of hidden file glob patterns. Patterns can be given as relative or absolute paths. Globbing supports the usual special characters, * to match any sequence, ? to match any character, and [...] or [^...] to match character sets or ranges. In addition, if a pattern starts with !, then its matches are excluded from hidden files. To add multiple patterns, use : as a separator. Example: .*:lost+found:*.bak history (bool) (default true) Save command history. icons (bool) (default false) Show icons before each item in the list. ifs (string) (default ``) Sets IFS variable in shell commands. It works by adding the assignment to the beginning of the command string as IFS=...; .... The reason is that IFS variable is not inherited by the shell for security reasons. This method assumes a POSIX shell syntax so it can fail for non-POSIX shells. This option has no effect when the value is left empty. This option does not have any effect on Windows. ignorecase (bool) (default true) Ignore case in sorting and search patterns. ignoredia (bool) (default true) Ignore diacritics in sorting and search patterns. incfilter (bool) (default false) Apply filter pattern after each keystroke during filtering. incsearch (bool) (default false) Jump to the first match after each keystroke during searching. info ([]string) (default ``) A list of information that is shown for directory items at the right side of the pane. The following information types are supported: perm file permission user user name group group name size file size time time of last data modification atime time of last access btime time of file birth ctime time of last status (inode) change custom property defined via `addcustominfo` (empty by default) Information is only shown when the pane width is more than twice the width of information. infotimefmtnew (string) (default Jan _2 15:04) Format string of the file time shown in the info column when it matches this year. infotimefmtold (string) (default Jan _2 2006) Format string of the file time shown in the info column when it doesn't match this year. menufmt (string) (default \033[0m) Format string of the menu. menuheaderfmt (string) (default \033[1m) Format string of the header row in the menu. menuselectfmt (string) (default \033[7m) Format string of the currently selected item in the menu. mergeindicators (bool) (default false) When mergeindicators is enabled, tag and selection indicators are drawn in a single column to reduce the gap before filenames. If a file is both tagged and selected, the tag uses the selection format (e.g. copyfmt) instead of tagfmt. mouse (bool) (default false) Send mouse events as input. number (bool) (default false) Show the position number for directory items on the left side of the pane. When the relativenumber option is enabled, only the current line shows the absolute position and relative positions are shown for the rest. numberfmt (string) (default \033[33m), numbercursorfmt (string) (default ``) Format strings for highlighting line numbers. numberfmt applies to all lines. numbercursorfmt applies to the cursor line and falls back to numberfmt when left empty. period (int) (default 0) Set the interval in seconds for periodic checks of directory updates. This works by periodically calling the load command. Note that directories are already updated automatically in many cases. This option can be useful when there is an external process changing the displayed directory and you are not doing anything in lf. Periodic checks are disabled when the value of this option is set to zero. preload (bool) (default false) Allow previews to be generated in advance using the previewer script as the user navigates through the filesystem. preserve ([]string) (default mode) List of attributes that are preserved when copying files. Currently supported attributes are mode (i.e. access mode) and timestamps (i.e. modification time and access time). Note that preserving other attributes like ownership of change/birth timestamp is desirable, but not portably supported in Go. preview (bool) (default true) Show previews of files and directories at the rightmost pane. If the file has more lines than the preview pane, the rest of the lines are not read. Files containing the null character (U+0000) in the read portion are considered binary files and displayed as binary. previewer (string) (default ``) (not filtered if empty) Set the path of a previewer file to filter the content of regular files for previewing. The file should be executable. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode ("preview" or "preload"). SIGPIPE signal is sent when enough lines are read. If the previewer returns a non-zero exit code, then the preview cache for the given file is disabled. This means that if the file is selected in the future, the previewer is called once again. Preview filtering is disabled and files are displayed as they are when the value of this option is left empty. If the preload option is enabled, then this will be called with preload as the mode when preloading file previews. Refer to the PREVIEWING FILES section for more information about how to configure custom previews. promptfmt (string) (default \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m) Format string of the prompt shown in the top line. The following special expansions are supported: %f file name %h host name %u user name %w working directory %d working directory (with trailing path separator) %F current filter %S spacer to right-align the following parts (can be used once) The home folder is shown as ~ in the working directory expansion. Directory names are automatically shortened to a single character starting from the leftmost parent when the prompt does not fit the screen. ratios ([]int) (default 1:2:3) List of ratios of pane widths. Number of items in the list determines the number of panes in the UI. When the preview option is enabled, the rightmost number is used for the width of the preview pane. relativenumber (bool) (default false) Show the position number relative to the current line. When number is enabled, the current line shows the absolute position, otherwise nothing is shown. reverse (bool) (default false) Reverse the direction of sort. rulerfile (string) (default ``) Set the path of the ruler file. If not set, then a default template will be used for the ruler. Refer to the RULER section for more information about how the ruler file works. rulerfmt (string) (default ``) Format string of the ruler shown in the bottom right corner. When set, it will be used along with statfmt to draw the ruler, and rulerfile will be ignored. However, using rulerfile is preferred and this option is provided for backwards compatibility. The following special expansions are supported: %a pressed keys %p progress of file operations %m number of files to be cut (moved) %c number of files to be copied %s number of selected files %v number of visually selected files %t number of shown files in the current directory %h number of hidden files in the current directory %f current filter %i cursor position %P scroll percentage %d amount of free disk space Additional expansions are provided for environment variables exported by lf, in the form %{lf_} (e.g. %{lf_selmode}). This is useful for displaying the current settings. Expansions are also provided for user-defined options, in the form %{lf_user_} (e.g. %{lf_user_foo}). The | character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown. scrolloff (int) (default 0) Minimum number of offset lines shown at all times at the top and bottom of the screen when scrolling. The current line is kept in the middle when this option is set to a large value that is bigger than half the number of lines. A smaller offset can be used when the current file is close to the beginning or end of the list to show the maximum number of items. searchmethod (string) (default text) How search command patterns are treated. Currently supported methods are text (i.e. string literals), glob (i.e. shell globs) and regex (i.e. regular expressions). See SEARCHING FILES for more details. selectfmt (string) (default \033[7;35m) Format string of the indicator for files that are selected. selmode (string) (default all) Selection mode for commands. When set to all it will use the selected files from all directories. When set to dir it will only use the selected files in the current directory. shell (string) (default sh for Unix and cmd for Windows) Shell executable to use for shell commands. Shell commands are executed as shell shellopts shellflag command -- arguments. shellflag (string) (default -c for Unix and /c for Windows) Command line flag used to pass shell commands. shellopts ([]string) (default ``) List of shell options to pass to the shell executable. showbinds (bool) (default true) Show bindings associated with pressed keys. sizeunits (string) (default binary) Determines whether file sizes are displayed using binary units (1K is 1024 bytes) or decimal units (1K is 1000 bytes). smartcase (bool) (default true) Override ignorecase option when the pattern contains an uppercase character. This option has no effect when ignorecase is disabled. smartdia (bool) (default false) Override ignoredia option when the pattern contains a character with diacritic. This option has no effect when ignoredia is disabled. sortby (string) (default natural) Sort type for directories. The following sort types are supported: natural file name (track_2.flac comes before track_10.flac) name file name (track_10.flac comes before track_2.flac) ext file extension size file size time time of last data modification atime time of last access btime time of file birth ctime time of last status (inode) change custom property defined via `addcustominfo` (empty by default) statfmt (string) (default \033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l) Format string of the file info shown in the bottom left corner. This option has no effect unless rulerfmt is also set. Using rulerfile is preferred and this option is provided for backwards compatibility. The following special expansions are supported: %p file permission %c link count %u user name %g group name %s file size %S file size (left-padded with spaces to a fixed width of 5 characters) %t time of last data modification %l link target %m current mode %M current mode (displaying `NORMAL` instead of a blank string in Normal mode) The | character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown. tabstop (int) (default 8) Number of space characters to show for horizontal tabulation (U+0009) character. tagfmt (string) (default \033[31m) Format string of the tags. If the format string contains the characters %s, it is interpreted as a format string for fmt.Sprintf. Such a string should end with the terminal reset sequence. For example, \033[4m%s\033[0m has the same effect as \033[4m. tempmarks (string) (default ``) Marks to be considered temporary (e.g. abc refers to marks a, b, and c). These marks are not synced to other clients and they are not saved in the bookmarks file. Note that the special bookmark ' is always treated as temporary and it does not need to be specified. terminalcursor (string) (default default) Set the appearance of the terminal cursor for prompts shown in the bottom line. Currently supported values are default, block, underline, bar, blinkblock, blinkunderline and blinkbar. timefmt (string) (default Mon Jan _2 15:04:05 2006) Format string of the file modification time shown in the bottom line. truncatechar (string) (default ~) The truncate character that is shown at the end when the filename does not fit into the pane. truncatepct (int) (default 100) When a filename is too long to be shown completely, the available space will be partitioned into two parts. truncatepct is a percentage value between 0 and 100 that determines the size of the first part, which will be shown at the beginning of the filename. The second part uses the rest of the available space, and will be shown at the end of the filename. Both parts are separated by the truncation character (truncatechar). Truncation is not applied to the file extension. For example, with the filename very_long_filename.txt: - set truncatepct 100 -> very_long_filena~.txt (default) - set truncatepct 50 -> very_lon~filename.txt - set truncatepct 0 -> ~ry_long_filename.txt visualfmt (string) (default \033[7;36m) Format string of the indicator for files that are visually selected. waitmsg (string) (default Press any key to continue) String shown after commands of shell-wait type. watch (bool) (default false) Watch the filesystem for changes using fsnotify to automatically refresh file information. FUSE is currently not supported due to limitations in fsnotify. wrapscan (bool) (default true) Searching can wrap around the file list. wrapscroll (bool) (default false) Scrolling can wrap around the file list. user_{option} (string) (default none) Any option that is prefixed with user_ is a user-defined option and can be set to any string. Inside a user-defined command, the value will be provided in the lf_user_{option} environment variable. These options are not used by lf and are not persisted. ENVIRONMENT VARIABLES The following variables are exported for shell commands: These are referred to with a $ prefix on POSIX shells (e.g. $f), between % characters on Windows cmd (e.g. %f%), and with a $env: prefix on Windows PowerShell (e.g. $env:f). f Current file selection as a full path. fs Selected file(s) separated with the value of filesep option as full path(s). fv Visually selected file(s) separated with the value of filesep option as full path(s). fx Selected file(s) (i.e. fs, never fv) if there are any selected files, otherwise current file selection (i.e. f). id Id of the running client. PWD Present working directory. OLDPWD Initial working directory. LF_LEVEL The value of this variable is set to the current nesting level when you run lf from a shell spawned inside lf. You can add the value of this variable to your shell prompt to make it clear that your shell runs inside lf. For example, with POSIX shells, you can use [ -n "$LF_LEVEL" ] && PS1="$PS1""(lf level: $LF_LEVEL) " in your shell configuration file (e.g. ~/.bashrc). OPENER If this variable is set in the environment, use the same value. Otherwise, this is set to start in Windows, open in macOS, xdg-open in others. EDITOR If VISUAL is set in the environment, use its value. Otherwise, use the value of the environment variable EDITOR. If neither variable is set, this is set to vi on Unix, notepad in Windows. PAGER If this variable is set in the environment, use the same value. Otherwise, this is set to less on Unix, more in Windows. SHELL If this variable is set in the environment, use the same value. Otherwise, this is set to sh on Unix, cmd in Windows. lf Absolute path to the currently running lf binary, if it can be found. Otherwise, this is set to the string lf. lf_{option} Value of the {option}. lf_user_{option} Value of the user_{option}. lf_flag_{flag} Value of the command line {flag}. lf_width, lf_height Width/Height of the terminal. lf_count Value of the count associated with the current command. lf_mode Current mode that lf is operating in. This is useful for customizing keybindings depending on what the current mode is. Possible values are compmenu, delete, rename, filter, find, mark, search, command, shell, pipe (when running a shell-pipe command), normal, visual and unknown. SPECIAL COMMANDS This section shows information about special shell commands. open This shell command can be defined to override the default open command when the current file is not a directory. paste This shell command can be defined to override the default paste command. rename This shell command can be defined to override the default rename command. delete This shell command can be defined to override the default delete command. pre-cd This shell command can be defined to be executed before changing a directory. on-cd This shell command can be defined to be executed after changing a directory. on-load This shell command can be defined to be executed after loading a directory. It provides the files inside the directory as arguments. on-focus-gained This shell command can be defined to be executed when the terminal gains focus. on-focus-lost This shell command can be defined to be executed when the terminal loses focus. on-init This shell command can be defined to be executed after initializing and connecting to the server. on-select This shell command can be defined to be executed after the selection changes. on-redraw This shell command can be defined to be executed after the screen is redrawn or if the terminal is resized. on-quit This shell command can be defined to be executed before quitting. PREFIXES The following command prefixes are used by lf: : read (default) built-in/custom command $ shell shell command % shell-pipe shell command running with the UI ! shell-wait shell command waiting for a key press & shell-async shell command running asynchronously The same evaluator is used for the command line and the configuration file for reading shell commands. The difference is that prefixes are not necessary in the command line. Instead, different modes are provided to read corresponding commands. These modes are mapped to the prefix keys above by default. Visual mode mappings are defined the same way Normal mode mappings are defined. SYNTAX Characters from # to newline are comments and ignored: # comments start with `#` The following commands (set, setlocal, map, nmap, vmap, cmap, and cmd) are used for configuration. Command set is used to set an option which can be a boolean, integer, or string: set hidden # boolean enable set hidden true # boolean enable set nohidden # boolean disable set hidden false # boolean disable set hidden! # boolean toggle set scrolloff 10 # integer value set sortby time # string value without quotes set sortby 'time' # string value with single quotes (whitespace) set sortby "time" # string value with double quotes (backslash escapes) Command setlocal is used to set a local option for a directory which can be a boolean or string. Currently supported local options are dircounts, dirfirst, dironly, hidden, info, reverse and sortby. setlocal /foo/bar hidden # boolean enable setlocal /foo/bar hidden true # boolean enable setlocal /foo/bar nohidden # boolean disable setlocal /foo/bar hidden false # boolean disable setlocal /foo/bar hidden! # boolean toggle setlocal /foo/bar sortby time # string value without quotes setlocal /foo/bar sortby 'time' # string value with single quotes (whitespace) setlocal /foo/bar sortby "time" # string value with double quotes (backslash escapes) Command map is used to bind a key in Normal and Visual mode to a command which can be a built-in command, custom command, or shell command: map gh cd ~ # built-in command map D trash # custom command map i $less $f # shell command map U !du -csh * # waiting shell command Command nmap does the same but for Normal mode only. Command vmap does the same but for Visual mode only. Overview of which map command works in which mode: map Normal, Visual nmap Normal vmap Visual cmap Command-line Command cmap is used to bind a key on the command line to a command line command or any other command: cmap cmd-escape cmap set incsearch! You can delete an existing binding by leaving the expression empty: map gh # deletes 'gh' mapping in Normal and Visual mode nmap v # deletes 'v' mapping in Normal mode vmap o # deletes 'o' mapping in Visual mode cmap # deletes '' mapping Command cmd is used to define a custom command: cmd usage $du -h -d1 | less You can delete an existing command by leaving the expression empty: cmd trash # deletes 'trash' command If there is no prefix then : is assumed: map zt set info time An explicit : can be provided to group statements until a newline which is especially useful for map and cmd commands: map st :set sortby time; set info time If you need multiline you can wrap statements in {{ and }} after the proper prefix. map st :{{ set sortby time set info time }} KEY MAPPINGS Regular keys are assigned to a command with the usual syntax: map a down Keys combined with the Shift key simply use the uppercase letter: map A down Special keys are written in between < and > characters and always use lowercase letters: map down Angle brackets can be assigned with their special names: map down map down Function keys are prefixed with an f character: map down Keys combined with the Ctrl key are prefixed with a c character: map down Keys combined with the Alt key are assigned in two different ways depending on the behavior of your terminal. Older terminals (e.g. xterm) may set the 8th bit of a character when the Alt key is pressed. On these terminals, you can use the corresponding byte for the mapping: map á down Newer terminals (e.g. gnome-terminal) may prefix the key with an escape character when the Alt key is pressed. lf uses the escape delaying mechanism to recognize Alt keys in these terminals (delay is 100ms). On these terminals, keys combined with the Alt key are prefixed with an a character: map down It is possible to combine special keys with modifiers: map down Combining multiple modifiers (e.g. Ctrl+Shift+Space) is not supported. Note that lf's key mapping syntax is similar to Vim's, but not identical. Some special keys and modifiers use different names and separators, and key names are matched literally (i.e. no case-folding, no aliases), so some familiar forms will not work: map down # not , or map down # not map down # not or (Meta) map down # not map down # not WARNING: Some key combinations will likely be intercepted by your OS, window manager, or terminal. Other key combinations cannot be recognized by lf due to the way terminals work (e.g. Ctrl+h combination sends a backspace key instead). The easiest way to find out the name of a key combination and whether it will work on your system is to press the key while lf is running and read the name from the unknown mapping error. Mouse buttons are prefixed with an m character: map down # primary map down # secondary map down # middle map down # thumb next map down # thumb prev map down map down map down Mouse wheel events are also prefixed with an m character: map down map down map down map down PUSH MAPPINGS The usual way to map a key sequence is to assign it to a named or unnamed command. While this provides a clean way to remap built-in keys as well as other commands, it can be limiting at times. For this reason, the push command is provided by lf. This command is used to simulate key pushes given as its arguments. You can map a key to a push command with an argument to create various keybindings. This is mainly useful for two purposes. First, it can be used to map a command with a command count: map push 10j Second, it can be used to avoid typing the name when a command takes arguments: map r push :rename One thing to be careful of is that since the push command works with keys instead of commands it is possible to accidentally create recursive bindings: map j push 2j These types of bindings create a deadlock when executed. SHELL COMMANDS Regular shell commands are the most basic command type that is useful for many purposes. For example, we can write a shell command to move the selected file(s) to trash. A first attempt to write such a command may look like this: cmd trash ${{ mkdir -p ~/.trash if [ -z "$fs" ]; then mv "$f" ~/.trash else IFS="$(printf '\n\t')"; mv $fs ~/.trash fi }} We check $fs to see if there are any selected files. Otherwise, we just delete the current file. Since this is such a common pattern, a separate $fx variable is provided. We can use this variable to get rid of the conditional: cmd trash ${{ mkdir -p ~/.trash IFS="$(printf '\n\t')"; mv $fx ~/.trash }} The trash directory is checked each time the command is executed. We can move it outside of the command so it would only run once at startup: ${{ mkdir -p ~/.trash }} cmd trash ${{ IFS="$(printf '\n\t')"; mv $fx ~/.trash }} Since these are one-liners, we can drop {{ and }}: $mkdir -p ~/.trash cmd trash $IFS="$(printf '\n\t')"; mv $fx ~/.trash Finally, note that we set the IFS variable manually in these commands. Instead, we could use the ifs option to set it for all shell commands (i.e. set ifs "\n"). This can be especially useful for interactive use (e.g. $rm $f or $rm $fs would simply work). This option is not set by default as it can behave unexpectedly for new users. However, use of this option is highly recommended and it is assumed in the rest of the documentation. PIPING SHELL COMMANDS Regular shell commands have some limitations in some cases. When an output or error message is given and the command exits afterwards, the UI is immediately resumed and there is no way to see the message without dropping to shell again. Also, even when there is no output or error, the UI still needs to be paused while the command is running. This can cause flickering on the screen for short commands and similar distractions for longer commands. Instead of pausing the UI, piping shell commands connect stdin, stdout, and stderr of the command to the statline at the bottom of the UI. This can be useful for programs following the Unix philosophy to give no output in the success case, and brief error messages or prompts in other cases. For example, the following rename command prompts for overwrite in the statline if there is an existing file with the given name: cmd rename %mv -i $f $1 You can also output error messages in the command and they will show up in the statline. For example, an alternative rename command may look like this: cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1 Note that input is line buffered and output and error are byte buffered. WAITING SHELL COMMANDS Waiting shell commands are similar to regular shell commands except that they wait for a key press when the command is finished. These can be useful to see the output of a program before the UI is resumed. Waiting shell commands are more appropriate than piping shell commands when the command is verbose and the output is best displayed as multiline. ASYNCHRONOUS SHELL COMMANDS Asynchronous shell commands are used to start a command in the background and then resume operation without waiting for the command to finish. Stdin, stdout, and stderr of the command are neither connected to the terminal nor the UI. REMOTE COMMANDS One of the more advanced features in lf is remote commands. All clients connect to a server on startup. It is possible to send commands to all or any of the connected clients over the common server. This is used internally to notify file selection changes to other clients. To use this feature, you need to use a client which supports communicating with a Unix domain socket. OpenBSD implementation of netcat (nc) is one such example. You can use it to send a command to the socket file: echo 'send echo hello world' | nc -U ${XDG_RUNTIME_DIR:-/tmp}/lf.${USER}.sock Since such a client may not be available everywhere, lf comes bundled with a command line flag to be used as such. When using lf, you do not need to specify the address of the socket file. This is the recommended way of using remote commands since it is shorter and immune to socket file address changes: lf -remote 'send echo hello world' In this command send is used to send the rest of the string as a command to all connected clients. You can optionally give it an ID number to send a command to a single client: lf -remote 'send 1234 echo hello world' All clients have a unique ID number but you may not be aware of the ID number when you are writing a command. For this purpose, an $id variable is exported to the environment for shell commands. The value of this variable is set to the process ID of the client. You can use it to send a remote command from a client to the server which in return sends a command back to itself. So now you can display a message in the current client by calling the following in a shell command: lf -remote "send $id echo hello world" Since lf does not have control flow syntax, remote commands are used for such needs. For example, you can configure the number of columns in the UI with respect to the terminal width as follows: cmd recol %{{ if [ $lf_width -le 80 ]; then lf -remote "send $id set ratios 1:2" elif [ $lf_width -le 160 ]; then lf -remote "send $id set ratios 1:2:3" else lf -remote "send $id set ratios 1:2:3:5" fi }} In addition, the query command can be used to obtain information about a specific lf instance by providing its ID: lf -remote "query $id maps" The following types of information are supported: maps list of mappings created by the 'map', 'nmap' and 'vmap' command nmaps list of mappings created by the 'nmap' and 'map' command vmaps list of mappings created by the 'vmap' and 'map' command cmaps list of mappings created by the 'cmap' command cmds list of commands created by the 'cmd' command jumps contents of the jump list, showing previously visited locations history list of previously executed commands on the command line files list of files in the currently open directory as displayed by lf, empty if dir is still loading When listing mappings the characters in the first column are: n Normal v Visual c Command-line This is useful for scripting actions based on the internal state of lf. For example, to select a previous command using fzf and execute it: map ${{ clear cmd=$( lf -remote "query $id history" | awk -F'\t' 'NR > 1 { print $NF}' | sort -u | fzf --reverse --prompt='Execute command: ' ) lf -remote "send $id $cmd" }} The list command prints the IDs of all currently connected clients: lf -remote 'list' There is also a quit command to quit the server when there are no connected clients left, and a quit! command to force quit the server by closing client connections first: lf -remote 'quit' lf -remote 'quit!' Lastly, the commands conn and drop connect or disconnect ID to/from the server: lf -remote 'conn $id' lf -remote 'drop $id' These are internal and generally not needed by users. FILE OPERATIONS lf uses its own built-in copy and move operations by default. These are implemented as asynchronous operations and progress is shown in the bottom ruler. These commands do not overwrite existing files or directories with the same name. Instead, a suffix that is compatible with the --backup=numbered option in GNU cp is added to the new files or directories. Only file modes and (some) timestamps can be preserved (see preserve option), all other attributes are ignored including ownership, context, and xattr. Special files such as character and block devices, named pipes, and sockets are skipped and links are not followed. Moving is performed using the rename operation of the underlying OS. For cross-device moving, lf falls back to copying and then deletes the original files if there are no errors. Operation errors are shown in the message line as well as the log file and they do not prematurely terminate the corresponding file operation. File operations can be performed on the currently selected file or on multiple files by selecting them first. When you copy a file, lf doesn't actually copy the file on the disk, but only records its name to a file. The actual file copying takes place when you paste. Similarly paste after a cut operation moves the file. You can customize copy and move operations by defining a paste command. This is a special command that is called when it is defined instead of the built-in implementation. You can use the following example as a starting point: cmd paste %{{ load=$(cat ~/.local/share/lf/files) mode=$(echo "$load" | sed -n '1p') list=$(echo "$load" | sed '1d') if [ $mode = 'copy' ]; then cp -R $list . elif [ $mode = 'move' ]; then mv $list . rm ~/.local/share/lf/files lf -remote 'send clear' fi }} Some useful things to be considered are to use the backup (--backup) and/or preserve attributes (-a) options with cp and mv commands if they support it (i.e. GNU implementation), change the command type to asynchronous, or use rsync command with progress bar option for copying and feed the progress to the client periodically with remote echo calls. By default, lf does not assign delete command to a key to protect new users. You can customize file deletion by defining a delete command. You can also assign a key to this command if you like. An example command to move selected files to a trash folder and remove files completely after a prompt is provided in the example configuration file. SEARCHING FILES There are two mechanisms implemented in lf to search a file in the current directory. Searching is the traditional method to move the selection to a file matching a given pattern. Finding is an alternative way to search for a pattern possibly using fewer keystrokes. The searching mechanism is implemented with commands search (default /), search-back (default ?), search-next (default n), and search-prev (default N). You can set searchmethod to glob to match using a glob pattern. Globbing supports * to match any sequence, ? to match any character, and [...] or [^...] to match character sets or ranges. You can set searchmethod to regex to match using a regex pattern. For a full overview of Go's RE2 syntax, see https://pkg.go.dev/regexp/syntax. You can enable incsearch option to jump to the current match at each keystroke while typing. In this mode, you can either use cmd-enter to accept the search or use cmd-escape to cancel the search. You can also map some other commands with cmap to accept the search and execute the command immediately afterwards. For example, you can use the right arrow key to finish the search and open the selected file with the following mapping: cmap :cmd-enter; open The finding mechanism is implemented with commands find (default f), find-back (default F), find-next (default ;), find-prev (default ,). You can disable anchorfind option to match a pattern at an arbitrary position in the filename instead of the beginning. You can set the number of keys to match using findlen option. If you set this value to zero, then the keys are read until there is only a single match. The default values of these two options are set to jump to the first file with the given initial. Some options affect both searching and finding. You can disable wrapscan option to prevent searches from being wrapped around at the end of the file list. You can disable ignorecase option to match cases in the pattern and the filename. This option is already automatically overridden if the pattern contains uppercase characters. You can disable smartcase option to disable this behavior. Two similar options ignoredia and smartdia are provided to control matching diacritics in Latin letters. OPENING FILES You can define an open command (default l and ) to configure file opening. This command is only called when the current file is not a directory, otherwise, the directory is entered instead. You can define it just as you would define any other command: cmd open $vi $fx It is possible to use different command types: cmd open &xdg-open $f You may want to use either file extensions or MIME types from file command: cmd open ${{ case $(file --mime-type -Lb $f) in text/*) vi $fx;; *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} You may want to use setsid before your opener command to have persistent processes that continue to run after lf quits. Regular shell commands (i.e. $) drop to the terminal which results in a flicker for commands that finish immediately (e.g. xdg-open in the above example). If you want to use asynchronous shell commands (i.e. &) but also want to use the terminal when necessary (e.g. vi in the above example), you can use a remote command: cmd open &{{ case $(file --mime-type -Lb $f) in text/*) lf -remote "send $id \$vi \$fx";; *) for f in $fx; do xdg-open $f > /dev/null 2> /dev/null & done;; esac }} Note that asynchronous shell commands run in their own process group by default so they do not require the manual use of setsid. The following command is provided by default: cmd open &$OPENER $f You may also use any other existing file openers as you like. Possible options are libfile-mimeinfo-perl (executable name is mimeopen), rifle (ranger's default file opener), or mimeo to name a few. PREVIEWING FILES lf previews files on the preview pane by printing the file until the end or until the preview pane is filled. This output can be enhanced by providing a custom preview script for filtering. This can be used to highlight source code, list contents of archive files or view PDF or image files to name a few. For coloring lf recognizes ANSI escape codes. To use this feature, you need to set the value of previewer option to the path of an executable file. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode ("preview" or "preload"). The output of the execution is printed in the preview pane. Different types of files can be handled by matching by extension (or MIME type from the file command): #!/bin/sh case "$1" in *.tar*) tar tf "$1";; *.zip) unzip -l "$1";; *.rar) unrar l "$1";; *.7z) 7z l "$1";; *.pdf) pdftotext "$1" -;; *) highlight -O ansi "$1";; esac Because files can be large, lf automatically closes the previewer script output pipe with a SIGPIPE when enough lines are read. Note that some programs may not respond well to SIGPIPE and will exit with a non-zero return code, which avoids caching. You may add a trailing || true command to avoid such errors: highlight -O ansi "$1" || true You may also want to use the same script in your pager mapping as well: set previewer ~/.config/lf/pv.sh map i $~/.config/lf/pv.sh $f | less -R For less pager, you may instead utilize LESSOPEN mechanism so that useful information about the file such as the full path of the file can still be displayed in the statusline below: set previewer ~/.config/lf/pv.sh map i $LESSOPEN='| ~/.config/lf/pv.sh %s' less -R $f Since the preview script is called for each file selection change, it may not generate previews fast enough if the user scrolls through files quickly. To deal with this, the preload option can be set to enable file previews to be preloaded in advance. If enabled, the preview script will be run on files in advance as the user navigates through them. In this case, if the exit code of the preview script is zero, then the output will be cached in memory and displayed by lf (useful for text or sixel previews). Otherwise, it will fallback to calling the preview script again when the file is actually selected (useful for previews managed by an external program). CHANGING DIRECTORY lf changes the working directory of the process to the current directory so that shell commands always work in the displayed directory. After quitting, it returns to the original directory where it is first launched like all shell programs. If you want to stay in the current directory after quitting, you can use one of the example lfcd wrapper shell scripts provided in the repository at https://github.com/gokcehan/lf/tree/master/etc There is a special command on-cd that runs a shell command when it is defined and the directory is changed. You can define it just as you would define any other command: cmd on-cd &{{ bash -c ' # display git repository status in your prompt source /usr/share/git/completion/git-prompt.sh GIT_PS1_SHOWDIRTYSTATE=auto GIT_PS1_SHOWSTASHSTATE=auto GIT_PS1_SHOWUNTRACKEDFILES=auto GIT_PS1_SHOWUPSTREAM=auto git=$(__git_ps1 " (%s)") fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f$git\033[0m" lf -remote "send $id set promptfmt \"$fmt\"" ' }} If you want to send escape sequences to the terminal, you can use the tty-write command to do so. The following xterm-specific escape sequence sets the terminal title to the working directory: cmd on-cd &{{ lf -remote "send $id tty-write \"\033]0;$PWD\007\"" }} This command runs whenever you change the directory but not on startup. You can add an extra call to make it run on startup as well: cmd on-cd &{{ ... }} on-cd Note that all shell commands are possible but % and & are usually more appropriate as $ and ! causes flickers and pauses respectively. There is also a pre-cd command, that works like on-cd, but is run before the directory is actually changed. Another related command is on-load which gets executed when loading a directory. LOADING DIRECTORY Similar to on-cd there also is on-load that when defined runs a shell command after loading a directory. It works well when combined with addcustominfo. The following example can be used to display git indicators in the info column: cmd on-load &{{ cd "$(dirname "$1")" || exit 1 [ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" = false ] || exit 0 cmds="" for file in "$@"; do case "$file" in */.git|*/.git/*) continue;; esac status=$(git status --porcelain --ignored -- "$file" | cut -c1-2 | head -n1) if [ -n "$status" ]; then cmds="${cmds}addcustominfo \"${file}\" \"$status\"; " else cmds="${cmds}addcustominfo \"${file}\" ''; " fi done if [ -n "$cmds" ]; then lf -remote "send $id :$cmds" fi }} Another use case could be showing the dimensions of images and videos: cmd on-load &{{ cmds="" for file in "$@"; do mime=$(file --mime-type -Lb -- "$file") case "$mime" in # vector images cause problems image/svg+xml) ;; image/*|video/*) dimensions=$(exiftool -s3 -imagesize -- "$file") cmds="${cmds}addcustominfo \"${file}\" \"$dimensions\"; " ;; esac done if [ -n "$cmds" ]; then lf -remote "send $id :$cmds" fi }} COLORS lf tries to automatically adapt its colors to the environment. It starts with a default color scheme and updates colors using values of existing environment variables possibly by overwriting its previous values. Colors are set in the following order: 1. default 2. LSCOLORS (macOS/BSD ls) 3. LS_COLORS (GNU ls) 4. LF_COLORS (lf specific) 5. colors file (lf specific) Please refer to the corresponding man pages for more information about LSCOLORS and LS_COLORS. LF_COLORS is provided with the same syntax as LS_COLORS in case you want to configure colors only for lf but not ls. This can be useful since there are some differences between ls and lf, though one should expect the same behavior for common cases. The colors file (refer to the CONFIGURATION section) is provided for easier configuration without environment variables. This file should consist of whitespace-separated pairs with a # character to start comments until the end of the line. You can configure lf colors in two different ways. First, you can only configure 8 basic colors used by your terminal and lf should pick up those colors automatically. Depending on your terminal, you should be able to select your colors from a 24-bit palette. This is the recommended approach as colors used by other programs will also match each other. Second, you can set the values of environment variables or colors file mentioned above for fine-grained customization. Note that LS_COLORS/LF_COLORS are more powerful than LSCOLORS and they can be used even when GNU programs are not installed on the system. You can combine this second method with the first method for the best results. Lastly, you may also want to configure the colors of the prompt line to match the rest of the colors. Colors of the prompt line can be configured using the promptfmt option which can include hardcoded colors as ANSI escapes. See the default value of this option to have an idea about how to color this line. It is worth noting that lf uses as many colors advertised by your terminal's entry in terminfo or infocmp databases on your system. If an entry is not present, it falls back to an internal database. If your terminal supports 24-bit colors but either does not have a database entry or does not advertise all capabilities, you can enable support by setting the $COLORTERM variable to truecolor or ensuring $TERM is set to a value that ends with -truecolor. Default lf colors are mostly taken from GNU dircolors defaults. These defaults use 8 basic colors and bold attribute. Default dircolors entries with background colors are simplified to avoid confusion with current file selection in lf. Similarly, there are only file type matchings and extension matchings are left out for simplicity. Default values are as follows given with their matching order in lf: ln 01;36 or 31;01 tw 01;34 ow 01;34 st 01;34 di 01;34 pi 33 so 01;35 bd 33;01 cd 33;01 su 01;32 sg 01;32 ex 01;32 fi 00 Note that lf first tries matching file names and then falls back to file types. The full order of matchings from most specific to least are as follows: 1. Full Path (e.g. ~/.config/lf/lfrc) 2. Dir Name (e.g. .git/) (only matches dirs with a trailing slash at the end) 3. File Type (e.g. ln) (except fi) 4. File Name (e.g. README*) 5. File Name (e.g. *README) 6. Base Name (e.g. README.*) 7. Extension (e.g. *.txt) 8. Default (i.e. fi) For example, given a regular text file /path/to/README.txt, the following entries are checked in the configuration and the first one to match is used: 1. /path/to/README.txt 2. (skipped since the file is not a directory) 3. (skipped since the file is of type fi) 4. README.txt* 5. *README.txt 6. README.* 7. *.txt 8. fi Given a regular directory /path/to/example.d, the following entries are checked in the configuration and the first one to match is used: 1. /path/to/example.d 2. example.d/ 3. di 4. example.d* 5. *example.d 6. example.* 7. *.d 8. fi Note that glob-like patterns do not perform glob matching for performance reasons. For example, you can set a variable as follows: export LF_COLORS="~/Documents=01;31:~/Downloads=01;31:~/.local/share=01;31:~/.config/lf/lfrc=31:.git/=01;32:.git*=32:*.gitignore=32:*Makefile=32:README.*=33:*.txt=34:*.md=34:ln=01;36:di=01;34:ex=01;32:" Having all entries on a single line can make it hard to read. You may instead divide it into multiple lines in between double quotes by escaping newlines with backslashes as follows: export LF_COLORS="\ ~/Documents=01;31:\ ~/Downloads=01;31:\ ~/.local/share=01;31:\ ~/.config/lf/lfrc=31:\ .git/=01;32:\ .git*=32:\ *.gitignore=32:\ *Makefile=32:\ README.*=33:\ *.txt=34:\ *.md=34:\ ln=01;36:\ di=01;34:\ ex=01;32:\ " The ln entry supports the special value target, which will use the link target to select a style. Filename rules will still apply based on the link's name -- this mirrors GNU's ls and dircolors behavior. Having such a long variable definition in a shell configuration file might be undesirable. You may instead use the colors file (refer to the CONFIGURATION section) for configuration. A sample colors file can be found at https://github.com/gokcehan/lf/blob/master/etc/colors.example You may also see the wiki page for ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code ICONS Icons are configured using LF_ICONS environment variable or an icons file (refer to the CONFIGURATION section). The variable uses the same syntax as LS_COLORS/LF_COLORS. Instead of colors, you should use single characters or symbols as values. The ln entry supports the special value target, which will use the link target to select a icon. Filename rules will still apply based on the link's name -- this mirrors GNU's ls and dircolors behavior. The icons file (refer to the CONFIGURATION section) should consist of whitespace-separated arrays with a # character to start comments until the end of the line. Each line should contain 1-3 columns: a file type or file name pattern, the icon, and an optional icon color. Using only one column disables all rules for that type or name. Do not forget to add set icons true to your lfrc to see the icons. Default values are listed below in the order lf matches them: ln l or l tw t ow d st t di d pi p so s bd b cd c su u sg g ex x fi - A sample icons file can be found at https://github.com/gokcehan/lf/blob/master/etc/icons.example A sample colored icons file can be found at https://github.com/gokcehan/lf/blob/master/etc/icons_colored.example RULER The ruler can be configured using the rulerfile option (refer to the CONFIGURATION section). The contents of the ruler file should be a Go template which is then rendered to create the actual output (refer to https://pkg.go.dev/text/template for more details on the syntax). The following data fields are exported: .Message string Includes internal messages, errors, and messages generated by the `echo`/`echomsg`/`echoerr` commands .Keys string Keys pressed by the user .Progress []string Progress indicators for copied, moved and deleted files .Copy []string List of files in the clipboard to be copied .Cut []string List of files in the clipboard to be moved .Select []string Selection list .Visual []string Visual selection .Index int Index of the cursor .Total int Number of visible files in the current working directory .Hidden int Number of hidden files in the current working directory .All int Number of all files in the current working directory .LinePercentage string Line percentage (analogous to `%p` for the `statusline` option in Vim) .ScrollPercentage string Scroll percentage (analogous to `%P` for the `statusline` option in Vim) .Filter []string Filter currently being applied .Mode string Current mode ("NORMAL" for Normal mode, and "VISUAL" for Visual mode) .Options map[string]string The value of options (e.g. `{{.Options.hidden}}`) .UserOptions map[string]string The value of user-defined options (e.g. `{{.UserOptions.foo}}`) .Stat.Path string Path of the current file .Stat.Name string Name of the current file .Stat.Extension string Extension of the current file .Stat.Size int64 Size of the current file .Stat.DirSize int64 Total size of the current directory if calculated via `calcdirsize` (`-1` if not calculated) .Stat.DirCount int Number of items in the current directory if the `dircounts` option is enabled (`-1` if the directory cannot be read) .Stat.Permissions string Permissions of the current file .Stat.ModTime string Last modified time of the current file (formatted based on the `timefmt` option) .Stat.AccessTime string Last access time of the current file (formatted based on the `timefmt` option) .Stat.BirthTime string Birth time of the current file (formatted based on the `timefmt` option) .Stat.ChangeTime string Last status (inode) change time of the current file (formatted based on the `timefmt` option) .Stat.LinkCount string Number of hard links for the current file .Stat.User string User of the current file .Stat.Group string Group of the current file .Stat.Target string Target if the current file is a symbolic link, otherwise a blank string .Stat.CustomInfo string Custom property if defined via `addcustominfo`, otherwise a blank string The following functions are exported: df func() string Get an indicator representing the amount of free disk space available env func(string) string Get the value of an environment variable humanize func(int64) string Express a file size in a human-readable format join func([]string, string) string Join a string array by a separator lower func(string) string Convert a string to lowercase substr func(string, int, int) string Get a substring based on starting index and length upper func(string) string Convert a string to uppercase The special identifier {{.SPACER}} can be used to divide the ruler into sections that are spaced evenly from each other. The default ruler file can be found at https://github.com/gokcehan/lf/blob/master/etc/ruler.default ================================================ FILE: etc/colors.example ================================================ # vim:ft=dircolors # (This is not a dircolors file but it helps to highlight colors and comments) # default values from dircolors # (entries with a leading # are not implemented in lf) # #no 00 # NORMAL # fi 00 # FILE # #rs 0 # RESET # di 01;34 # DIR # ln 01;36 # LINK # #mh 00 # MULTIHARDLINK # pi 40;33 # FIFO # so 01;35 # SOCK # #do 01;35 # DOOR # bd 40;33;01 # BLK # cd 40;33;01 # CHR # or 40;31;01 # ORPHAN # #mi 00 # MISSING # su 37;41 # SETUID # sg 30;43 # SETGID # #ca 30;41 # CAPABILITY # tw 30;42 # STICKY_OTHER_WRITABLE # ow 34;42 # OTHER_WRITABLE # st 37;44 # STICKY # ex 01;32 # EXEC # default values from lf (with matching order) # ln 01;36 # LINK # or 31;01 # ORPHAN # tw 01;34 # STICKY_OTHER_WRITABLE # ow 01;34 # OTHER_WRITABLE # st 01;34 # STICKY # di 01;34 # DIR # pi 33 # FIFO # so 01;35 # SOCK # bd 33;01 # BLK # cd 33;01 # CHR # su 01;32 # SETUID # sg 01;32 # SETGID # ex 01;32 # EXEC # fi 00 # FILE # file types (with matching order) ln 01;36 # LINK or 31;01 # ORPHAN tw 34 # STICKY_OTHER_WRITABLE ow 34 # OTHER_WRITABLE st 01;34 # STICKY di 01;34 # DIR pi 33 # FIFO so 01;35 # SOCK bd 33;01 # BLK cd 33;01 # CHR su 01;32 # SETUID sg 01;32 # SETGID ex 01;32 # EXEC fi 00 # FILE # archives or compressed (dircolors defaults) *.tar 01;31 *.tgz 01;31 *.arc 01;31 *.arj 01;31 *.taz 01;31 *.lha 01;31 *.lz4 01;31 *.lzh 01;31 *.lzma 01;31 *.tlz 01;31 *.txz 01;31 *.tzo 01;31 *.t7z 01;31 *.zip 01;31 *.z 01;31 *.dz 01;31 *.gz 01;31 *.lrz 01;31 *.lz 01;31 *.lzo 01;31 *.xz 01;31 *.zst 01;31 *.tzst 01;31 *.bz2 01;31 *.bz 01;31 *.tbz 01;31 *.tbz2 01;31 *.tz 01;31 *.deb 01;31 *.rpm 01;31 *.jar 01;31 *.war 01;31 *.ear 01;31 *.sar 01;31 *.rar 01;31 *.alz 01;31 *.ace 01;31 *.zoo 01;31 *.cpio 01;31 *.7z 01;31 *.rz 01;31 *.cab 01;31 *.wim 01;31 *.swm 01;31 *.dwm 01;31 *.esd 01;31 # image formats (dircolors defaults) *.jpg 01;35 *.jpeg 01;35 *.mjpg 01;35 *.mjpeg 01;35 *.gif 01;35 *.bmp 01;35 *.pbm 01;35 *.pgm 01;35 *.ppm 01;35 *.tga 01;35 *.xbm 01;35 *.xpm 01;35 *.tif 01;35 *.tiff 01;35 *.png 01;35 *.svg 01;35 *.svgz 01;35 *.mng 01;35 *.pcx 01;35 *.mov 01;35 *.mpg 01;35 *.mpeg 01;35 *.m2v 01;35 *.mkv 01;35 *.webm 01;35 *.ogm 01;35 *.mp4 01;35 *.m4v 01;35 *.mp4v 01;35 *.vob 01;35 *.qt 01;35 *.nuv 01;35 *.wmv 01;35 *.asf 01;35 *.rm 01;35 *.rmvb 01;35 *.flc 01;35 *.avi 01;35 *.fli 01;35 *.flv 01;35 *.gl 01;35 *.dl 01;35 *.xcf 01;35 *.xwd 01;35 *.yuv 01;35 *.cgm 01;35 *.emf 01;35 *.ogv 01;35 *.ogx 01;35 # audio formats (dircolors defaults) *.aac 00;36 *.au 00;36 *.flac 00;36 *.m4a 00;36 *.mid 00;36 *.midi 00;36 *.mka 00;36 *.mp3 00;36 *.mpc 00;36 *.ogg 00;36 *.ra 00;36 *.wav 00;36 *.oga 00;36 *.opus 00;36 *.spx 00;36 *.xspf 00;36 ================================================ FILE: etc/icons.example ================================================ # vim:ft=conf # These examples require Nerd Fonts or a compatible font to be used. # See https://www.nerdfonts.com for more information. # default values from lf (with matching order) # ln l # LINK # or l # ORPHAN # tw t # STICKY_OTHER_WRITABLE # ow d # OTHER_WRITABLE # st t # STICKY # di d # DIR # pi p # FIFO # so s # SOCK # bd b # BLK # cd c # CHR # su u # SETUID # sg g # SETGID # ex x # EXEC # fi - # FILE # file types (with matching order) ln  # LINK or  # ORPHAN tw t # STICKY_OTHER_WRITABLE ow  # OTHER_WRITABLE st t # STICKY di  # DIR pi p # FIFO so s # SOCK bd b # BLK cd c # CHR su u # SETUID sg g # SETGID ex  # EXEC fi  # FILE # disable some default filetype icons, let them choose icon by filename # ln  # LINK # or  # ORPHAN # tw # STICKY_OTHER_WRITABLE # ow # OTHER_WRITABLE # st # STICKY # di  # DIR # pi # FIFO # so # SOCK # bd # BLK # cd # CHR # su # SETUID # sg # SETGID # ex # EXEC # fi  # FILE # file extensions (vim-devicons) *.styl  *.sass  *.scss  *.htm  *.html  *.slim  *.haml  *.ejs  *.css  *.less  *.md  *.mdx  *.markdown  *.rmd  *.json  *.webmanifest  *.js  *.mjs  *.jsx  *.rb  *.gemspec  *.rake  *.php  *.py  *.pyc  *.pyo  *.pyd  *.coffee  *.mustache  *.hbs  *.conf  *.ini  *.yml  *.yaml  *.toml  *.bat  *.mk  *.jpg  *.jpeg  *.bmp  *.png  *.webp  *.gif  *.ico  *.twig  *.cpp  *.c++  *.cxx  *.cc  *.cp  *.c  *.cs 󰌛 *.h  *.hh  *.hpp  *.hxx  *.hs  *.lhs  *.nix  *.lua  *.java  *.sh  *.fish  *.bash  *.zsh  *.ksh  *.csh  *.awk  *.ps1  *.ml λ *.mli λ *.diff  *.db  *.sql  *.dump  *.clj  *.cljc  *.cljs  *.edn  *.scala  *.go  *.dart  *.xul  *.sln  *.suo  *.pl  *.pm  *.t  *.rss  '*.f#'  *.fsscript  *.fsx  *.fs  *.fsi  *.rs  *.rlib  *.d  *.erl  *.hrl  *.ex  *.exs  *.eex  *.leex  *.heex  *.vim  *.ai  *.psd  *.psb  *.ts  *.tsx  *.jl  *.pp  *.vue  *.elm  *.swift  *.xcplayground  *.tex 󰙩 *.r 󰟔 *.rproj 󰗆 *.sol 󰡪 *.pem  # file names (vim-devicons) (case-insensitive not supported in lf) *gruntfile.coffee  *gruntfile.js  *gruntfile.ls  *gulpfile.coffee  *gulpfile.js  *gulpfile.ls  *mix.lock  *dropbox  *.ds_store  *.gitconfig  *.gitignore  *.gitattributes  *.gitlab-ci.yml  *.bashrc  *.zshrc  *.zshenv  *.zprofile  *.vimrc  *.gvimrc  *_vimrc  *_gvimrc  *.bashprofile  *favicon.ico  *license  *node_modules  *react.jsx  *procfile  *dockerfile  *docker-compose.yml  *docker-compose.yaml  *compose.yml  *compose.yaml  *rakefile  *config.ru  *gemfile  *makefile  *cmakelists.txt  *robots.txt 󰚩 # file names (case-sensitive adaptations) *Gruntfile.coffee  *Gruntfile.js  *Gruntfile.ls  *Gulpfile.coffee  *Gulpfile.js  *Gulpfile.ls  *Dropbox  *.DS_Store  *LICENSE  *React.jsx  *Procfile  *Dockerfile  *Docker-compose.yml  *Docker-compose.yaml  *Rakefile  *Gemfile  *Makefile  *CMakeLists.txt  # file patterns (vim-devicons) (patterns not supported in lf) # .*jquery.*\.js$  # .*angular.*\.js$  # .*backbone.*\.js$  # .*require.*\.js$  # .*materialize.*\.js$  # .*materialize.*\.css$  # .*mootools.*\.js$  # .*vimrc.*  # Vagrantfile$  # file patterns (file name adaptations) *jquery.min.js  *angular.min.js  *backbone.min.js  *require.min.js  *materialize.min.js  *materialize.min.css  *mootools.min.js  *vimrc  Vagrantfile  # archives or compressed (extensions from dircolors defaults) *.tar  *.tgz  *.arc  *.arj  *.taz  *.lha  *.lz4  *.lzh  *.lzma  *.tlz  *.txz  *.tzo  *.t7z  *.zip  *.z  *.dz  *.gz  *.lrz  *.lz  *.lzo  *.xz  *.zst  *.tzst  *.bz2  *.bz  *.tbz  *.tbz2  *.tz  *.deb  *.rpm  *.jar  *.war  *.ear  *.sar  *.rar  *.alz  *.ace  *.zoo  *.cpio  *.7z  *.rz  *.cab  *.wim  *.swm  *.dwm  *.esd  # image formats (extensions from dircolors defaults) *.jpg  *.jpeg  *.mjpg  *.mjpeg  *.gif  *.bmp  *.pbm  *.pgm  *.ppm  *.tga  *.xbm  *.xpm  *.tif  *.tiff  *.png  *.svg  *.svgz  *.mng  *.pcx  *.mov  *.mpg  *.mpeg  *.m2v  *.mkv  *.webm  *.ogm  *.mp4  *.m4v  *.mp4v  *.vob  *.qt  *.nuv  *.wmv  *.asf  *.rm  *.rmvb  *.flc  *.avi  *.fli  *.flv  *.gl  *.dl  *.xcf  *.xwd  *.yuv  *.cgm  *.emf  *.ogv  *.ogx  # audio formats (extensions from dircolors defaults) *.aac  *.au  *.flac  *.m4a  *.mid  *.midi  *.mka  *.mp3  *.mpc  *.ogg  *.ra  *.wav  *.oga  *.opus  *.spx  *.xspf  # other formats *.pdf  ================================================ FILE: etc/icons_colored.example ================================================ # vim:ft=conf # These examples require Nerd Fonts or a compatible font to be used. # See https://www.nerdfonts.com for more information. # default values from lf (with matching order) # ln l # LINK # or l # ORPHAN # tw t # STICKY_OTHER_WRITABLE # ow d # OTHER_WRITABLE # st t # STICKY # di d # DIR # pi p # FIFO # so s # SOCK # bd b # BLK # cd c # CHR # su u # SETUID # sg g # SETGID # ex x # EXEC # fi - # FILE # file types (with matching order) ln  # LINK or  # ORPHAN tw t # STICKY_OTHER_WRITABLE ow  # OTHER_WRITABLE st t # STICKY di  # DIR pi p # FIFO so s # SOCK bd b # BLK cd c # CHR su u # SETUID sg g # SETGID ex  # EXEC fi  # FILE # disable some default filetype icons, let them choose icon by filename # ln  # LINK # or  # ORPHAN # tw # STICKY_OTHER_WRITABLE # ow # OTHER_WRITABLE # st # STICKY # di  # DIR # pi # FIFO # so # SOCK # bd # BLK # cd # CHR # su # SETUID # sg # SETGID # ex # EXEC # fi  # FILE # file extensions (vim-devicons) *.styl  00;38;2;141;193;73 *.sass  00;38;2;245;83;133 *.scss  00;38;2;245;83;133 *.htm  00;38;2;227;76;38 *.html  00;38;2;227;76;38 *.slim  00;38;2;227;76;38 *.haml  00;38;2;234;234;225 *.ejs  00;38;2;203;203;65 *.css  00;38;2;86;61;124 *.less  00;38;2;86;61;124 *.md  00;38;2;81;154;186 *.mdx  00;38;2;81;154;186 *.markdown  00;38;2;81;154;186 *.rmd  00;38;2;81;154;186 *.json  00;38;2;149;157;165 *.webmanifest  00;38;2;241;224;90 *.js  00;38;2;203;203;65 *.mjs  00;38;2;241;224;90 *.jsx  00;38;2;81;154;186 *.rb  00;38;2;112;21;22 *.gemspec  00;38;2;112;21;22 *.rake  00;38;2;112;21;22 *.php  00;38;2;160;116;196 *.py  00;38;2;81;154;186 *.pyc  00;38;2;81;154;186 *.pyo  00;38;2;81;154;186 *.pyd  00;38;2;81;154;186 *.coffee  00;38;2;203;203;65 *.mustache  00;38;2;227;121;51 *.hbs  00;38;2;240;119;43 *.conf  00;38;2;109;128;134 *.ini  00;38;2;109;128;134 *.yml  00;38;2;149;157;165 *.yaml  00;38;2;149;157;165 *.toml  00;38;2;109;128;134 *.bat  00;38;2;193;241;46 *.mk  00;38;2;109;128;134 *.jpg  00;38;2;160;116;196 *.jpeg  00;38;2;160;116;196 *.bmp  00;38;2;160;116;196 *.png  00;38;2;160;116;196 *.webp  00;38;2;160;116;196 *.gif  00;38;2;160;116;196 *.ico  00;38;2;203;203;65 *.twig  00;38;2;141;193;73 *.cpp  00;38;2;81;154;186 *.c++  00;38;2;89;158;255 *.cxx  00;38;2;81;154;186 *.cc  00;38;2;81;154;186 *.cp  00;38;2;81;154;186 *.c  00;38;2;89;158;255 *.cs 󰌛 00;38;2;89;103;6 *.h  00;38;2;160;116;196 *.hh  00;38;2;160;116;196 *.hpp  00;38;2;160;116;196 *.hxx  00;38;2;160;116;196 *.hs  00;38;2;160;116;196 *.lhs  00;38;2;160;116;196 *.nix  00;38;2;126;186;228 *.lua  00;38;2;81;160;207 *.java  00;38;2;204;62;68 *.sh  00;38;2;137;224;81 *.fish  00;38;2;137;224;81 *.bash  00;38;2;137;224;81 *.zsh  00;38;2;137;224;81 *.ksh  00;38;2;137;224;81 *.csh  00;38;2;137;224;81 *.awk  00;38;2;137;224;81 *.ps1  00;38;2;137;224;81 *.ml λ 00;38;2;227;121;51 *.mli λ 00;38;2;227;121;51 *.diff  00;38;2;65;83;91 *.db  00;38;2;218;216;216 *.sql  00;38;2;218;216;216 *.dump  00;38;2;218;216;216 *.clj  00;38;2;141;193;73 *.cljc  00;38;2;141;193;73 *.cljs  00;38;2;81;154;186 *.edn  00;38;2;81;154;186 *.scala  00;38;2;204;62;68 *.go  00;38;2;81;154;186 *.dart  00;38;2;3;88;156 *.xul  00;38;2;227;121;51 *.sln  00;38;2;133;76;199 *.suo  00;38;2;133;76;199 *.pl  00;38;2;81;154;186 *.pm  00;38;2;81;154;186 *.t  00;38;2;81;154;186 *.rss  00;38;2;251;157;59 '*.f#'  00;38;2;81;154;186 *.fsscript  00;38;2;81;154;186 *.fsx  00;38;2;81;154;186 *.fs  00;38;2;81;154;186 *.fsi  00;38;2;81;154;186 *.rs  00;38;2;222;165;132 *.rlib  00;38;2;222;165;132 *.d  00;38;2;66;120;25 *.erl  00;38;2;184;57;152 *.hrl  00;38;2;184;57;152 *.ex  00;38;2;160;116;196 *.exs  00;38;2;160;116;196 *.eex  00;38;2;160;116;196 *.leex  00;38;2;160;116;196 *.heex  00;38;2;160;116;196 *.vim  00;38;2;1;152;51 *.ai  00;38;2;203;203;65 *.psd  00;38;2;81;154;186 *.psb  00;38;2;81;154;186 *.ts  00;38;2;81;154;186 *.tsx  00;38;2;81;154;186 *.jl  00;38;2;162;112;186 *.pp  00;38;2;255;166;26 *.vue  00;38;2;141;193;73 *.elm  00;38;2;81;154;186 *.swift  00;38;2;227;121;51 *.xcplayground  00;38;2;227;121;51 *.tex 󰙩 00;38;2;61;97;23 *.r 󰟔 00;38;2;53;138;91 *.rproj 󰗆 00;38;2;53;138;91 *.sol 󰡪 00;38;2;81;154;186 *.pem  00;38;2;205;155;62 # file names (vim-devicons) (case-insensitive not supported in lf) *gruntfile.coffee  00;38;2;227;121;51 *gruntfile.js  00;38;2;227;121;51 *gruntfile.ls  00;38;2;227;121;51 *gulpfile.coffee  00;38;2;204;62;68 *gulpfile.js  00;38;2;204;62;68 *gulpfile.ls  00;38;2;204;62;68 *mix.lock  00;38;2;160;116;196 *dropbox  00;38;2;0;97;254 *.ds_store  00;38;2;77;90;94 *.gitconfig  00;38;2;65;83;91 *.gitignore  00;38;2;65;83;91 *.gitattributes  00;38;2;65;83;91 *.gitlab-ci.yml  00;38;2;226;67;41 *.bashrc  00;38;2;137;224;81 *.zshrc  00;38;2;137;224;81 *.zshenv  00;38;2;137;224;81 *.zprofile  00;38;2;137;224;81 *.vimrc  00;38;2;1;152;51 *.gvimrc  00;38;2;1;152;51 *_vimrc  00;38;2;1;152;51 *_gvimrc  00;38;2;1;152;51 *.bashprofile  00;38;2;137;224;81 *favicon.ico  00;38;2;203;203;65 *license  00;38;2;203;203;65 *node_modules  00;38;2;232;39;75 *react.jsx  00;38;2;81;154;186 *procfile  00;38;2;160;116;196 *dockerfile  00;38;2;81;154;186 *docker-compose.yml  00;38;2;81;154;186 *docker-compose.yaml  00;38;2;81;154;186 *compose.yml  00;38;2;81;154;186 *compose.yaml  00;38;2;81;154;186 *rakefile  00;38;2;112;21;22 *config.ru  00;38;2;112;21;22 *gemfile  00;38;2;112;21;22 *makefile  00;38;2;109;128;134 *cmakelists.txt  00;38;2;109;128;134 *robots.txt 󰚩 00;38;2;109;128;134 # file names (case-sensitive adaptations) *Gruntfile.coffee  00;38;2;227;121;51 *Gruntfile.js  00;38;2;227;121;51 *Gruntfile.ls  00;38;2;227;121;51 *Gulpfile.coffee  00;38;2;204;62;68 *Gulpfile.js  00;38;2;204;62;68 *Gulpfile.ls  00;38;2;204;62;68 *Dropbox  00;38;2;0;97;254 *.DS_Store  00;38;2;193;241;46 *LICENSE  00;38;2;203;203;65 *React.jsx  00;38;2;81;154;186 *Procfile  00;38;2;160;116;196 *Dockerfile  00;38;2;81;154;186 *Docker-compose.yml  00;38;2;81;154;186 *Docker-compose.yaml  00;38;2;81;154;186 *Rakefile  00;38;2;112;21;22 *Gemfile  00;38;2;112;21;22 *Makefile  00;38;2;109;128;134 *CMakeLists.txt  00;38;2;109;128;134 # file patterns (vim-devicons) (patterns not supported in lf) # .*jquery.*\.js$  00;38;2;227;117;187 # .*angular.*\.js$  00;38;2;226;50;55 # .*backbone.*\.js$  00;38;2;0;113;181 # .*require.*\.js$  00;38;2;244;74;65 # .*materialize.*\.js$  00;38;2;238;110;115 # .*materialize.*\.css$  00;38;2;238;110;115 # .*mootools.*\.js$  00;38;2;236;236;236 # .*vimrc.*  00;38;2;1;152;51 # Vagrantfile$  00;38;2;21;99;255 # file patterns (file name adaptations) *jquery.min.js  00;38;2;227;117;187 *angular.min.js  00;38;2;226;50;55 *backbone.min.js  00;38;2;0;113;181 *require.min.js  00;38;2;244;74;65 *materialize.min.js  00;38;2;238;110;115 *materialize.min.css  00;38;2;238;110;115 *mootools.min.js  00;38;2;236;236;236 *vimrc  00;38;2;1;152;51 Vagrantfile  00;38;2;21;99;255 # archives or compressed (extensions from dircolors defaults) *.tar  *.tgz  *.arc  *.arj  *.taz  *.lha  *.lz4  *.lzh  *.lzma  *.tlz  *.txz  *.tzo  *.t7z  *.zip  *.z  *.dz  *.gz  *.lrz  *.lz  *.lzo  *.xz  *.zst  *.tzst  *.bz2  *.bz  *.tbz  *.tbz2  *.tz  *.deb  *.rpm  *.jar  *.war  *.ear  *.sar  *.rar  *.alz  *.ace  *.zoo  *.cpio  *.7z  *.rz  *.cab  *.wim  *.swm  *.dwm  *.esd  # image formats (extensions from dircolors defaults) *.jpg  *.jpeg  *.mjpg  *.mjpeg  *.gif  *.bmp  *.pbm  *.pgm  *.ppm  *.tga  *.xbm  *.xpm  *.tif  *.tiff  *.png  *.svg  *.svgz  *.mng  *.pcx  *.mov  *.mpg  *.mpeg  *.m2v  *.mkv  *.webm  *.ogm  *.mp4  *.m4v  *.mp4v  *.vob  *.qt  *.nuv  *.wmv  *.asf  *.rm  *.rmvb  *.flc  *.avi  *.fli  *.flv  *.gl  *.dl  *.xcf  *.xwd  *.yuv  *.cgm  *.emf  *.ogv  *.ogx  # audio formats (extensions from dircolors defaults) *.aac  *.au  *.flac  *.m4a  *.mid  *.midi  *.mka  *.mp3  *.mpc  *.ogg  *.ra  *.wav  *.oga  *.opus  *.spx  *.xspf  # other formats *.pdf  00;38;2;179;11;0 ================================================ FILE: etc/lf.bash ================================================ # Autocompletion for bash shell. # # You may put this file to a directory used by bash-completion: # # mkdir -p ~/.local/share/bash-completion/completions # ln -s "/path/to/lf.bash" ~/.local/share/bash-completion/completions # _lf () { local -a opts=( -command -config -cpuprofile -doc -last-dir-path -log -memprofile -print-last-dir -print-selection -remote -selection-path -server -single -version -help ) if [[ $2 == -* ]]; then COMPREPLY=( $(compgen -W "${opts[*]}" -- "$2") ) else COMPREPLY=( $(compgen -f -d -- "$2") ) fi } complete -o filenames -F _lf lf lfcd ================================================ FILE: etc/lf.csh ================================================ # Autocompletion for tcsh shell. # # You need to either copy the content of this file to your shell rc file # (e.g. ~/.tcshrc) or source this file directly: # # set LF_COMPLETE = "/path/to/lf.csh" # if ( -f "$LF_COMPLETE" ) then # source "$LF_COMPLETE" # endif # set LF_ARGS = "-command -config -cpuprofile -doc -last-dir-path -log -memprofile -print-last-dir -print-selection -remote -selection-path -server -single -version -help " complete lf "C/-*/(${LF_ARGS})/" complete lfcd "C/-*/(${LF_ARGS})/" ================================================ FILE: etc/lf.fish ================================================ # Autocompletion for fish shell. # # You may put this file to a directory in $fish_complete_path variable: # # mkdir -p ~/.config/fish/completions # ln -s "/path/to/lf.fish" ~/.config/fish/completions # complete -c lf -o command -r -d 'command to execute on client initialization' complete -c lf -o config -r -d 'path to the config file (instead of the usual paths)' complete -c lf -o cpuprofile -r -d 'path to the file to write the CPU profile' complete -c lf -o doc -d 'show documentation' complete -c lf -o last-dir-path -r -d 'path to the file to write the last dir on exit (to use for cd)' complete -c lf -o log -r -d 'path to the log file to write messages' complete -c lf -o memprofile -r -d 'path to the file to write the memory profile' complete -c lf -o print-last-dir -d 'print the last dir to stdout on exit (to use for cd)' complete -c lf -o print-selection -d 'print the selected files to stdout on open (to use as open file dialog)' complete -c lf -o remote -x -d 'send remote command to server' complete -c lf -o selection-path -r -d 'path to the file to write selected files on open (to use as open file dialog)' complete -c lf -o server -d 'start server (automatic)' complete -c lf -o single -d 'start a client without server' complete -c lf -o version -d 'show version' complete -c lf -o help -d 'show help' ================================================ FILE: etc/lf.nu ================================================ # Autocompletion for nushell. # # Documentation: https://www.nushell.sh/book/externs.html # To enable autocompletion you may put this file into a directory: # # mkdir -p ~/.config/nushell/completions # ln -s "/path/to/lf.nu" ~/.config/nushell/completions # # Then you need to source this file in your nu config (Open the config with the # command 'config nu' inside the nushell) by adding: # # source ~/.config/nushell/completions/lf.nu export extern "lf" [ --command # command to execute on client initialization --config: string # path to the config file (instead of the usual paths) --cpuprofile: string # path to the file to write the CPU profile --doc # show documentation --last-dir-path: string # path to the file to write the last dir on exit (to use for cd) --log: string # path to the log file to write messages --memprofile: string # path to the file to write the memory profile --print-last-dir # print the last dir to stdout on exit (to use for cd) --print-selection # print the selected files to stdout on open (to use as open file dialog) --remote: string # send remote command to server --selection-path: string # path to the file to write selected files on open (to use as open file dialog) --server # start server (automatic) --single # start a client without server --version # show version --help # show help ] ================================================ FILE: etc/lf.ps1 ================================================ # Autocompletion for PowerShell. # # You need to either copy the content of this file to $PROFILE or call this # script directly. # using namespace System.Management.Automation Register-ArgumentCompleter -Native -CommandName 'lf' -ScriptBlock { param($wordToComplete) $completions = @( [CompletionResult]::new('-command ', '-command', [CompletionResultType]::ParameterName, 'command to execute on client initialization') [CompletionResult]::new('-config ', '-config', [CompletionResultType]::ParameterName, 'path to the config file (instead of the usual paths)') [CompletionResult]::new('-cpuprofile ', '-cpuprofile', [CompletionResultType]::ParameterName, 'path to the file to write the CPU profile') [CompletionResult]::new('-doc', '-doc', [CompletionResultType]::ParameterName, 'show documentation') [CompletionResult]::new('-last-dir-path ', '-last-dir-path', [CompletionResultType]::ParameterName, 'path to the file to write the last dir on exit (to use for cd)') [CompletionResult]::new('-log ', '-log', [CompletionResultType]::ParameterName, 'path to the log file to write messages') [CompletionResult]::new('-memprofile ', '-memprofile', [CompletionResultType]::ParameterName, 'path to the file to write the memory profile') [CompletionResult]::new('-print-last-dir', '-print-last-dir', [CompletionResultType]::ParameterName, 'print the last dir to stdout on exit (to use for cd)') [CompletionResult]::new('-print-selection', '-print-selection', [CompletionResultType]::ParameterName, 'print the selected files to stdout on open (to use as open file dialog)') [CompletionResult]::new('-remote ', '-remote', [CompletionResultType]::ParameterName, 'send remote command to server') [CompletionResult]::new('-selection-path ', '-selection-path', [CompletionResultType]::ParameterName, 'path to the file to write selected files on open (to use as open file dialog)') [CompletionResult]::new('-server', '-server', [CompletionResultType]::ParameterName, 'start server (automatic)') [CompletionResult]::new('-single', '-single', [CompletionResultType]::ParameterName, 'start a client without server') [CompletionResult]::new('-version', '-version', [CompletionResultType]::ParameterName, 'show version') [CompletionResult]::new('-help', '-help', [CompletionResultType]::ParameterName, 'show help') ) if ($wordToComplete.StartsWith('-')) { $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText } } ================================================ FILE: etc/lf.vim ================================================ " Use lf to select and open file(s) in vim (adapted from ranger). " " You need to either copy the content of this file to your ~/.vimrc or source " this file directly: " " let lfvim = "/path/to/lf.vim" " if filereadable(lfvim) " exec "source " . lfvim " endif " " You may also like to assign a key to this command: " " nnoremap l :LF " function! LF() let temp = tempname() exec 'silent !lf -selection-path=' . shellescape(temp) if !filereadable(temp) redraw! return endif let names = readfile(temp) if empty(names) redraw! return endif exec 'edit ' . fnameescape(names[0]) for name in names[1:] exec 'argadd ' . fnameescape(name) endfor redraw! endfunction command! -bar LF call LF() ================================================ FILE: etc/lf.zsh ================================================ #compdef lf lfcd # Autocompletion for zsh shell. # # You need to rename this file to _lf and add containing folder to $fpath in # ~/.zshrc file: # # fpath=(/path/to/directory/containing/the/file $fpath) # autoload -U compinit # compinit # local arguments arguments=( '-command[command to execute on client initialization]' '-config[path to the config file (instead of the usual paths)]' '-cpuprofile[path to the file to write the CPU profile]' '-doc[show documentation]' '-last-dir-path[path to the file to write the last dir on exit (to use for cd)]' '-log[path to the log file to write messages]' '-memprofile[path to the file to write the memory profile]' '-print-last-dir[print the last dir to stdout on exit (to use for cd)]' '-print-selection[print the selected files to stdout on open (to use as open file dialog)]' '-remote[send remote command to server]' '-selection-path[path to the file to write selected files on open (to use as open file dialog)]' '-server[start server (automatic)]' '-single[start a client without server]' '-version[show version]' '-help[show help]' '*:filename:_files' ) _arguments -s $arguments ================================================ FILE: etc/lfcd.cmd ================================================ @echo off rem Change working dir in cmd.exe to last dir in lf on exit. rem rem You need to put this file to a folder in %PATH% variable. chcp 65001 > nul 2>&1 for /f "usebackq tokens=*" %%d in (`lf -print-last-dir %*`) do cd /d %%d ================================================ FILE: etc/lfcd.csh ================================================ # Change working dir in tcsh to last dir in lf on exit (adapted from ranger). # # You need to either copy the content of this file to your shell rc file (e.g. # ~/.tcshrc) or source this file directly: # # setenv LF_HOME "${HOME}/.config/lf" # [ -e "${LF_HOME}/lfcd.csh" ] && source "${LF_HOME}/lfcd.csh" # # You may also like to assign a key to this command: # # bindkey -c "^O" lfcd # alias lfcd 'set _=`mktemp` && lf -last-dir-path=$_ "\!*" && set _=`cat $_ && rm -f $_` && [ -d "$_" ] && cd "$_"' ================================================ FILE: etc/lfcd.fish ================================================ # Change working dir in fish to last dir in lf on exit (adapted from ranger). # # You may put this file to a directory in $fish_function_path variable: # # mkdir -p ~/.config/fish/functions # ln -s "/path/to/lfcd.fish" ~/.config/fish/functions # # You may also like to assign a key (Ctrl-O) to this command: # # bind \co 'set old_tty (stty -g); stty sane; lfcd; stty $old_tty; commandline -f repaint' # # You may put this in a function called fish_user_key_bindings. function lfcd --wraps="lf" --description="lf - Terminal file manager (changing directory on exit)" # `command` is needed in case `lfcd` is aliased to `lf`. # Quotes will cause `cd` to not change directory if `lf` prints nothing to stdout due to an error. cd "$(command lf -print-last-dir $argv)" end ================================================ FILE: etc/lfcd.nu ================================================ # Change working dir in shell to last dir in lf on exit (adapted from ranger). # # You need to add this to your Nushell Environment Config File # (Execute 'config env' in the nushell to open it). # You may also like to assign a key (Ctrl-O) to this command: # See the documentation: https://www.nushell.sh/book/line_editor.html#keybindings # # keybindings: [ # { # name: lfcd # modifier: control # keycode: char_o # mode: [emacs, vi_normal, vi_insert] # event: { # send: executehostcommand # cmd: "lfcd" # } # } # ] # For nushell version >= 0.87.0 def --env --wrapped lfcd [...args: string] { cd (lf -print-last-dir ...$args) } ================================================ FILE: etc/lfcd.ps1 ================================================ # Change working dir in PowerShell to last dir in lf on exit. # # You need to put this file to a folder in $ENV:PATH variable. # # You may also like to assign a key to this command: # # Set-PSReadLineKeyHandler -Chord Ctrl+o -ScriptBlock { # [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() # [Microsoft.PowerShell.PSConsoleReadLine]::Insert('lfcd.ps1') # [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() # } # # You may put this in one of the profiles found in $PROFILE. # lf -print-last-dir $args | Set-Location ================================================ FILE: etc/lfcd.sh ================================================ # Change working dir in shell to last dir in lf on exit (adapted from ranger). # # You need to either copy the content of this file to your shell rc file # (e.g. ~/.bashrc) or source this file directly: # # LFCD="/path/to/lfcd.sh" # if [ -f "$LFCD" ]; then # source "$LFCD" # fi # # You may also like to assign a key (Ctrl-O) to this command: # # bind '"\C-o":"lfcd\C-m"' # bash # bindkey -s '^o' 'lfcd\n' # zsh # lfcd () { # `command` is needed in case `lfcd` is aliased to `lf` cd "$(command lf -print-last-dir "$@")" } ================================================ FILE: etc/lfrc.cmd.example ================================================ # interpreter for shell commands set shell cmd # Shell commands with multiline definitions and/or positional arguments and/or # quotes do not work in Windows. For anything but the simplest shell commands, # it is recommended to create separate script files and simply call them here # in commands or mappings. # change the editor used in default editor keybinding # There is no builtin terminal editor installed in Windows. The default editor # mapping uses 'notepad' which launches in a separate GUI window. You may # instead install a terminal editor of your choice and replace the default # editor keybinding accordingly. map e $vim %f% # change the pager used in default pager keybinding # The standard pager used in Windows is 'more' which is not a very capable # pager. You may instead install a pager of your choice and replace the default # pager keybinding accordingly. map i $less %f% # change the shell used in default shell keybinding map w $powershell # change 'help' command to use a different pager cmd help $lf -doc | less # leave some space at the top and the bottom of the screen set scrolloff 10 # use enter for shell commands map shell ================================================ FILE: etc/lfrc.example ================================================ # interpreter for shell commands set shell sh # set '-eu' options for shell commands # These options are used to have safer shell commands. Option '-e' is used to # exit on error and option '-u' is used to give error for unset variables. # Option '-f' disables pathname expansion which can be useful when $f, $fs, and # $fx variables contain names with '*' or '?' characters. However, this option # is used selectively within individual commands as it can be limiting at # times. set shellopts '-eu' # set internal field separator (IFS) to "\n" for shell commands # This is useful to automatically split file names in $fs and $fx properly # since default file separator used in these variables (i.e. 'filesep' option) # is newline. You need to consider the values of these options and create your # commands accordingly. set ifs "\n" # leave some space at the top and the bottom of the screen set scrolloff 10 # Use the `dim` attribute instead of underline for the cursor in the preview pane set cursorpreviewfmt "\033[7;2m" # use enter for shell commands map shell # show the result of execution of previous commands map ` !true # execute current file (must be executable) map x $$f map X !$f # dedicated keys for file opener actions map o &mimeopen $f map O $mimeopen --ask $f # define a custom 'open' command # This command is called when current file is not a directory. You may want to # use either file extensions and/or mime types here. Below uses an editor for # text files and a file opener for the rest. cmd open &{{ case $(file --mime-type -Lb $f) in text/*) lf -remote "send $id \$$EDITOR \$fx";; *) for f in $fx; do $OPENER $f > /dev/null 2> /dev/null & done;; esac }} # mkdir command. See wiki if you want it to select created dir map a :push %mkdir # define a custom 'rename' command without prompt for overwrite # cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1 # map r push :rename # make sure trash folder exists # %mkdir -p ~/.trash # move current file or selected files to trash folder # (also see 'man mv' for backup/overwrite options) cmd trash %set -f; mv -t ~/.trash $fx # define a custom 'delete' command # cmd delete ${{ # set -f # printf "$fx\n" # printf "delete? [y/N] " # read ans # [ "$ans" = "y" ] && rm -rf $fx # }} # use '' key for either 'trash' or 'delete' command # map trash # map delete # extract the current file with the right command # (xkcd link: https://xkcd.com/1168/) cmd extract ${{ set -f case $f in *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;; *.tar.gz|*.tgz) tar xzvf $f;; *.tar.xz|*.txz) tar xJvf $f;; *.zip) unzip $f;; *.rar) unrar x $f;; *.7z) 7z x $f;; esac }} # compress current file or selected files with tar and gunzip cmd tar ${{ set -f mkdir $1 cp -r $fx $1 tar czf $1.tar.gz $1 rm -rf $1 }} # compress current file or selected files with zip cmd zip ${{ set -f mkdir $1 cp -r $fx $1 zip -r $1.zip $1 rm -rf $1 }} ================================================ FILE: etc/lfrc.ps1.example ================================================ # interpreter for shell commands set shell pwsh # allow the usage of $args inside shell commands set shellflag "-cwa" # speed up Powershell by skipping the profile set shellopts "-nop" # Shell commands with multiline definitions and/or positional arguments and/or # quotes only partly work in Windows. For anything but the simplest shell commands, # it is recommended to create separate script files and simply call them here # in commands or mappings. # # Also, the default commands and keybindings are defined using cmd syntax (i.e. '%EDITOR%') # which does not work with Powershell. Therefore, you need to override these # with explicit choices accordingly. # change the default commands and keybindings to work in Powershell cmd open &&$Env:OPENER.Trim('"', ' ') "$Env:f" map e $&$Env:EDITOR "$Env:f" map i !&$Env:PAGER "$Env:f" map w $&$Env:SHELL cmd help !&$Env:lf -doc | &$Env:PAGER cmd maps !&$Env:lf -remote "query $Env:id maps" | &$Env:PAGER cmd nmaps !&$Env:lf -remote "query $Env:id nmaps" | &$Env:PAGER cmd vmaps !&$Env:lf -remote "query $Env:id vmaps" | &$Env:PAGER cmd cmaps !&$Env:lf -remote "query $Env:id cmaps" | &$Env:PAGER cmd cmds !&$Env:lf -remote "query $Env:id cmds" | &$Env:PAGER ================================================ FILE: etc/ruler.default ================================================ {{with .Message -}} {{. -}} {{else with .Stat -}} {{.Permissions | printf "\033[36m%s\033[0m" -}} {{with .LinkCount}} {{.}}{{end -}} {{with .User}} {{.}}{{end -}} {{with .Group}} {{.}}{{end -}} {{.Size | humanize | printf " %5s" -}} {{.ModTime | printf " %s" -}} {{with .Target}} -> {{.}}{{end -}} {{end -}} {{.SPACER -}} {{with .Keys}} {{.}}{{end -}} {{with .Progress}} {{join . " "}}{{end -}} {{with .Copy}} {{len . | printf "%s %d \033[0m" $.Options.copyfmt}}{{end -}} {{with .Cut}} {{len . | printf "%s %d \033[0m" $.Options.cutfmt}}{{end -}} {{with .Select}} {{len . | printf "%s %d \033[0m" $.Options.selectfmt}}{{end -}} {{with .Visual}} {{len . | printf "%s %d \033[0m" $.Options.visualfmt}}{{end -}} {{with .Filter}} {{join . " " | printf "\033[7;34m %s \033[0m"}}{{end -}} {{printf " %d/%d" .Index .Total}} ================================================ FILE: eval.go ================================================ package main import ( "errors" "fmt" "io" "log" "os" "path/filepath" "slices" "strconv" "strings" "time" "unicode" "unicode/utf8" "github.com/gdamore/tcell/v3" "github.com/rivo/uniseg" ) func applyBoolOpt(opt *bool, e *setExpr) error { switch { case strings.HasPrefix(e.opt, "no"): if e.val != "" { return fmt.Errorf("%s: unexpected value: %s", e.opt, e.val) } *opt = false case strings.HasSuffix(e.opt, "!"): if e.val != "" { return fmt.Errorf("%s: unexpected value: %s", e.opt, e.val) } *opt = !*opt default: switch e.val { case "", "true": *opt = true case "false": *opt = false default: return fmt.Errorf("%s: value should be empty, 'true', or 'false'", e.opt) } } return nil } func applyLocalBoolOpt(localOpt map[string]bool, globalOpt bool, e *setLocalExpr) error { opt, ok := localOpt[e.path] if !ok { opt = globalOpt } if err := applyBoolOpt(&opt, &setExpr{e.opt, e.val}); err != nil { return err } localOpt[e.path] = opt return nil } func (e *setExpr) eval(app *app, _ []string) { var err error switch e.opt { case "anchorfind", "noanchorfind", "anchorfind!": err = applyBoolOpt(&gOpts.anchorfind, e) case "autoquit", "noautoquit", "autoquit!": err = applyBoolOpt(&gOpts.autoquit, e) case "dircounts", "nodircounts", "dircounts!": err = applyBoolOpt(&gOpts.dircounts, e) if err == nil { app.nav.renew() app.ui.loadFile(app, false) } case "dirfirst", "nodirfirst", "dirfirst!": err = applyBoolOpt(&gOpts.dirfirst, e) if err == nil { app.nav.sort() } case "dironly", "nodironly", "dironly!": err = applyBoolOpt(&gOpts.dironly, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "dirpreviews", "nodirpreviews", "dirpreviews!": err = applyBoolOpt(&gOpts.dirpreviews, e) case "drawbox", "nodrawbox", "drawbox!": err = applyBoolOpt(&gOpts.drawbox, e) if err == nil { app.ui.renew() app.nav.resize(app.ui) app.ui.loadFile(app, true) } case "hidden", "nohidden", "hidden!": err = applyBoolOpt(&gOpts.hidden, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "history", "nohistory", "history!": err = applyBoolOpt(&gOpts.history, e) case "icons", "noicons", "icons!": err = applyBoolOpt(&gOpts.icons, e) case "ignorecase", "noignorecase", "ignorecase!": err = applyBoolOpt(&gOpts.ignorecase, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "ignoredia", "noignoredia", "ignoredia!": err = applyBoolOpt(&gOpts.ignoredia, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "incfilter", "noincfilter", "incfilter!": err = applyBoolOpt(&gOpts.incfilter, e) case "incsearch", "noincsearch", "incsearch!": err = applyBoolOpt(&gOpts.incsearch, e) case "mergeindicators", "nomergeindicators", "mergeindicators!": err = applyBoolOpt(&gOpts.mergeindicators, e) case "mouse", "nomouse", "mouse!": err = applyBoolOpt(&gOpts.mouse, e) if err == nil { if gOpts.mouse { app.ui.screen.EnableMouse(tcell.MouseButtonEvents) } else { app.ui.screen.DisableMouse() } } case "number", "nonumber", "number!": err = applyBoolOpt(&gOpts.number, e) case "preload", "nopreload", "preload!": err = applyBoolOpt(&gOpts.preload, e) case "preview", "nopreview", "preview!": preview := gOpts.preview err = applyBoolOpt(&preview, e) if preview && len(gOpts.ratios) < 2 { err = errors.New("preview: 'ratios' should consist of at least two numbers before enabling 'preview'") } if err == nil { gOpts.preview = preview app.ui.sxScreen.forceClear = true app.ui.loadFile(app, true) } case "relativenumber", "norelativenumber", "relativenumber!": err = applyBoolOpt(&gOpts.relativenumber, e) case "reverse", "noreverse", "reverse!": err = applyBoolOpt(&gOpts.reverse, e) if err == nil { app.nav.sort() } // DEPRECATED: remove after r42 is released case "roundbox", "noroundbox", "roundbox!": app.ui.echoerr("option 'roundbox' is deprecated, use 'borderstyle' instead") case "showbinds", "noshowbinds", "showbinds!": err = applyBoolOpt(&gOpts.showbinds, e) case "smartcase", "nosmartcase", "smartcase!": err = applyBoolOpt(&gOpts.smartcase, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "smartdia", "nosmartdia", "smartdia!": err = applyBoolOpt(&gOpts.smartdia, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "watch", "nowatch", "watch!": err = applyBoolOpt(&gOpts.watch, e) if err == nil { if gOpts.watch { app.watch.start() for _, dir := range app.nav.dirCache { app.watchDir(dir) } } else { app.watch.stop() } } case "wrapscan", "nowrapscan", "wrapscan!": err = applyBoolOpt(&gOpts.wrapscan, e) case "wrapscroll", "nowrapscroll", "wrapscroll!": err = applyBoolOpt(&gOpts.wrapscroll, e) case "borderfmt": gOpts.borderfmt = e.val case "cleaner": gOpts.cleaner = replaceTilde(e.val) case "copyfmt": gOpts.copyfmt = e.val case "cursoractivefmt": gOpts.cursoractivefmt = e.val case "cursorparentfmt": gOpts.cursorparentfmt = e.val case "cursorpreviewfmt": gOpts.cursorpreviewfmt = e.val case "cutfmt": gOpts.cutfmt = e.val case "borderstyle": switch e.val { case "box": gOpts.borderstyle = borderBox case "roundbox": gOpts.borderstyle = borderRoundBox case "outline": gOpts.borderstyle = borderOutline case "roundoutline": gOpts.borderstyle = borderRoundOutline case "separators": gOpts.borderstyle = borderSeparators default: app.ui.echoerr("borderstyle: value should either be 'box', 'roundbox', 'outline', 'roundoutline' or 'separators'") return } app.ui.renew() app.nav.resize(app.ui) app.ui.loadFile(app, true) case "dupfilefmt": gOpts.dupfilefmt = e.val case "errorfmt": gOpts.errorfmt = e.val case "filesep": gOpts.filesep = e.val case "filtermethod": switch e.val { case "text", "glob", "regex": gOpts.filtermethod = searchMethod(e.val) default: app.ui.echoerr("filtermethod: value should either be 'text', 'glob' or 'regex") return } app.nav.sort() app.nav.position() app.ui.loadFile(app, true) case "findlen": n, err := strconv.Atoi(e.val) if err != nil { app.ui.echoerrf("findlen: %s", err) return } if n < 0 { app.ui.echoerr("findlen: value should be a non-negative number") return } gOpts.findlen = n case "hiddenfiles": toks := strings.Split(e.val, ":") for _, s := range toks { if s == "" { app.ui.echoerr("hiddenfiles: glob should be non-empty") return } _, err := filepath.Match(s, "a") if err != nil { app.ui.echoerrf("hiddenfiles: %s", err) return } } gOpts.hiddenfiles = toks app.nav.sort() app.nav.position() app.ui.loadFile(app, true) case "ifs": gOpts.ifs = e.val case "info": if e.val == "" { gOpts.info = nil return } toks := strings.Split(e.val, ":") for _, s := range toks { switch s { case "size", "time", "atime", "btime", "ctime", "perm", "user", "group", "custom": default: app.ui.echoerr("info: should consist of 'size', 'time', 'atime', 'btime', 'ctime', 'perm', 'user', 'group' or 'custom' separated with colon") return } } gOpts.info = toks case "infotimefmtnew": gOpts.infotimefmtnew = e.val case "infotimefmtold": gOpts.infotimefmtold = e.val case "menufmt": gOpts.menufmt = e.val case "menuheaderfmt": gOpts.menuheaderfmt = e.val case "menuselectfmt": gOpts.menuselectfmt = e.val case "numbercursorfmt": gOpts.numbercursorfmt = e.val case "numberfmt": gOpts.numberfmt = e.val case "period": n, err := strconv.Atoi(e.val) if err != nil { app.ui.echoerrf("period: %s", err) return } if n < 0 { app.ui.echoerr("period: value should be a non-negative number") return } gOpts.period = n if n == 0 { app.ticker.Stop() } else { app.ticker.Stop() app.ticker = time.NewTicker(time.Duration(gOpts.period) * time.Second) } case "preserve": if e.val == "" { gOpts.preserve = nil return } toks := strings.Split(e.val, ":") for _, s := range toks { switch s { case "mode", "timestamps": default: app.ui.echoerr("preserve: should consist of 'mode' or 'timestamps' separated with colon") return } } gOpts.preserve = toks case "previewer": gOpts.previewer = replaceTilde(e.val) case "promptfmt": gOpts.promptfmt = e.val case "ratios": toks := strings.Split(e.val, ":") rats := make([]int, 0, len(toks)) for _, s := range toks { n, err := strconv.Atoi(s) if err != nil { app.ui.echoerrf("ratios: %s", err) return } if n <= 0 { app.ui.echoerr("ratios: value should be a positive number") return } rats = append(rats, n) } if gOpts.preview && len(rats) < 2 { app.ui.echoerr("ratios: should consist of at least two numbers when 'preview' is enabled") return } gOpts.ratios = rats app.ui.wins = getWins(app.ui.screen) app.nav.resize(app.ui) app.ui.loadFile(app, true) case "rulerfile", "norulerfile", "rulerfile!": gOpts.rulerfile = replaceTilde(e.val) app.ui.ruler, app.ui.rulerErr = parseRuler(gOpts.rulerfile) case "rulerfmt": gOpts.rulerfmt = e.val case "scrolloff": n, err := strconv.Atoi(e.val) if err != nil { app.ui.echoerrf("scrolloff: %s", err) return } if n < 0 { app.ui.echoerr("scrolloff: value should be a non-negative number") return } gOpts.scrolloff = n case "searchmethod": switch e.val { case "text", "glob", "regex": gOpts.searchmethod = searchMethod(e.val) default: app.ui.echoerr("searchmethod: value should either be 'text', 'glob' or 'regex'") return } case "selectfmt": gOpts.selectfmt = e.val case "selmode": switch e.val { case "all", "dir": gOpts.selmode = e.val default: app.ui.echoerr("selmode: value should either be 'all' or 'dir'") return } case "shell": gOpts.shell = e.val case "shellflag": gOpts.shellflag = e.val case "shellopts": if e.val == "" { gOpts.shellopts = nil return } gOpts.shellopts = strings.Split(e.val, ":") case "sizeunits": switch e.val { case "binary", "decimal": gOpts.sizeunits = e.val default: app.ui.echoerr("sizeunits: value should either be 'binary' or 'decimal'") return } case "sortby": method := sortMethod(e.val) if !isValidSortMethod(method) { app.ui.echoerr(invalidSortErrorMessage) return } gOpts.sortby = method app.nav.sort() case "statfmt": gOpts.statfmt = e.val case "tabstop": n, err := strconv.Atoi(e.val) if err != nil { app.ui.echoerrf("tabstop: %s", err) return } if n <= 0 { app.ui.echoerr("tabstop: value should be a positive number") return } gOpts.tabstop = n case "tagfmt": gOpts.tagfmt = e.val case "tempmarks": gOpts.tempmarks = "'" + e.val case "terminalcursor": style := cursorStyle(e.val) switch style { case defaultCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleDefault) case blockCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleSteadyBlock) case underlineCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleSteadyUnderline) case barCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleSteadyBar) case blinkBlockCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleBlinkingBlock) case blinkUnderlineCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleBlinkingUnderline) case blinkBarCursor: app.ui.screen.SetCursorStyle(tcell.CursorStyleBlinkingBar) default: app.ui.echoerr("terminalcursor: value should either be 'default', 'block', 'underline', 'bar', 'blinkblock', 'blinkunderline' or 'blinkbar'") return } gOpts.terminalcursor = style case "timefmt": gOpts.timefmt = e.val case "truncatechar": if uniseg.StringWidth(e.val) != 1 { app.ui.echoerr("truncatechar: value should be a single character") return } gOpts.truncatechar = e.val case "truncatepct": n, err := strconv.Atoi(e.val) if err != nil { app.ui.echoerrf("truncatepct: %s", err) return } if n < 0 || n > 100 { app.ui.echoerrf("truncatepct: must be between 0 and 100 (both inclusive), got %d", n) return } gOpts.truncatepct = n case "visualfmt": gOpts.visualfmt = e.val case "waitmsg": gOpts.waitmsg = e.val default: // any key with the prefix user_ is accepted as a user defined option if strings.HasPrefix(e.opt, "user_") { gOpts.user[e.opt[5:]] = e.val // Export user defined options immediately, so that the current values // are available for some external previewer, which is started in a // different thread and thus cannot export (as `setenv` is not thread-safe). os.Setenv("lf_"+e.opt, e.val) } else { err = fmt.Errorf("unknown option: %s", e.opt) } } if err != nil { app.ui.echoerr(err.Error()) } } func (e *setLocalExpr) eval(app *app, _ []string) { var err error e.path, err = filepath.Abs(replaceTilde(e.path)) if err != nil { app.ui.echoerrf("setlocal: %s", err) return } switch e.opt { case "dircounts", "nodircounts", "dircounts!": err = applyLocalBoolOpt(gLocalOpts.dircounts, gOpts.dircounts, e) case "dirfirst", "nodirfirst", "dirfirst!": err = applyLocalBoolOpt(gLocalOpts.dirfirst, gOpts.dirfirst, e) if err == nil { app.nav.sort() } case "dironly", "nodironly", "dironly!": err = applyLocalBoolOpt(gLocalOpts.dironly, gOpts.dironly, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "hidden", "nohidden", "hidden!": err = applyLocalBoolOpt(gLocalOpts.hidden, gOpts.hidden, e) if err == nil { app.nav.sort() app.nav.position() app.ui.loadFile(app, true) } case "reverse", "noreverse", "reverse!": err = applyLocalBoolOpt(gLocalOpts.reverse, gOpts.reverse, e) if err == nil { app.nav.sort() } case "info": if e.val == "" { gLocalOpts.info[e.path] = nil return } toks := strings.Split(e.val, ":") for _, s := range toks { switch s { case "size", "time", "atime", "btime", "ctime", "perm", "user", "group", "custom": default: app.ui.echoerr("info: should consist of 'size', 'time', 'atime', 'btime', 'ctime', 'perm', 'user', 'group' or 'custom' separated with colon") return } } gLocalOpts.info[e.path] = toks case "sortby": method := sortMethod(e.val) if !isValidSortMethod(method) { app.ui.echoerr(invalidSortErrorMessage) return } gLocalOpts.sortby[e.path] = method app.nav.sort() default: err = fmt.Errorf("unknown option: %s", e.opt) } if err != nil { app.ui.echoerr(err.Error()) } } func (e *mapExpr) eval(app *app, _ []string) { if e.expr == nil { delete(gOpts.nkeys, e.keys) delete(gOpts.vkeys, e.keys) } else { gOpts.nkeys[e.keys] = e.expr gOpts.vkeys[e.keys] = e.expr } } func (e *nmapExpr) eval(app *app, _ []string) { if e.expr == nil { delete(gOpts.nkeys, e.keys) } else { gOpts.nkeys[e.keys] = e.expr } } func (e *vmapExpr) eval(app *app, _ []string) { if e.expr == nil { delete(gOpts.vkeys, e.keys) } else { gOpts.vkeys[e.keys] = e.expr } } func (e *cmapExpr) eval(app *app, _ []string) { if e.expr == nil { delete(gOpts.cmdkeys, e.key) } else { gOpts.cmdkeys[e.key] = e.expr } } func (e *cmdExpr) eval(app *app, _ []string) { if e.expr == nil { delete(gOpts.cmds, e.name) } else { gOpts.cmds[e.name] = e.expr } // only enable focus reporting if required by the user if e.name == "on-focus-gained" || e.name == "on-focus-lost" { _, onFocusGainedExists := gOpts.cmds["on-focus-gained"] _, onFocusLostExists := gOpts.cmds["on-focus-lost"] if onFocusGainedExists || onFocusLostExists { app.ui.screen.EnableFocus() } else { app.ui.screen.DisableFocus() } } } func preChdir(app *app) { if cmd, ok := gOpts.cmds["pre-cd"]; ok { cmd.eval(app, nil) } } func onChdir(app *app) { app.nav.addJumpList() if cmd, ok := gOpts.cmds["on-cd"]; ok { cmd.eval(app, nil) } } func onLoad(app *app, files []string) { if cmd, ok := gOpts.cmds["on-load"]; ok { cmd.eval(app, files) } } func onFocusGained(app *app) { if cmd, ok := gOpts.cmds["on-focus-gained"]; ok { cmd.eval(app, nil) } } func onFocusLost(app *app) { if cmd, ok := gOpts.cmds["on-focus-lost"]; ok { cmd.eval(app, nil) } } func onInit(app *app) { if cmd, ok := gOpts.cmds["on-init"]; ok { cmd.eval(app, nil) } } func onRedraw(app *app) { if cmd, ok := gOpts.cmds["on-redraw"]; ok { cmd.eval(app, nil) } } func onSelect(app *app) { app.nav.preload() if cmd, ok := gOpts.cmds["on-select"]; ok { cmd.eval(app, nil) } } func onQuit(app *app) { if cmd, ok := gOpts.cmds["on-quit"]; ok { cmd.eval(app, nil) } } func splitKeys(s string) (keys []string) { for i := 0; i < len(s); { r, w := utf8.DecodeRuneInString(s[i:]) if r != '<' { keys = append(keys, s[i:i+w]) i += w } else { j := i + w for r != '>' && j < len(s) { r, w = utf8.DecodeRuneInString(s[j:]) j += w } keys = append(keys, s[i:j]) i = j } } return } func update(app *app) { exitCompMenu(app) app.cmdHistoryInput = nil switch { case gOpts.incsearch && app.ui.cmdPrefix == "/": app.nav.search = app.ui.cmdAccLeft + app.ui.cmdAccRight if app.nav.search == "" { return } dir := app.nav.currDir() old := dir.ind dir.ind = app.nav.searchInd dir.pos = app.nav.searchPos if _, err := app.nav.searchNext(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) } else if old != dir.ind { app.ui.loadFile(app, true) } case gOpts.incsearch && app.ui.cmdPrefix == "?": app.nav.search = app.ui.cmdAccLeft + app.ui.cmdAccRight if app.nav.search == "" { return } dir := app.nav.currDir() old := dir.ind dir.ind = app.nav.searchInd dir.pos = app.nav.searchPos if _, err := app.nav.searchPrev(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) } else if old != dir.ind { app.ui.loadFile(app, true) } case gOpts.incfilter && app.ui.cmdPrefix == "filter: ": filter := app.ui.cmdAccLeft + app.ui.cmdAccRight dir := app.nav.currDir() old := dir.ind if err := app.nav.setFilter(strings.Split(filter, " ")); err != nil { app.ui.echoerrf("filter: %s", err) } else if old != dir.ind { app.ui.loadFile(app, true) } } } func restartIncCmd(app *app) { if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { dir := app.nav.currDir() app.nav.searchInd = dir.ind app.nav.searchPos = dir.pos update(app) } else if gOpts.incfilter && app.ui.cmdPrefix == "filter: " { dir := app.nav.currDir() app.nav.prevFilter = dir.filter update(app) } } func resetIncCmd(app *app) { if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { dir := app.nav.currDir() dir.pos = app.nav.searchPos if dir.ind != app.nav.searchInd { dir.ind = app.nav.searchInd app.ui.loadFile(app, true) } } else if gOpts.incfilter && app.ui.cmdPrefix == "filter: " { dir := app.nav.currDir() old := dir.ind if err := app.nav.setFilter(app.nav.prevFilter); err != nil { log.Printf("reset filter: %s", err) } else if old != dir.ind { app.ui.loadFile(app, true) } } } func normal(app *app) { resetIncCmd(app) exitCompMenu(app) app.cmdHistoryInd = 0 app.cmdHistoryInput = nil app.ui.cmdAccLeft = "" app.ui.cmdAccRight = "" app.ui.cmdPrefix = "" } func insert(app *app, arg string) { switch { case gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?"): app.ui.cmdAccLeft += arg update(app) case gOpts.incfilter && app.ui.cmdPrefix == "filter: ": app.ui.cmdAccLeft += arg update(app) case app.ui.cmdPrefix == "find: ": app.nav.find = app.ui.cmdAccLeft + arg + app.ui.cmdAccRight if gOpts.findlen == 0 { switch app.nav.findSingle() { case 0: app.ui.echoerrf("find: pattern not found: %s", app.nav.find) case 1: app.ui.loadFile(app, true) default: app.ui.cmdAccLeft += arg return } } else { if len(app.nav.find) < gOpts.findlen { app.ui.cmdAccLeft += arg return } if moved, found := app.nav.findNext(); !found { app.ui.echoerrf("find: pattern not found: %s", app.nav.find) } else if moved { app.ui.loadFile(app, true) } } normal(app) case app.ui.cmdPrefix == "find-back: ": app.nav.find = app.ui.cmdAccLeft + arg + app.ui.cmdAccRight if gOpts.findlen == 0 { switch app.nav.findSingle() { case 0: app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) case 1: app.ui.loadFile(app, true) default: app.ui.cmdAccLeft += arg return } } else { if len(app.nav.find) < gOpts.findlen { app.ui.cmdAccLeft += arg return } if moved, found := app.nav.findPrev(); !found { app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) } else if moved { app.ui.loadFile(app, true) } } normal(app) case strings.HasPrefix(app.ui.cmdPrefix, "delete"): normal(app) if arg == "y" { if err := app.nav.del(app); err != nil { app.ui.echoerrf("delete: %s", err) return } app.nav.unselect() app.ui.loadFile(app, true) } case strings.HasPrefix(app.ui.cmdPrefix, "replace"): normal(app) if arg == "y" { if err := app.nav.rename(); err != nil { app.ui.echoerrf("rename: %s", err) return } if gSingleMode { app.nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { app.ui.echoerrf("rename: %s", err) return } } app.ui.loadFile(app, true) } case strings.HasPrefix(app.ui.cmdPrefix, "create"): normal(app) if arg == "y" { if err := os.MkdirAll(filepath.Dir(app.nav.renameNewPath), os.ModePerm); err != nil { app.ui.echoerrf("rename: %s", err) return } if err := app.nav.rename(); err != nil { app.ui.echoerrf("rename: %s", err) return } if gSingleMode { app.nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { app.ui.echoerrf("rename: %s", err) return } } app.ui.loadFile(app, true) } case app.ui.cmdPrefix == "mark-save: ": normal(app) app.nav.marks[arg] = app.nav.currDir().path if err := app.nav.writeMarks(); err != nil { app.ui.echoerrf("mark-save: %s", err) return } if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("mark-save: %s", err) return } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("mark-save: %s", err) return } } case app.ui.cmdPrefix == "mark-load: ": normal(app) path, ok := app.nav.marks[arg] if !ok { app.ui.echoerr("mark-load: no such mark") return } if err := cd(app, path); err != nil { app.ui.echoerrf("mark-load: %s", err) } case app.ui.cmdPrefix == "mark-remove: ": normal(app) if err := app.nav.removeMark(arg); err != nil { app.ui.echoerrf("mark-remove: %s", err) return } if err := app.nav.writeMarks(); err != nil { app.ui.echoerrf("mark-remove: %s", err) return } if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("mark-remove: %s", err) return } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("mark-remove: %s", err) return } } case app.ui.cmdPrefix == ":" && app.ui.cmdAccLeft == "": switch arg { case "!", "$", "%", "&": app.ui.cmdPrefix = arg app.cmdHistoryInd = 0 app.cmdHistoryInput = nil return } fallthrough default: exitCompMenu(app) app.cmdHistoryInput = nil app.ui.cmdAccLeft += arg } } func cd(app *app, path string) error { wd := app.nav.currDir().path path, err := filepath.Abs(replaceTilde(path)) if err != nil { return fmt.Errorf("getting absolute path: %w", err) } if path == wd { return nil } resetIncCmd(app) preChdir(app) if err := app.nav.cd(path); err != nil { return fmt.Errorf("changing directory: %w", err) } app.ui.loadFile(app, true) app.nav.marks["'"] = wd restartIncCmd(app) onChdir(app) return nil } func exitCompMenu(app *app) { app.ui.menu = "" app.ui.menuSelect = nil app.menuCompActive = false } func (e *callExpr) eval(app *app, _ []string) { os.Setenv("lf_count", strconv.Itoa(e.count)) // commands that shouldn't clear the message line silentCmds := []string{ "addcustominfo", "clearmaps", "draw", "load", "push", "redraw", "source", "sync", "tty-write", "on-focus-gained", "on-focus-lost", "on-init", } if !slices.Contains(silentCmds, e.name) && app.ui.cmdPrefix != ">" { app.ui.echo("") } switch e.name { case "quit": app.quitChan <- struct{}{} case "up": if app.nav.up(e.count) { app.ui.loadFile(app, true) } case "half-up": if app.nav.up(e.count * app.nav.height / 2) { app.ui.loadFile(app, true) } case "page-up": if app.nav.up(e.count * app.nav.height) { app.ui.loadFile(app, true) } case "scroll-up": if app.nav.scrollUp(e.count) { app.ui.loadFile(app, true) } case "down": if app.nav.down(e.count) { app.ui.loadFile(app, true) } case "half-down": if app.nav.down(e.count * app.nav.height / 2) { app.ui.loadFile(app, true) } case "page-down": if app.nav.down(e.count * app.nav.height) { app.ui.loadFile(app, true) } case "scroll-down": if app.nav.scrollDown(e.count) { app.ui.loadFile(app, true) } case "updir": resetIncCmd(app) preChdir(app) for range e.count { if err := app.nav.updir(); err != nil { app.ui.echoerrf("%s", err) return } } app.ui.loadFile(app, true) restartIncCmd(app) onChdir(app) case "open": curr := app.nav.currFile() if curr == nil { return } if curr.IsDir() { resetIncCmd(app) preChdir(app) err := app.nav.open() if err != nil { app.ui.echoerrf("opening directory: %s", err) return } app.ui.loadFile(app, true) restartIncCmd(app) onChdir(app) } else { if gSelectionPath != "" || gPrintSelection { app.selectionOut, _ = app.nav.currFileOrSelections() app.quitChan <- struct{}{} return } if cmd, ok := gOpts.cmds["open"]; ok { cmd.eval(app, e.args) } } case "jump-next": resetIncCmd(app) preChdir(app) for range e.count { app.nav.cdJumpListNext() } app.ui.loadFile(app, true) restartIncCmd(app) onChdir(app) case "jump-prev": resetIncCmd(app) preChdir(app) for range e.count { app.nav.cdJumpListPrev() } app.ui.loadFile(app, true) restartIncCmd(app) onChdir(app) case "top": var moved bool if e.count == 1 { moved = app.nav.top() } else { moved = app.nav.move(e.count - 1) } if moved { app.ui.loadFile(app, true) } case "bottom": var moved bool if e.count == 1 { // Different from Vim, which would treat a count of 1 as meaning to // move to the first line (i.e. the top) moved = app.nav.bottom() } else { moved = app.nav.move(e.count - 1) } if moved { app.ui.loadFile(app, true) } case "high": if app.nav.high() { app.ui.loadFile(app, true) } case "middle": if app.nav.middle() { app.ui.loadFile(app, true) } case "low": if app.nav.low() { app.ui.loadFile(app, true) } case "toggle": if len(e.args) == 0 { app.nav.toggle() } else { for _, path := range e.args { path, err := filepath.Abs(replaceTilde(path)) if err != nil { app.ui.echoerrf("toggle: %s", err) continue } if _, err := os.Lstat(path); os.IsNotExist(err) { app.ui.echoerrf("toggle: %s", err) continue } app.nav.toggleSelection(path) } } case "invert": app.nav.invert() case "unselect": app.nav.unselect() case "glob-select": if len(e.args) != 1 { app.ui.echoerr("glob-select: requires a pattern to match") return } if err := app.nav.globSel(e.args[0], false); err != nil { app.ui.echoerrf("%s", err) return } case "glob-unselect": if len(e.args) != 1 { app.ui.echoerr("glob-unselect: requires a pattern to match") return } if err := app.nav.globSel(e.args[0], true); err != nil { app.ui.echoerrf("%s", err) return } case "copy": if err := app.nav.save(clipboardCopy); err != nil { app.ui.echoerrf("copy: %s", err) return } app.nav.unselect() if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("copy: %s", err) return } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("copy: %s", err) return } } case "cut": if err := app.nav.save(clipboardCut); err != nil { app.ui.echoerrf("cut: %s", err) return } app.nav.unselect() if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("cut: %s", err) return } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("cut: %s", err) return } } case "paste": if cmd, ok := gOpts.cmds["paste"]; ok { cmd.eval(app, e.args) } else if err := app.nav.paste(app); err != nil { app.ui.echoerrf("paste: %s", err) return } app.ui.loadFile(app, true) case "clear": if err := saveFiles(clipboard{nil, clipboardCut}); err != nil { app.ui.echoerrf("clear: %s", err) return } if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("clear: %s", err) return } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("clear: %s", err) return } } case "sync": if err := app.nav.sync(); err != nil { app.ui.echoerrf("sync: %s", err) } case "draw": case "redraw": app.ui.screen.Sync() app.ui.renew() app.nav.resize(app.ui) app.ui.sxScreen.forceClear = true app.ui.loadFile(app, true) onRedraw(app) case "load": if gOpts.watch { return } app.nav.renew() app.ui.loadFile(app, false) case "reload": app.nav.reload() app.ui.loadFile(app, true) case "delete": if cmd, ok := gOpts.cmds["delete"]; ok { cmd.eval(app, e.args) app.nav.unselect() if gSingleMode { app.nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { app.ui.echoerrf("delete: %s", err) return } } } else { list, err := app.nav.currFileOrSelections() if err != nil { app.ui.echoerrf("delete: %s", err) return } if app.ui.cmdPrefix == ">" { return } normal(app) if len(list) == 1 { app.ui.cmdPrefix = "delete '" + list[0] + "'? [y/N] " } else { app.ui.cmdPrefix = "delete " + strconv.Itoa(len(list)) + " items? [y/N] " } } case "rename": if cmd, ok := gOpts.cmds["rename"]; ok { cmd.eval(app, e.args) if gSingleMode { app.nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { app.ui.echoerrf("rename: %s", err) return } } } else { curr := app.nav.currFile() if curr == nil { app.ui.echoerr("rename: empty directory") return } if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "rename: " extension := getFileExtension(curr) if len(extension) == 0 { // no extension or .hidden or is directory app.ui.cmdAccLeft = curr.Name() } else { app.ui.cmdAccLeft = strings.TrimSuffix(curr.Name(), extension) app.ui.cmdAccRight = extension } } case "read": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = ":" case "shell": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "$" case "shell-pipe": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "%" case "shell-wait": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "!" case "shell-async": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "&" case "find": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "find: " app.nav.findBack = false case "find-back": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "find-back: " app.nav.findBack = true case "find-next": dir := app.nav.currDir() old := dir.ind for range e.count { if app.nav.findBack { app.nav.findPrev() } else { app.nav.findNext() } } if old != dir.ind { app.ui.loadFile(app, true) } case "find-prev": dir := app.nav.currDir() old := dir.ind for range e.count { if app.nav.findBack { app.nav.findNext() } else { app.nav.findPrev() } } if old != dir.ind { app.ui.loadFile(app, true) } case "search": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "/" dir := app.nav.currDir() app.nav.searchInd = dir.ind app.nav.searchPos = dir.pos app.nav.searchBack = false case "search-back": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "?" dir := app.nav.currDir() app.nav.searchInd = dir.ind app.nav.searchPos = dir.pos app.nav.searchBack = true case "search-next": for range e.count { if app.nav.searchBack { if moved, err := app.nav.searchPrev(); err != nil { app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) } else if moved { app.ui.loadFile(app, true) } } else { if moved, err := app.nav.searchNext(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) } else if moved { app.ui.loadFile(app, true) } } } case "search-prev": for range e.count { if app.nav.searchBack { if moved, err := app.nav.searchNext(); err != nil { app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) } else if moved { app.ui.loadFile(app, true) } } else { if moved, err := app.nav.searchPrev(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) } else if moved { app.ui.loadFile(app, true) } } } case "filter": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "filter: " dir := app.nav.currDir() app.nav.prevFilter = dir.filter if len(e.args) == 0 { app.ui.cmdAccLeft = strings.Join(dir.filter, " ") } else { app.ui.cmdAccLeft = strings.Join(e.args, " ") } case "setfilter": log.Printf("filter: %s", e.args) if err := app.nav.setFilter(e.args); err != nil { app.ui.echoerrf("filter: %s", err) } app.ui.loadFile(app, true) case "mark-save": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.cmdPrefix = "mark-save: " case "mark-load": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.menu = listMarks(app.nav.marks) app.ui.cmdPrefix = "mark-load: " case "mark-remove": if app.ui.cmdPrefix == ">" { return } normal(app) app.ui.menu = listMarks(app.nav.marks) app.ui.cmdPrefix = "mark-remove: " case "tag": tag := "*" if len(e.args) != 0 { tag = e.args[0] } if err := app.nav.tag(tag); err != nil { app.ui.echoerrf("tag: %s", err) } else if err := app.nav.writeTags(); err != nil { app.ui.echoerrf("tag: %s", err) } if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("tag: %s", err) } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("tag: %s", err) } } case "tag-toggle": tag := "*" if len(e.args) != 0 { tag = e.args[0] } if err := app.nav.tagToggle(tag); err != nil { app.ui.echoerrf("tag-toggle: %s", err) } else if err := app.nav.writeTags(); err != nil { app.ui.echoerrf("tag-toggle: %s", err) } if gSingleMode { if err := app.nav.sync(); err != nil { app.ui.echoerrf("tag-toggle: %s", err) } } else { if _, err := remote("send sync"); err != nil { app.ui.echoerrf("tag-toggle: %s", err) } } case "echo": app.ui.echo(strings.Join(e.args, " ")) case "echomsg": app.ui.echomsg(strings.Join(e.args, " ")) case "echoerr": app.ui.echoerr(strings.Join(e.args, " ")) case "cd": path := "~" if len(e.args) > 0 { path = e.args[0] } if err := cd(app, path); err != nil { app.ui.echoerrf("cd: %s", err) } case "select": if len(e.args) != 1 { app.ui.echoerr("select: requires an argument") return } path := replaceTilde(e.args[0]) lstat, err := os.Lstat(path) if err != nil { app.ui.echoerrf("select: %s", err) return } path, err = filepath.Abs(replaceTilde(e.args[0])) if err != nil { app.ui.echoerrf("select: %s", err) return } if err := cd(app, filepath.Dir(path)); err != nil { app.ui.echoerrf("select: %s", err) return } dir := app.nav.currDir() app.nav.checkDir(dir) if dir.loading { dir.files = append(dir.files, &file{FileInfo: lstat}) } dir.sel(filepath.Base(path), app.nav.height) app.ui.loadFile(app, true) case "source": if len(e.args) != 1 { app.ui.echoerr("source: requires an argument") return } app.readFile(replaceTilde(e.args[0])) case "push": if len(e.args) != 1 { app.ui.echoerr("push: requires an argument") return } log.Println("pushing keys", e.args[0]) for _, val := range splitKeys(e.args[0]) { app.ui.evChan <- parseKey(val) } case "addcustominfo": var k, v string switch len(e.args) { case 1: k, v = e.args[0], "" case 2: k, v = e.args[0], e.args[1] // don't trim to allow for custom alignment default: app.ui.echoerr("addcustominfo: requires either 1 or 2 arguments") return } path, err := filepath.Abs(replaceTilde(k)) if err != nil { app.ui.echoerrf("addcustominfo: %s", err) return } d := app.nav.getDir(filepath.Dir(path)) var f *file for _, file := range d.allFiles { if file.path == path { f = file break } } if f == nil { app.ui.echoerrf("addcustominfo: file not found: %s", path) return } if f.customInfo != v { f.customInfo = v // only sort when order changes if getSortBy(d.path) == customSort { d.sort() } } case "calcdirsize": err := app.nav.calcDirSize() if err != nil { app.ui.echoerrf("calcdirsize: %s", err) return } app.nav.sort() case "clearmaps": // leave `:` and cmaps bound so the user can still exit using `:quit` clear(gOpts.nkeys) clear(gOpts.vkeys) gOpts.nkeys[":"] = &callExpr{"read", nil, 1} gOpts.vkeys[":"] = &callExpr{"read", nil, 1} case "tty-write": if len(e.args) != 1 { app.ui.echoerr("tty-write: requires an argument") return } tty, ok := app.ui.screen.Tty() if !ok { log.Print("tty-write: failed to get tty") return } tty.Write([]byte(e.args[0])) case "visual": dir := app.nav.currDir() dir.visualAnchor = dir.ind dir.visualWrap = 0 case "visual-accept": dir := app.nav.currDir() for _, path := range dir.visualSelections() { if _, ok := app.nav.selections[path]; !ok { app.nav.selections[path] = app.nav.selectionInd app.nav.selectionInd++ } } // resetting Visual mode here instead of inside `normal()` // allows us to use Visual mode inside search, find etc. dir.visualAnchor = -1 normal(app) case "visual-unselect": dir := app.nav.currDir() for _, path := range dir.visualSelections() { delete(app.nav.selections, path) } if len(app.nav.selections) == 0 { app.nav.selectionInd = 0 } dir.visualAnchor = -1 normal(app) case "visual-discard": dir := app.nav.currDir() dir.visualAnchor = -1 normal(app) case "visual-change": if !app.nav.isVisualMode() { return } dir := app.nav.currDir() old := dir.ind beg := max(dir.ind-dir.pos, 0) dir.ind, dir.visualAnchor = dir.visualAnchor, dir.ind dir.pos = dir.ind - beg dir.visualWrap = -dir.visualWrap dir.boundPos(app.nav.height) if old != dir.ind { app.ui.loadFile(app, true) } case "cmd-insert": if len(e.args) == 0 { return } insert(app, e.args[0]) case "cmd-escape": if app.ui.cmdPrefix == ">" { return } normal(app) case "cmd-complete": app.doComplete() case "cmd-menu-complete": app.menuComplete(1) case "cmd-menu-complete-back": app.menuComplete(-1) case "cmd-menu-accept": exitCompMenu(app) case "cmd-menu-discard": if app.menuCompActive { app.ui.cmdAccLeft = strings.Join(app.menuCompTmp, " ") } exitCompMenu(app) case "cmd-enter": s := app.ui.cmdAccLeft + app.ui.cmdAccRight if len(s) == 0 && app.ui.cmdPrefix != "filter: " && app.ui.cmdPrefix != ">" { return } exitCompMenu(app) app.ui.cmdAccLeft = "" app.ui.cmdAccRight = "" switch app.ui.cmdPrefix { case ":": log.Printf("command: %s", s) app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" p := newParser(strings.NewReader(s)) for p.parse() { p.expr.eval(app, nil) } if p.err != nil { app.ui.echoerrf("%s", p.err) } case "$": log.Printf("shell: %s", s) app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" app.runShell(s, nil, "$") case "%": log.Printf("shell-pipe: %s", s) app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.runShell(s, nil, "%") case ">": io.WriteString(app.cmdIn, s+"\n") app.cmdOutBuf = nil case "!": log.Printf("shell-wait: %s", s) app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" app.runShell(s, nil, "!") case "&": log.Printf("shell-async: %s", s) app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) app.ui.cmdPrefix = "" app.runShell(s, nil, "&") case "/": dir := app.nav.currDir() old := dir.ind if gOpts.incsearch { dir.ind = app.nav.searchInd dir.pos = app.nav.searchPos } log.Printf("search: %s", s) app.ui.cmdPrefix = "" app.nav.search = s if _, err := app.nav.searchNext(); err != nil { app.ui.echoerrf("search: %s: %s", err, app.nav.search) } else if old != dir.ind { app.ui.loadFile(app, true) } case "?": dir := app.nav.currDir() old := dir.ind if gOpts.incsearch { dir.ind = app.nav.searchInd dir.pos = app.nav.searchPos } log.Printf("search-back: %s", s) app.ui.cmdPrefix = "" app.nav.search = s if _, err := app.nav.searchPrev(); err != nil { app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) } else if old != dir.ind { app.ui.loadFile(app, true) } case "filter: ": log.Printf("filter: %s", s) app.ui.cmdPrefix = "" if err := app.nav.setFilter(strings.Split(s, " ")); err != nil { app.ui.echoerrf("filter: %s", err) } app.ui.loadFile(app, true) case "find: ": app.ui.cmdPrefix = "" if moved, found := app.nav.findNext(); !found { app.ui.echoerrf("find: pattern not found: %s", app.nav.find) } else if moved { app.ui.loadFile(app, true) } case "find-back: ": app.ui.cmdPrefix = "" if moved, found := app.nav.findPrev(); !found { app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) } else if moved { app.ui.loadFile(app, true) } case "rename: ": app.ui.cmdPrefix = "" curr := app.nav.currFile() if curr == nil { app.ui.echoerr("rename: empty directory") return } wd, err := os.Getwd() if err != nil { log.Printf("getting current directory: %s", err) return } oldPath := filepath.Join(wd, curr.Name()) newPath := filepath.Clean(replaceTilde(s)) if !filepath.IsAbs(newPath) { newPath = filepath.Join(wd, newPath) } if oldPath == newPath { return } app.nav.renameOldPath = oldPath app.nav.renameNewPath = newPath newDir := filepath.Dir(newPath) if _, err := os.Stat(newDir); os.IsNotExist(err) { app.ui.cmdPrefix = "create '" + newDir + "'? [y/N] " return } oldStat, err := os.Lstat(oldPath) if err != nil { app.ui.echoerrf("rename: %s", err) return } if newStat, err := os.Lstat(newPath); !os.IsNotExist(err) && !os.SameFile(oldStat, newStat) { app.ui.cmdPrefix = "replace '" + newPath + "'? [y/N] " return } if err := app.nav.rename(); err != nil { app.ui.echoerrf("rename: %s", err) return } if gSingleMode { app.nav.renew() } else { if _, err := remote("send load"); err != nil { app.ui.echoerrf("rename: %s", err) return } } app.ui.loadFile(app, true) default: log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix) } case "cmd-interrupt": if app.cmd != nil { err := shellKill(app.cmd) if err != nil { app.ui.echoerrf("kill: %s", err) } else { app.ui.echoerr("process interrupt") } } normal(app) case "cmd-history-next": if !slices.Contains([]string{":", "$", "!", "%", "&"}, app.ui.cmdPrefix) { return } input := app.ui.cmdPrefix + app.ui.cmdAccLeft if app.cmdHistoryInput == nil { app.cmdHistoryInput = &input } for i := app.cmdHistoryInd - 1; i >= 0; i-- { if i == 0 { if *app.cmdHistoryInput == "" { normal(app) } else { exitCompMenu(app) app.ui.cmdAccLeft = "" app.cmdHistoryInd = 0 } break } cmd := app.cmdHistory[len(app.cmdHistory)-i] if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input { exitCompMenu(app) app.ui.cmdPrefix = cmd[:1] app.ui.cmdAccLeft = cmd[1:] app.cmdHistoryInd = i break } } case "cmd-history-prev": if !slices.Contains([]string{":", "$", "!", "%", "&", ""}, app.ui.cmdPrefix) { return } input := app.ui.cmdPrefix + app.ui.cmdAccLeft if app.cmdHistoryInput == nil { app.cmdHistoryInput = &input } for i := app.cmdHistoryInd + 1; i <= len(app.cmdHistory); i++ { cmd := app.cmdHistory[len(app.cmdHistory)-i] if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input { exitCompMenu(app) app.ui.cmdPrefix = cmd[:1] app.ui.cmdAccLeft = cmd[1:] app.cmdHistoryInd = i break } } case "cmd-left": if app.ui.cmdAccLeft == "" { return } last := lastGraphemeCluster(app.ui.cmdAccLeft) app.ui.cmdAccLeft = strings.TrimSuffix(app.ui.cmdAccLeft, last) app.ui.cmdAccRight = last + app.ui.cmdAccRight case "cmd-right": if app.ui.cmdAccRight == "" { return } first := firstGraphemeCluster(app.ui.cmdAccRight) app.ui.cmdAccLeft += first app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) case "cmd-home": app.ui.cmdAccRight = app.ui.cmdAccLeft + app.ui.cmdAccRight app.ui.cmdAccLeft = "" case "cmd-end": app.ui.cmdAccLeft += app.ui.cmdAccRight app.ui.cmdAccRight = "" case "cmd-delete": if app.ui.cmdAccRight == "" { return } first := firstGraphemeCluster(app.ui.cmdAccRight) app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) update(app) case "cmd-delete-back": if app.ui.cmdAccLeft == "" { switch app.ui.cmdPrefix { case "!", "$", "%", "&": app.ui.cmdPrefix = ":" app.cmdHistoryInd = 0 app.cmdHistoryInput = nil case ">", "rename: ", "filter: ": // Don't mess with programs waiting for input. // Exiting on backspace is also inconvenient for 'rename' and 'filter', // since the text field can start out nonempty. default: normal(app) } return } last := lastGraphemeCluster(app.ui.cmdAccLeft) app.ui.cmdAccLeft = strings.TrimSuffix(app.ui.cmdAccLeft, last) update(app) case "cmd-delete-home": if app.ui.cmdAccLeft == "" { return } app.ui.cmdYankBuf = app.ui.cmdAccLeft app.ui.cmdAccLeft = "" update(app) case "cmd-delete-end": if app.ui.cmdAccRight == "" { return } app.ui.cmdYankBuf = app.ui.cmdAccRight app.ui.cmdAccRight = "" update(app) case "cmd-delete-unix-word": ind := strings.LastIndex(strings.TrimRight(app.ui.cmdAccLeft, " "), " ") + 1 app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:] app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] update(app) case "cmd-yank": app.ui.cmdAccLeft += app.ui.cmdYankBuf update(app) case "cmd-transpose": var c []string gr := uniseg.NewGraphemes(app.ui.cmdAccLeft) for gr.Next() { c = append(c, gr.Str()) } first := firstGraphemeCluster(app.ui.cmdAccRight) if first != "" { c = append(c, first) } if len(c) < 2 { return } app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) c[len(c)-1], c[len(c)-2] = c[len(c)-2], c[len(c)-1] app.ui.cmdAccLeft = strings.Join(c, "") update(app) case "cmd-transpose-word": if app.ui.cmdAccLeft == "" { return } locs := reWord.FindAllStringIndex(app.ui.cmdAccLeft, -1) if len(locs) < 2 { return } if app.ui.cmdAccRight != "" { loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) if loc != nil { ind := loc[3] app.ui.cmdAccLeft += app.ui.cmdAccRight[:ind] app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] } } locs = reWord.FindAllStringIndex(app.ui.cmdAccLeft, -1) beg1, end1 := locs[len(locs)-2][0], locs[len(locs)-2][1] beg2, end2 := locs[len(locs)-1][0], locs[len(locs)-1][1] app.ui.cmdAccLeft = app.ui.cmdAccLeft[:beg1] + app.ui.cmdAccLeft[beg2:end2] + app.ui.cmdAccLeft[end1:beg2] + app.ui.cmdAccLeft[beg1:end1] + app.ui.cmdAccLeft[end2:] update(app) case "cmd-word": if app.ui.cmdAccRight == "" { return } loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) if loc == nil { return } ind := loc[3] app.ui.cmdAccLeft += app.ui.cmdAccRight[:ind] app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] case "cmd-word-back": if app.ui.cmdAccLeft == "" { return } locs := reWordBeg.FindAllStringSubmatchIndex(app.ui.cmdAccLeft, -1) if locs == nil { return } ind := locs[len(locs)-1][3] app.ui.cmdAccRight = app.ui.cmdAccLeft[ind:] + app.ui.cmdAccRight app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] case "cmd-delete-word": if app.ui.cmdAccRight == "" { return } loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) if loc == nil { return } ind := loc[3] app.ui.cmdYankBuf = app.ui.cmdAccRight[:ind] app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] update(app) case "cmd-delete-word-back": if app.ui.cmdAccLeft == "" { return } locs := reWordBeg.FindAllStringSubmatchIndex(app.ui.cmdAccLeft, -1) if locs == nil { return } ind := locs[len(locs)-1][3] app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:] app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] update(app) case "cmd-capitalize-word": if app.ui.cmdAccRight == "" { return } loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) if loc == nil { return } ind := loc[3] capitalize := func(s string) string { runes := []rune(s) for i, r := range runes { if !unicode.IsSpace(r) { runes[i] = unicode.ToUpper(r) break } } return string(runes) } app.ui.cmdAccLeft += capitalize(app.ui.cmdAccRight[:ind]) app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] update(app) case "cmd-uppercase-word": if app.ui.cmdAccRight == "" { return } loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) if loc == nil { return } ind := loc[3] app.ui.cmdAccLeft += strings.ToUpper(app.ui.cmdAccRight[:ind]) app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] update(app) case "cmd-lowercase-word": if app.ui.cmdAccRight == "" { return } loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) if loc == nil { return } ind := loc[3] app.ui.cmdAccLeft += strings.ToLower(app.ui.cmdAccRight[:ind]) app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] update(app) case "on-focus-gained": onFocusGained(app) case "on-focus-lost": onFocusLost(app) case "on-init": onInit(app) default: cmd, ok := gOpts.cmds[e.name] if !ok { app.ui.echoerrf("command not found: %s", e.name) return } cmd.eval(app, e.args) } } func (e *execExpr) eval(app *app, args []string) { switch e.prefix { case "$": log.Printf("shell: %s -- %s", e, args) app.runShell(e.value, args, e.prefix) case "%": log.Printf("shell-pipe: %s -- %s", e, args) app.runShell(e.value, args, e.prefix) case "!": log.Printf("shell-wait: %s -- %s", e, args) app.runShell(e.value, args, e.prefix) case "&": log.Printf("shell-async: %s -- %s", e, args) app.runShell(e.value, args, e.prefix) default: log.Printf("evaluating unknown execution prefix: %q", e.prefix) } } func (e *listExpr) eval(app *app, _ []string) { for range e.count { for _, expr := range e.exprs { expr.eval(app, nil) } } } ================================================ FILE: eval_test.go ================================================ package main import ( "fmt" "reflect" "strings" "testing" ) var gEvalTests = []struct { inp string toks []string exprs []expr }{ { "", []string{}, nil, }, { "# comments start with '#'", []string{}, nil, }, { "echo hello", []string{"echo", "hello", "\n"}, []expr{&callExpr{"echo", []string{"hello"}, 1}}, }, { "echo hello world", []string{"echo", "hello", "world", "\n"}, []expr{&callExpr{"echo", []string{"hello", "world"}, 1}}, }, { "echo 'hello world'", []string{"echo", "hello world", "\n"}, []expr{&callExpr{"echo", []string{"hello world"}, 1}}, }, { `echo "hello world"`, []string{"echo", "hello world", "\n"}, []expr{&callExpr{"echo", []string{"hello world"}, 1}}, }, { `echo "hello\"world"`, []string{"echo", `hello"world`, "\n"}, []expr{&callExpr{"echo", []string{`hello"world`}, 1}}, }, { `echo "hello\tworld"`, []string{"echo", "hello\tworld", "\n"}, []expr{&callExpr{"echo", []string{"hello\tworld"}, 1}}, }, { `echo "hello\nworld"`, []string{"echo", "hello\nworld", "\n"}, []expr{&callExpr{"echo", []string{"hello\nworld"}, 1}}, }, { `echo "hello\zworld"`, []string{"echo", `hello\zworld`, "\n"}, []expr{&callExpr{"echo", []string{`hello\zworld`}, 1}}, }, { `echo "hello\0world"`, []string{"echo", "hello\000world", "\n"}, []expr{&callExpr{"echo", []string{"hello\000world"}, 1}}, }, { `echo "hello\101world"`, []string{"echo", "hello\101world", "\n"}, []expr{&callExpr{"echo", []string{"hello\101world"}, 1}}, }, { `echo hello\ world`, []string{"echo", "hello world", "\n"}, []expr{&callExpr{"echo", []string{"hello world"}, 1}}, }, { "echo hello\\\tworld", []string{"echo", "hello\tworld", "\n"}, []expr{&callExpr{"echo", []string{"hello\tworld"}, 1}}, }, { "echo hello\\\nworld", []string{"echo", "hello\nworld", "\n"}, []expr{&callExpr{"echo", []string{"hello\nworld"}, 1}}, }, { `echo hello\\world`, []string{"echo", `hello\world`, "\n"}, []expr{&callExpr{"echo", []string{`hello\world`}, 1}}, }, { `echo hello\zworld`, []string{"echo", "hellozworld", "\n"}, []expr{&callExpr{"echo", []string{"hellozworld"}, 1}}, }, { "set hidden # trailing comments are allowed", []string{"set", "hidden", "\n"}, []expr{&setExpr{"hidden", ""}}, }, { "set hidden; set preview", []string{"set", "hidden", ";", "set", "preview", "\n"}, []expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}}, }, { "set hidden\nset preview", []string{"set", "hidden", "\n", "set", "preview", "\n"}, []expr{&setExpr{"hidden", ""}, &setExpr{"preview", ""}}, }, { `set ifs ""`, []string{"set", "ifs", "", "\n"}, []expr{&setExpr{"ifs", ""}}, }, { `set ifs "\n"`, []string{"set", "ifs", "\n", "\n"}, []expr{&setExpr{"ifs", "\n"}}, }, { "set ratios 1:2:3", []string{"set", "ratios", "1:2:3", "\n"}, []expr{&setExpr{"ratios", "1:2:3"}}, }, { "set ratios 1:2:3;", []string{"set", "ratios", "1:2:3", ";"}, []expr{&setExpr{"ratios", "1:2:3"}}, }, { ":set ratios 1:2:3", []string{":", "set", "ratios", "1:2:3", "\n", "\n"}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}}, }, { ":set ratios 1:2:3\nset hidden", []string{":", "set", "ratios", "1:2:3", "\n", "\n", "set", "hidden", "\n"}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}, &setExpr{"hidden", ""}}, }, { ":set ratios 1:2:3;", []string{":", "set", "ratios", "1:2:3", ";", "\n"}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}}, }, { ":set ratios 1:2:3;\nset hidden", []string{":", "set", "ratios", "1:2:3", ";", "\n", "set", "hidden", "\n"}, []expr{&listExpr{[]expr{&setExpr{"ratios", "1:2:3"}}, 1}, &setExpr{"hidden", ""}}, }, { "set ratios 1:2:3\n set hidden", []string{"set", "ratios", "1:2:3", "\n", "set", "hidden", "\n"}, []expr{&setExpr{"ratios", "1:2:3"}, &setExpr{"hidden", ""}}, }, { "set ratios 1:2:3 \nset hidden", []string{"set", "ratios", "1:2:3", "\n", "set", "hidden", "\n"}, []expr{&setExpr{"ratios", "1:2:3"}, &setExpr{"hidden", ""}}, }, { "setlocal /foo/bar hidden # trailing comments are allowed", []string{"setlocal", "/foo/bar", "hidden", "\n"}, []expr{&setLocalExpr{"/foo/bar", "hidden", ""}}, }, { "setlocal /foo/bar hidden; setlocal /foo/bar reverse", []string{"setlocal", "/foo/bar", "hidden", ";", "setlocal", "/foo/bar", "reverse", "\n"}, []expr{&setLocalExpr{"/foo/bar", "hidden", ""}, &setLocalExpr{"/foo/bar", "reverse", ""}}, }, { "setlocal /foo/bar hidden\nsetlocal /foo/bar reverse", []string{"setlocal", "/foo/bar", "hidden", "\n", "setlocal", "/foo/bar", "reverse", "\n"}, []expr{&setLocalExpr{"/foo/bar", "hidden", ""}, &setLocalExpr{"/foo/bar", "reverse", ""}}, }, { `setlocal /foo/bar info ""`, []string{"setlocal", "/foo/bar", "info", "", "\n"}, []expr{&setLocalExpr{"/foo/bar", "info", ""}}, }, { `setlocal /foo/bar info "size"`, []string{"setlocal", "/foo/bar", "info", "size", "\n"}, []expr{&setLocalExpr{"/foo/bar", "info", "size"}}, }, { "setlocal /foo/bar info size:time", []string{"setlocal", "/foo/bar", "info", "size:time", "\n"}, []expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, }, { "setlocal /foo/bar info size:time;", []string{"setlocal", "/foo/bar", "info", "size:time", ";"}, []expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, }, { ":setlocal /foo/bar info size:time", []string{":", "setlocal", "/foo/bar", "info", "size:time", "\n", "\n"}, []expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}}, }, { ":setlocal /foo/bar info size:time\nsetlocal /foo/bar hidden", []string{":", "setlocal", "/foo/bar", "info", "size:time", "\n", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, []expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}, &setLocalExpr{"/foo/bar", "hidden", ""}}, }, { ":setlocal /foo/bar info size:time;", []string{":", "setlocal", "/foo/bar", "info", "size:time", ";", "\n"}, []expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}}, }, { ":setlocal /foo/bar info size:time;\nsetlocal /foo/bar hidden", []string{":", "setlocal", "/foo/bar", "info", "size:time", ";", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, []expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}, &setLocalExpr{"/foo/bar", "hidden", ""}}, }, { "setlocal /foo/bar info size:time\n setlocal /foo/bar hidden", []string{"setlocal", "/foo/bar", "info", "size:time", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, []expr{&setLocalExpr{"/foo/bar", "info", "size:time"}, &setLocalExpr{"/foo/bar", "hidden", ""}}, }, { "setlocal /foo/bar info size:time \nsetlocal /foo/bar hidden", []string{"setlocal", "/foo/bar", "info", "size:time", "\n", "setlocal", "/foo/bar", "hidden", "\n"}, []expr{&setLocalExpr{"/foo/bar", "info", "size:time"}, &setLocalExpr{"/foo/bar", "hidden", ""}}, }, { "map gh cd ~", []string{"map", "gh", "cd", "~", "\n"}, []expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}, 1}}}, }, { "map gh cd ~;", []string{"map", "gh", "cd", "~", ";"}, []expr{&mapExpr{"gh", &callExpr{"cd", []string{"~"}, 1}}}, }, { "map gh :cd ~", []string{"map", "gh", ":", "cd", "~", "\n", "\n"}, []expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}, 1}}, 1}}}, }, { "map gh :cd ~;", []string{"map", "gh", ":", "cd", "~", ";", "\n"}, []expr{&mapExpr{"gh", &listExpr{[]expr{&callExpr{"cd", []string{"~"}, 1}}, 1}}}, }, { "nmap :toggle; down", []string{"nmap", "", ":", "toggle", ";", "down", "\n", "\n"}, []expr{&nmapExpr{"", &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1}}}, }, { "vmap visual-accept", []string{"vmap", "", "visual-accept", "\n"}, []expr{&vmapExpr{"", &callExpr{"visual-accept", nil, 1}}}, }, { "cmap cmd-escape", []string{"cmap", "", "cmd-escape", "\n"}, []expr{&cmapExpr{"", &callExpr{"cmd-escape", nil, 1}}}, }, { "cmd usage $du -h . | less", []string{"cmd", "usage", "$", "du -h . | less", "\n"}, []expr{&cmdExpr{"usage", &execExpr{"$", "du -h . | less"}}}, }, { "cmd 世界 $echo 世界", []string{"cmd", "世界", "$", "echo 世界", "\n"}, []expr{&cmdExpr{"世界", &execExpr{"$", "echo 世界"}}}, }, { "map u usage", []string{"map", "u", "usage", "\n"}, []expr{&mapExpr{"u", &callExpr{"usage", nil, 1}}}, }, { "map u usage;", []string{"map", "u", "usage", ";"}, []expr{&mapExpr{"u", &callExpr{"usage", nil, 1}}}, }, { "map u :usage", []string{"map", "u", ":", "usage", "\n", "\n"}, []expr{&mapExpr{"u", &listExpr{[]expr{&callExpr{"usage", nil, 1}}, 1}}}, }, { "map u :usage;", []string{"map", "u", ":", "usage", ";", "\n"}, []expr{&mapExpr{"u", &listExpr{[]expr{&callExpr{"usage", nil, 1}}, 1}}}, }, { "map r push :rename", []string{"map", "r", "push", ":rename", "\n"}, []expr{&mapExpr{"r", &callExpr{"push", []string{":rename"}, 1}}}, }, { "map r push :rename;", []string{"map", "r", "push", ":rename;", "\n"}, []expr{&mapExpr{"r", &callExpr{"push", []string{":rename;"}, 1}}}, }, { "map r push :rename # trailing comments are allowed after a space", []string{"map", "r", "push", ":rename", "\n"}, []expr{&mapExpr{"r", &callExpr{"push", []string{":rename"}, 1}}}, }, { "map r :push :rename", []string{"map", "r", ":", "push", ":rename", "\n", "\n"}, []expr{&mapExpr{"r", &listExpr{[]expr{&callExpr{"push", []string{":rename"}, 1}}, 1}}}, }, { "map r :push :rename ; set hidden", []string{"map", "r", ":", "push", ":rename", ";", "set", "hidden", "\n", "\n"}, []expr{&mapExpr{"r", &listExpr{[]expr{&callExpr{"push", []string{":rename"}, 1}, &setExpr{"hidden", ""}}, 1}}}, }, { "map u $du -h . | less", []string{"map", "u", "$", "du -h . | less", "\n"}, []expr{&mapExpr{"u", &execExpr{"$", "du -h . | less"}}}, }, { "cmd usage $du -h $1 | less", []string{"cmd", "usage", "$", "du -h $1 | less", "\n"}, []expr{&cmdExpr{"usage", &execExpr{"$", "du -h $1 | less"}}}, }, { "map u usage /", []string{"map", "u", "usage", "/", "\n"}, []expr{&mapExpr{"u", &callExpr{"usage", []string{"/"}, 1}}}, }, { "map ss :set sortby size; set info size", []string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "info", "size", "\n", "\n"}, []expr{&mapExpr{"ss", &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1}}}, }, { "map ss :set sortby size; set info size;", []string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "info", "size", ";", "\n"}, []expr{&mapExpr{"ss", &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1}}}, }, { `cmd gohome :{{ cd ~ set hidden }}`, []string{ "cmd", "gohome", ":", "{{", "cd", "~", "\n", "set", "hidden", "\n", "}}", "\n", }, []expr{&cmdExpr{ "gohome", &listExpr{[]expr{ &callExpr{"cd", []string{"~"}, 1}, &setExpr{"hidden", ""}, }, 1}, }}, }, { `map gh :{{ cd ~ set hidden }}`, []string{ "map", "gh", ":", "{{", "cd", "~", "\n", "set", "hidden", "\n", "}}", "\n", }, []expr{&mapExpr{ "gh", &listExpr{[]expr{ &callExpr{"cd", []string{"~"}, 1}, &setExpr{"hidden", ""}, }, 1}, }}, }, { `map c ${{ mkdir foo cp $fs foo tar -czvf foo.tar.gz foo rm -rf foo }}`, []string{"map", "c", "$", "{{", ` mkdir foo cp $fs foo tar -czvf foo.tar.gz foo rm -rf foo `, "}}", "\n"}, []expr{&mapExpr{"c", &execExpr{"$", ` mkdir foo cp $fs foo tar -czvf foo.tar.gz foo rm -rf foo `}}}, }, { `cmd compress ${{ mkdir $1 cp $fs $1 tar -czvf $1.tar.gz $1 rm -rf $1 }}`, []string{"cmd", "compress", "$", "{{", ` mkdir $1 cp $fs $1 tar -czvf $1.tar.gz $1 rm -rf $1 `, "}}", "\n"}, []expr{&cmdExpr{"compress", &execExpr{"$", ` mkdir $1 cp $fs $1 tar -czvf $1.tar.gz $1 rm -rf $1 `}}}, }, } func TestScan(t *testing.T) { for _, test := range gEvalTests { s := newScanner(strings.NewReader(test.inp)) for _, tok := range test.toks { if s.scan(); s.tok != tok { t.Errorf("at input '%s' expected '%s' but scanned '%s'", test.inp, tok, s.tok) } } if s.scan() { t.Errorf("at input '%s' unexpected '%s'", test.inp, s.tok) } } } func TestParse(t *testing.T) { for _, test := range gEvalTests { p := newParser(strings.NewReader(test.inp)) for _, expr := range test.exprs { if p.parse(); !reflect.DeepEqual(p.expr, expr) { t.Errorf("at input '%s' expected '%s' but parsed '%s'", test.inp, expr, p.expr) } } if p.parse(); p.expr != nil { t.Errorf("at input '%s' unexpected '%s'", test.inp, p.expr) } } } func TestExprString(t *testing.T) { tests := []struct { e expr exp string }{ {&setExpr{"hidden!", ""}, "set hidden!"}, {&setExpr{"hidden", "true"}, "set hidden true"}, {&setLocalExpr{"~", "hidden!", ""}, "setlocal ~ hidden!"}, {&setLocalExpr{"~", "hidden", "true"}, "setlocal ~ hidden true"}, {&mapExpr{"q", nil}, "map q"}, {&mapExpr{"q", &callExpr{"quit", nil, 1}}, "map q quit"}, {&nmapExpr{"q", nil}, "nmap q"}, {&nmapExpr{"q", &callExpr{"quit", nil, 1}}, "nmap q quit"}, {&vmapExpr{"q", nil}, "vmap q"}, {&vmapExpr{"q", &callExpr{"quit", nil, 1}}, "vmap q quit"}, {&cmapExpr{"q", nil}, "cmap q"}, {&cmapExpr{"q", &callExpr{"quit", nil, 1}}, "cmap q quit"}, {&cmdExpr{"foo", nil}, "cmd foo"}, {&cmdExpr{"foo", &callExpr{"quit", nil, 1}}, "cmd foo quit"}, {&callExpr{"quit", nil, 1}, "quit"}, {&callExpr{"cd", []string{"~"}, 1}, "cd ~"}, {&execExpr{"$", "du -h . | less"}, "${{ du -h . | less }}"}, { &execExpr{"$", ` mkdir foo cp $fs foo tar -czvf foo.tar.gz foo rm -rf foo `}, "${{ mkdir foo ... }}", }, {&listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1}, ":{{ toggle; down; }}"}, } for _, test := range tests { if got := test.e.String(); got != test.exp { t.Errorf("at test '%#v' expected '%s' but got '%s'", test.e, test.exp, got) } } } func TestSplitKeys(t *testing.T) { inps := []struct { s string keys []string }{ {"", nil}, {"j", []string{"j"}}, {"jk", []string{"j", "k"}}, {"1j", []string{"1", "j"}}, {"42j", []string{"4", "2", "j"}}, {"", []string{""}}, {"j", []string{"j", ""}}, {"jk", []string{"j", "", "k"}}, {"1jk", []string{"1", "j", "", "k"}}, {"1j1k", []string{"1", "j", "", "1", "k"}}, {"<>", []string{"<>"}}, {"", []string{""}}, {">", []string{">", ""}}, {">>", []string{">", "", ">"}}, } for _, inp := range inps { if keys := splitKeys(inp.s); !reflect.DeepEqual(keys, inp.keys) { t.Errorf("at input '%s' expected '%v' but got '%v'", inp.s, inp.keys, keys) } } } func TestApplyBoolOpt(t *testing.T) { tests := []struct { opt bool e setExpr exp bool }{ {true, setExpr{"feature", ""}, true}, {true, setExpr{"feature", "true"}, true}, {true, setExpr{"feature", "false"}, false}, {false, setExpr{"feature", ""}, true}, {false, setExpr{"feature", "true"}, true}, {false, setExpr{"feature", "false"}, false}, {true, setExpr{"nofeature", ""}, false}, {false, setExpr{"nofeature", ""}, false}, {true, setExpr{"feature!", ""}, false}, {false, setExpr{"feature!", ""}, true}, } for _, test := range tests { testStr := fmt.Sprintf("%v", test) if err := applyBoolOpt(&test.opt, &test.e); err != nil { t.Errorf("at test '%s' expected '%t' but got an error '%s'", testStr, test.exp, err) continue } if test.opt != test.exp { t.Errorf("at test '%s' expected '%t' but got '%t'", testStr, test.exp, test.opt) } } } func TestApplyLocalBoolOpt(t *testing.T) { tests := []struct { localOpt map[string]bool globalOpt bool e setLocalExpr exp bool }{ {map[string]bool{}, false, setLocalExpr{"/", "feature", ""}, true}, {map[string]bool{}, false, setLocalExpr{"/", "feature", "true"}, true}, {map[string]bool{}, false, setLocalExpr{"/", "feature", "false"}, false}, {map[string]bool{}, false, setLocalExpr{"/", "nofeature", ""}, false}, {map[string]bool{}, true, setLocalExpr{"/", "feature!", ""}, false}, {map[string]bool{}, false, setLocalExpr{"/", "feature!", ""}, true}, {map[string]bool{"/": true}, false, setLocalExpr{"/", "feature", ""}, true}, {map[string]bool{"/": true}, false, setLocalExpr{"/", "feature", "true"}, true}, {map[string]bool{"/": true}, false, setLocalExpr{"/", "feature", "false"}, false}, {map[string]bool{"/": true}, false, setLocalExpr{"/", "nofeature", ""}, false}, {map[string]bool{"/": true}, true, setLocalExpr{"/", "feature!", ""}, false}, {map[string]bool{"/": true}, false, setLocalExpr{"/", "feature!", ""}, false}, {map[string]bool{"/": false}, false, setLocalExpr{"/", "feature", ""}, true}, {map[string]bool{"/": false}, false, setLocalExpr{"/", "feature", "true"}, true}, {map[string]bool{"/": false}, false, setLocalExpr{"/", "feature", "false"}, false}, {map[string]bool{"/": false}, false, setLocalExpr{"/", "nofeature", ""}, false}, {map[string]bool{"/": false}, true, setLocalExpr{"/", "feature!", ""}, true}, {map[string]bool{"/": false}, false, setLocalExpr{"/", "feature!", ""}, true}, } for _, test := range tests { testStr := fmt.Sprintf("%v", test) if err := applyLocalBoolOpt(test.localOpt, test.globalOpt, &test.e); err != nil { t.Errorf("at test '%s' expected '%t' but got an error '%s'", testStr, test.exp, err) continue } result := test.localOpt[test.e.path] if result != test.exp { t.Errorf("at test '%s' expected '%t' but got '%t'", testStr, test.exp, result) } } } ================================================ FILE: gen/build.sh ================================================ #!/bin/sh # Builds a static stripped binary with version information. # # This script is used to build a binary for the current platform. Cgo is # disabled to make sure the binary is statically linked. Appropriate flags are # given to the go compiler to strip the binary. Current git tag is passed to # the compiler by default to be used as the version in the binary. set -o errexit -o nounset [ -z "${version:-}" ] && version=$(git describe --tags --abbrev=0) CGO_ENABLED=0 go build -ldflags="-s -w -X main.gVersion=$version" "$@" # vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80 ================================================ FILE: gen/deflist.lua ================================================ local stringify = pandoc.utils.stringify local List = pandoc.List local DefinitionList = pandoc.DefinitionList -- We use bold paragraphs for flags (e.g. **-help**) local function is_term(b) return b.tag == "Para" and b.content[1] and b.content[1].tag == "Strong" end -- GitHub-flavored markdown lacks native definition lists (i.e. ":" syntax). -- Therefore, we generate them on the fly to get a better-looking "OPTIONS" -- section in man pages and plain-text help without changing the source file. function Blocks(blocks) local out = List() local i = 1 local in_opts, lvl = false, nil while blocks[i] do local b = blocks[i] i = i + 1 if b.tag == "Header" then -- Apply definition lists inside "OPTIONS" only if stringify(b.content) == "OPTIONS" then in_opts, lvl = true, b.level else in_opts = in_opts and b.level > lvl end out:insert(b) elseif in_opts and is_term(b) then -- Collect definition for current term local def = List() while blocks[i] and blocks[i].tag ~= "Header" and not is_term(blocks[i]) do def:insert(blocks[i]) i = i + 1 end out:insert(DefinitionList({ { b.content, { def } } })) else -- Pass through unchanged out:insert(b) end end return out end ================================================ FILE: gen/doc.sh ================================================ #!/bin/sh # Generates `lf.1` and `doc.txt` from the `doc.md` file. # # This script is used to generate a man page and a plain text conversion of the # markdown documentation using pandoc (https://pandoc.org/). GitHub Flavored # Markdown (GFM) (https://github.github.com/gfm/) is used for the markdown # input format. The markdown file is automatically rendered in the GitHub # repository (https://github.com/gokcehan/lf/blob/master/doc.md). The man page # file `lf.1` is meant to be used for installations on Unix systems. The plain # text file `doc.txt` is embedded in the binary to be displayed on request with # the `-doc` command line flag. Thus the same documentation is used for online # and terminal display. set -o errexit -o nounset get_version() { printf "r%s" $(($(git describe --tags --abbrev=0 | tr -d r) + 1)) } [ -z "${version:-}" ] && version=$(get_version) [ -z "${date:-}" ] && date=$(date +%F) PANDOC_IMAGE=pandoc/minimal:3.7 generate_man_page() { "${OCI_PROGRAM?}" run \ --rm \ --volume "$PWD:/data" \ "$@" "$PANDOC_IMAGE" \ --standalone \ --lua-filter /data/gen/deflist.lua \ --from gfm --to man \ --metadata=title:"LF" \ --metadata=section:"1" \ --metadata=date:"$date" \ --metadata=footer:"$version" \ --metadata=header:"DOCUMENTATION" \ doc.md -o lf.1 } generate_plain_text() { "${OCI_PROGRAM?}" run \ --rm \ --volume "$PWD:/data" \ "$@" "$PANDOC_IMAGE" \ --standalone \ --lua-filter /data/gen/deflist.lua \ --from gfm --to plain \ doc.md -o doc.txt } is_rootless() { case "$OCI_PROGRAM" in podman) podman info -f '{{.Host.Security.Rootless}}' | grep -q true ;; docker) docker info -f '{{.SecurityOptions}}' | grep -q rootless ;; *) echo >&2 \ "Unknown OCI program \"$OCI_PROGRAM\", assuming rootless mode" ;; esac } # You can set your own OCI_PROGRAM, which assumes it runs in rootless mode. if [ -z "${OCI_PROGRAM:-}" ]; then if command -v podman > /dev/null; then OCI_PROGRAM=podman elif command -v docker > /dev/null; then OCI_PROGRAM=docker fi fi if is_rootless; then generate_man_page else generate_man_page --user "$(id -u):$(id -g)" fi if is_rootless; then generate_plain_text else generate_plain_text --user "$(id -u):$(id -g)" fi # vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80 ================================================ FILE: gen/package.sh ================================================ #!/bin/sh # Compresses a binary into an archived form. # # This script is used to compress a binary built from `build.sh` into an # archive. `.zip` is used for Windows and `.tar.gz` otherwise. The archive is # placed inside a directory named `dist`. set -o errexit -o nounset mkdir -p dist if [ "$GOOS" = "windows" ]; then zip "dist/lf-${GOOS}-${GOARCH}.zip" lf.exe else tar czf "dist/lf-${GOOS}-${GOARCH}.tar.gz" lf fi # vim: tabstop=4 shiftwidth=4 textwidth=80 colorcolumn=80 ================================================ FILE: go.mod ================================================ module github.com/gokcehan/lf go 1.25.0 require ( github.com/djherbis/times v1.6.0 github.com/fsnotify/fsnotify v1.9.0 github.com/gdamore/tcell/v3 v3.1.2 github.com/rivo/uniseg v0.4.7 golang.org/x/sys v0.42.0 golang.org/x/term v0.41.0 ) require ( github.com/gdamore/encoding v1.0.1 // indirect github.com/lucasb-eyer/go-colorful v1.3.0 // indirect golang.org/x/text v0.34.0 // indirect ) ================================================ FILE: go.sum ================================================ github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c= github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0= github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw= github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo= github.com/gdamore/tcell/v3 v3.1.2 h1:qEaXnDaYZpCIMDfa3XFHkxrwFBINUuDiePwj39vErZ8= github.com/gdamore/tcell/v3 v3.1.2/go.mod h1:MikpZpivMtggrw1kL999dI2VuXw6Wya4724VAh3DzIg= github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU= golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= ================================================ FILE: icons.go ================================================ package main import ( "log" "os" "path/filepath" "strings" "github.com/gdamore/tcell/v3" ) type iconDef struct { icon string hasStyle bool style tcell.Style } type iconMap struct { icons map[string]iconDef useLinkTarget bool } func iconWithoutStyle(icon string) iconDef { return iconDef{icon, false, tcell.StyleDefault} } func iconWithStyle(icon string, style tcell.Style) iconDef { return iconDef{icon, true, style} } func parseIcons() iconMap { im := iconMap{ icons: make(map[string]iconDef), useLinkTarget: false, } defaultIcons := []string{ "ln=l", "or=l", "tw=t", "ow=d", "st=t", "di=d", "pi=p", "so=s", "bd=b", "cd=c", "su=u", "sg=g", "ex=x", "fi=-", } im.parseEnv(strings.Join(defaultIcons, ":")) if env := os.Getenv("LF_ICONS"); env != "" { im.parseEnv(env) } for _, path := range gIconsPaths { if _, err := os.Stat(path); !os.IsNotExist(err) { im.parseFile(path) } } return im } func (im *iconMap) parseFile(path string) { log.Printf("reading file: %s", path) f, err := os.Open(path) if err != nil { log.Printf("opening icons file: %s", err) return } defer f.Close() arrs, err := readArrays(f, 1, 3) if err != nil { log.Printf("reading icons file: %s", err) return } for _, arr := range arrs { im.parseArray(arr) } } func (im *iconMap) parseEnv(env string) { for entry := range strings.SplitSeq(env, ":") { if entry == "" { continue } pair := strings.Split(entry, "=") if len(pair) != 2 { log.Printf("invalid $LF_ICONS entry: %s", entry) return } im.parseArray(pair) } } func (im *iconMap) parseArray(arr []string) { key := replaceTilde(arr[0]) if filepath.IsAbs(key) { key = filepath.Clean(key) } switch len(arr) { case 1: delete(im.icons, key) case 2: icon := arr[1] if key == "ln" && icon == "target" { im.useLinkTarget = true } else { im.icons[key] = iconWithoutStyle(icon) } case 3: icon, color := arr[1], arr[2] im.icons[key] = iconWithStyle(icon, applySGR(color, tcell.StyleDefault)) } } func (im iconMap) get(f *file) iconDef { if val, ok := im.icons[f.path]; ok { return val } if f.IsDir() { if val, ok := im.icons[f.Name()+"/"]; ok { return val } } var key string switch { case f.linkState == working && !im.useLinkTarget: key = "ln" case f.linkState == broken: key = "or" case f.IsDir() && f.Mode()&os.ModeSticky != 0 && f.Mode()&0o002 != 0: key = "tw" case f.IsDir() && f.Mode()&0o002 != 0: key = "ow" case f.IsDir() && f.Mode()&os.ModeSticky != 0: key = "st" case f.IsDir(): key = "di" case f.Mode()&os.ModeNamedPipe != 0: key = "pi" case f.Mode()&os.ModeSocket != 0: key = "so" case f.Mode()&os.ModeCharDevice != 0: key = "cd" case f.Mode()&os.ModeDevice != 0: key = "bd" case f.Mode()&os.ModeSetuid != 0: key = "su" case f.Mode()&os.ModeSetgid != 0: key = "sg" case isExecutable(f.FileInfo): key = "ex" } if val, ok := im.icons[key]; ok { return val } if val, ok := im.icons[f.Name()+"*"]; ok { return val } if val, ok := im.icons["*"+f.Name()]; ok { return val } if val, ok := im.icons[filepath.Base(f.Name())+".*"]; ok { return val } if val, ok := im.icons["*"+strings.ToLower(f.ext)]; ok { return val } if val, ok := im.icons["fi"]; ok { return val } return iconWithoutStyle(" ") } ================================================ FILE: key.go ================================================ package main import ( "fmt" "regexp" "strings" "github.com/gdamore/tcell/v3" ) var gKeyVal = map[tcell.Key]string{ tcell.KeyEnter: "", tcell.KeyBackspace: "", tcell.KeyTab: "", tcell.KeyBacktab: "", tcell.KeyEsc: "", tcell.KeyDelete: "", tcell.KeyInsert: "", tcell.KeyUp: "", tcell.KeyDown: "", tcell.KeyLeft: "", tcell.KeyRight: "", tcell.KeyHome: "", tcell.KeyEnd: "", tcell.KeyUpLeft: "", tcell.KeyUpRight: "", tcell.KeyDownLeft: "", tcell.KeyDownRight: "", tcell.KeyCenter: "
", tcell.KeyPgDn: "", tcell.KeyPgUp: "", tcell.KeyClear: "", tcell.KeyExit: "", tcell.KeyCancel: "", tcell.KeyPause: "", tcell.KeyPrint: "", tcell.KeyF1: "", tcell.KeyF2: "", tcell.KeyF3: "", tcell.KeyF4: "", tcell.KeyF5: "", tcell.KeyF6: "", tcell.KeyF7: "", tcell.KeyF8: "", tcell.KeyF9: "", tcell.KeyF10: "", tcell.KeyF11: "", tcell.KeyF12: "", tcell.KeyF13: "", tcell.KeyF14: "", tcell.KeyF15: "", tcell.KeyF16: "", tcell.KeyF17: "", tcell.KeyF18: "", tcell.KeyF19: "", tcell.KeyF20: "", tcell.KeyF21: "", tcell.KeyF22: "", tcell.KeyF23: "", tcell.KeyF24: "", tcell.KeyF25: "", tcell.KeyF26: "", tcell.KeyF27: "", tcell.KeyF28: "", tcell.KeyF29: "", tcell.KeyF30: "", tcell.KeyF31: "", tcell.KeyF32: "", tcell.KeyF33: "", tcell.KeyF34: "", tcell.KeyF35: "", tcell.KeyF36: "", tcell.KeyF37: "", tcell.KeyF38: "", tcell.KeyF39: "", tcell.KeyF40: "", tcell.KeyF41: "", tcell.KeyF42: "", tcell.KeyF43: "", tcell.KeyF44: "", tcell.KeyF45: "", tcell.KeyF46: "", tcell.KeyF47: "", tcell.KeyF48: "", tcell.KeyF49: "", tcell.KeyF50: "", tcell.KeyF51: "", tcell.KeyF52: "", tcell.KeyF53: "", tcell.KeyF54: "", tcell.KeyF55: "", tcell.KeyF56: "", tcell.KeyF57: "", tcell.KeyF58: "", tcell.KeyF59: "", tcell.KeyF60: "", tcell.KeyF61: "", tcell.KeyF62: "", tcell.KeyF63: "", tcell.KeyF64: "", tcell.KeyCtrlA: "", tcell.KeyCtrlB: "", tcell.KeyCtrlC: "", tcell.KeyCtrlD: "", tcell.KeyCtrlE: "", tcell.KeyCtrlF: "", tcell.KeyCtrlG: "", tcell.KeyCtrlJ: "", tcell.KeyCtrlK: "", tcell.KeyCtrlL: "", tcell.KeyCtrlN: "", tcell.KeyCtrlO: "", tcell.KeyCtrlP: "", tcell.KeyCtrlQ: "", tcell.KeyCtrlR: "", tcell.KeyCtrlS: "", tcell.KeyCtrlT: "", tcell.KeyCtrlU: "", tcell.KeyCtrlV: "", tcell.KeyCtrlW: "", tcell.KeyCtrlX: "", tcell.KeyCtrlY: "", tcell.KeyCtrlZ: "", } var gValKey map[string]tcell.Key func init() { gValKey = make(map[string]tcell.Key, len(gKeyVal)) for k, v := range gKeyVal { gValKey[v] = k } } // for simplicity, assume there will only be one modifier (ctrl, shift or alt) var reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`) func wrapModifier(s string, mod string) string { s = strings.TrimPrefix(s, "<") s = strings.TrimSuffix(s, ">") return fmt.Sprintf("<%s-%s>", mod, s) } func addKeyModifier(s string, mod tcell.ModMask) string { if reModKey.MatchString(s) { return s } switch { case mod&tcell.ModCtrl != 0: return wrapModifier(s, "c") case mod&tcell.ModShift != 0: return wrapModifier(s, "s") case mod&tcell.ModAlt != 0: return wrapModifier(s, "a") default: return s } } func readKey(ev *tcell.EventKey) string { var s string if ev.Key() == tcell.KeyRune { switch ev.Str() { case "<": s = "" case ">": s = "" case " ": s = "" default: s = ev.Str() } } else { s = gKeyVal[ev.Key()] } return addKeyModifier(s, ev.Modifiers()) } func parseKeyModifier(s string) (tcell.ModMask, string) { matches := reModKey.FindStringSubmatch(s) if matches == nil { return tcell.ModNone, s } mod := tcell.ModNone switch matches[1] { case "c": mod = tcell.ModCtrl case "s": mod = tcell.ModShift case "a": mod = tcell.ModAlt } s = matches[2] if len(s) > 1 { s = "<" + s + ">" } return mod, s } func parseKey(s string) *tcell.EventKey { if key, ok := gValKey[s]; ok { return tcell.NewEventKey(key, "", tcell.ModNone) } mod, s := parseKeyModifier(s) k := tcell.KeyRune if key, ok := gValKey[s]; ok { k = key s = "" } else { switch s { case "": s = "<" case "": s = ">" case "": s = " " } } return tcell.NewEventKey(k, s, mod) } ================================================ FILE: key_test.go ================================================ package main import ( "testing" "github.com/gdamore/tcell/v3" ) var gKeyTests = []struct { ev *tcell.EventKey s string }{ {tcell.NewEventKey(tcell.KeyRune, "<", tcell.ModNone), ""}, {tcell.NewEventKey(tcell.KeyRune, ">", tcell.ModNone), ""}, {tcell.NewEventKey(tcell.KeyRune, " ", tcell.ModNone), ""}, {tcell.NewEventKey(tcell.KeyRune, "a", tcell.ModNone), "a"}, {tcell.NewEventKey(tcell.KeyCtrlA, "", tcell.ModNone), ""}, {tcell.NewEventKey(tcell.KeyRune, "A", tcell.ModNone), "A"}, {tcell.NewEventKey(tcell.KeyRune, "a", tcell.ModAlt), ""}, {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModNone), ""}, {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModCtrl), ""}, {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModShift), ""}, {tcell.NewEventKey(tcell.KeyLeft, "", tcell.ModAlt), ""}, {tcell.NewEventKey(tcell.KeyEsc, "", tcell.ModNone), ""}, {tcell.NewEventKey(tcell.KeyF1, "", tcell.ModNone), ""}, } func TestReadKey(t *testing.T) { for _, test := range gKeyTests { if got := readKey(test.ev); got != test.s { t.Errorf("at input '%#v' expected '%s' but got '%s'", test.ev, test.s, got) } } } func TestParseKey(t *testing.T) { keyEqual := func(ev1, ev2 *tcell.EventKey) bool { return ev1.Key() == ev2.Key() && ev1.Modifiers() == ev2.Modifiers() && ev1.Str() == ev2.Str() } for _, test := range gKeyTests { if got := parseKey(test.s); !keyEqual(got, test.ev) { t.Errorf("at input '%s' expected '%#v' but got '%#v'", test.s, test.ev, got) } } } ================================================ FILE: lf.1 ================================================ .\" Automatically generated by Pandoc 3.7.0.2 .\" .TH "LF" "1" "2026\-03\-21" "r42" "DOCUMENTATION" .SH NAME lf \- terminal file manager .SH SYNOPSIS \f[B]lf\f[R] [\f[B]\-command\f[R] \f[I]command\f[R]] [\f[B]\-config\f[R] \f[I]path\f[R]] [\f[B]\-cpuprofile\f[R] \f[I]path\f[R]] [\f[B]\-doc\f[R]] [\f[B]\-help\f[R]] [\f[B]\-last\-dir\-path\f[R] \f[I]path\f[R]] [\f[B]\-log\f[R] \f[I]path\f[R]] [\f[B]\-memprofile\f[R] \f[I]path\f[R]] [\f[B]\-print\-last\-dir\f[R]] [\f[B]\-print\-selection\f[R]] [\f[B]\-remote\f[R] \f[I]command\f[R]] [\f[B]\-selection\-path\f[R] \f[I]path\f[R]] [\f[B]\-server\f[R]] [\f[B]\-single\f[R]] [\f[B]\-version\f[R]] [\f[I]cd\-or\-select\-path\f[R]] .SH DESCRIPTION lf is a terminal file manager. .PP The source code can be found in the repository at \c .UR https://github.com/gokcehan/lf .UE \c .PP This documentation can either be read from the terminal using \f[CR]lf \-doc\f[R] or online at \c .UR https://github.com/gokcehan/lf/blob/master/doc.md .UE \c \ You can also use the \f[CR]help\f[R] command (default \f[CR]\f[R]) inside lf to view the documentation in a pager. A man page with the same content is also available in the repository at \c .UR https://github.com/gokcehan/lf/blob/master/lf.1 .UE \c .SH OPTIONS .SS POSITIONAL ARGUMENTS .TP \f[B]cd\-or\-select\-path\f[R] Set the starting location. If \f[I]path\f[R] is a directory, start in there. If it\(aqs a file, start in the file\(aqs parent directory and select the file. When no \f[I]path\f[R] is supplied, lf uses the current directory. Only accepts one argument. .SS META OPTIONS .TP \f[B]\-doc\f[R] Show lf\(aqs documentation (same content as this file) and exit. .TP \f[B]\-help\f[R] Show command\-line usage and exit. .TP \f[B]\-version\f[R] Show version information and exit. .SS STARTUP & CONFIGURATION .TP \f[B]\-command\f[R] \f[I]command\f[R] Execute \f[I]command\f[R] during client initialization (i.e. after reading configuration, before \f[CR]on\-init\f[R]). To execute more than one command, you can either use the \f[B]\-command\f[R] flag multiple times or pass multiple commands at once by chaining them with \(dq;\(dq. .TP \f[B]\-config\f[R] \f[I]path\f[R] Use the config file at \f[I]path\f[R] instead of the normal search locations. This only affects which \f[CR]lfrc\f[R] is read at startup. .SS SHELL INTEGRATION .TP \f[B]\-print\-last\-dir\f[R] Print the last directory to stdout when lf exits. This can be used to let lf change your shells working directory. See \f[CR]CHANGING DIRECTORY\f[R] for more details. .TP \f[B]\-last\-dir\-path\f[R] \f[I]path\f[R] Same as \f[B]\-print\-last\-dir\f[R], but write the directory to \f[I]path\f[R] instead of stdout. .TP \f[B]\-print\-selection\f[R] Print selected files to stdout when opening a file in lf. This can be used to use lf as an \(dqopen file\(dq dialog. First, select the files you want to pass to another program. Then, confirm the selection by opening a file. This causes lf to quit and print out the selection. Quitting lf prematurely discards the selection. .TP \f[B]\-selection\-path\f[R] \f[I]path\f[R] Same as \f[B]\-print\-selection\f[R], but write the newline\-separated list to \f[I]path\f[R] instead of stdout. .SS SERVER .TP \f[B]\-remote\f[R] \f[I]command\f[R] Send \f[I]command\f[R] to the running server (i.e. \f[CR]send\f[R], \f[CR]query\f[R], \f[CR]list\f[R], \f[CR]quit\f[R], or \f[CR]quit!\f[R]). See \f[CR]REMOTE COMMANDS\f[R] for more details. .TP \f[B]\-server\f[R] Start the (headless) server process explicitly. Runs in the foreground and writes server logs to stderr (or the file set with \f[B]\-log\f[R]). Clients auto\-start a server if none is running unless \f[B]\-single\f[R] is used. .TP \f[B]\-single\f[R] Start a stand\-alone client without a server. Disables remote control. .SS DIAGNOSTICS .TP \f[B]\-log\f[R] \f[I]path\f[R] Append runtime log messages to \f[I]path\f[R]. .TP \f[B]\-cpuprofile\f[R] \f[I]path\f[R] Write a CPU profile to \f[I]path\f[R]. The profile can be used by \f[CR]go tool pprof\f[R]. .TP \f[B]\-memprofile\f[R] \f[I]path\f[R] Write a memory profile to \f[I]path\f[R]. The profile can be used by \f[CR]go tool pprof\f[R]. .SS EXAMPLES Use \f[CR]lf\f[R] to select files (while hiding certain file types): .IP .EX lf \-command \(aqset nohidden\(aq \-command \(aqset hiddenfiles \(dq*mp4:*pdf:*txt\(dq\(aq \-print\-selection .EE .PP Another sophisticated \(dqopen file\(dq dialog focusing on design: .IP .EX lf \-command \(aqset nopreview; set ratios 1; set drawbox; set promptfmt \(dqSelect files [%w] %S q: cancel, l: confirm\(dq\(aq \-print\-selection .EE .PP Open Downloads and set \f[CR]sortby\f[R] and \f[CR]info\f[R] to creation date: .IP .EX lf \-command \(aqset sortby btime; set info btime\(aq \(ti/Downloads .EE .PP Temporarily prevent \f[CR]lf\f[R] from modifying the command history: .IP .EX lf \-command \(aqset nohistory\(aq .EE .PP Use default settings and log current session: .IP .EX lf \-config /dev/null \-log /tmp/lf.log .EE .PP Force\-quit the server: .IP .EX lf \-remote \(aqquit!\(aq .EE .PP Inherit lf\(aqs working directory in your shell: .IP .EX cd \(dq$(lf \-print\-last\-dir)\(dq .EE .SH QUICK REFERENCE The following commands are provided by lf: .IP .EX quit (default \(aqq\(aq) up (default \(aqk\(aq and \(aq\(aq) half\-up (default \(aq\(aq) page\-up (default \(aq\(aq and \(aq\(aq) scroll\-up (default \(aq\(aq) down (default \(aqj\(aq and \(aq\(aq) half\-down (default \(aq\(aq) page\-down (default \(aq\(aq and \(aq\(aq) scroll\-down (default \(aq\(aq) updir (default \(aqh\(aq and \(aq\(aq) open (default \(aql\(aq and \(aq\(aq) jump\-next (default \(aq]\(aq) jump\-prev (default \(aq[\(aq) top (default \(aqgg\(aq and \(aq\(aq) bottom (default \(aqG\(aq and \(aq\(aq) high (default \(aqH\(aq) middle (default \(aqM\(aq) low (default \(aqL\(aq) toggle invert (default \(aqv\(aq) unselect (default \(aqu\(aq) glob\-select glob\-unselect copy (default \(aqy\(aq) cut (default \(aqd\(aq) paste (default \(aqp\(aq) clear (default \(aqc\(aq) sync draw redraw (default \(aq\(aq) load reload (default \(aq\(aq) delete (modal) rename (modal) (default \(aqr\(aq) read (modal) (default \(aq:\(aq) shell (modal) (default \(aq$\(aq) shell\-pipe (modal) (default \(aq%\(aq) shell\-wait (modal) (default \(aq!\(aq) shell\-async (modal) (default \(aq&\(aq) find (modal) (default \(aqf\(aq) find\-back (modal) (default \(aqF\(aq) find\-next (default \(aq;\(aq) find\-prev (default \(aq,\(aq) search (modal) (default \(aq/\(aq) search\-back (modal) (default \(aq?\(aq) search\-next (default \(aqn\(aq) search\-prev (default \(aqN\(aq) filter (modal) setfilter mark\-save (modal) (default \(aqm\(aq) mark\-load (modal) (default \(dq\(aq\(dq) mark\-remove (modal) (default \(aq\(dq\(aq) tag tag\-toggle (default \(aqt\(aq) echo echomsg echoerr cd select source push addcustominfo calcdirsize clearmaps tty\-write visual (default \(aqV\(aq) .EE .PP The following Visual mode commands are provided by lf: .IP .EX visual\-accept (default \(aqV\(aq) visual\-unselect visual\-discard (default \(aq\(aq) visual\-change (default \(aqo\(aq) .EE .PP The following Command\-line mode commands are provided by lf: .IP .EX cmd\-insert cmd\-escape (default \(aq\(aq) cmd\-complete (default \(aq\(aq) cmd\-menu\-complete cmd\-menu\-complete\-back cmd\-menu\-accept cmd\-menu\-discard cmd\-enter (default \(aq\(aq and \(aq\(aq) cmd\-interrupt (default \(aq\(aq) cmd\-history\-next (default \(aq\(aq and \(aq\(aq) cmd\-history\-prev (default \(aq\(aq and \(aq\(aq) cmd\-left (default \(aq\(aq and \(aq\(aq) cmd\-right (default \(aq\(aq and \(aq\(aq) cmd\-home (default \(aq\(aq and \(aq\(aq) cmd\-end (default \(aq\(aq and \(aq\(aq) cmd\-delete (default \(aq\(aq and \(aq\(aq) cmd\-delete\-back (default \(aq\(aq) cmd\-delete\-home (default \(aq\(aq) cmd\-delete\-end (default \(aq\(aq) cmd\-delete\-unix\-word (default \(aq\(aq) cmd\-yank (default \(aq\(aq) cmd\-transpose (default \(aq\(aq) cmd\-transpose\-word (default \(aq\(aq) cmd\-word (default \(aq\(aq) cmd\-word\-back (default \(aq\(aq) cmd\-delete\-word (default \(aq\(aq) cmd\-delete\-word\-back (default \(aq\(aq) cmd\-capitalize\-word (default \(aq\(aq) cmd\-uppercase\-word (default \(aq\(aq) cmd\-lowercase\-word (default \(aq\(aq) .EE .PP The following options can be used to customize the behavior of lf: .IP .EX anchorfind bool (default true) autoquit bool (default true) borderfmt string (default \(dq\(rs033[0m\(dq) borderstyle string (default \(aqbox\(aq) cleaner string (default \(aq\(aq) copyfmt string (default \(dq\(rs033[7;33m\(dq) cursoractivefmt string (default \(dq\(rs033[7m\(dq) cursorparentfmt string (default \(dq\(rs033[7m\(dq) cursorpreviewfmt string (default \(dq\(rs033[4m\(dq) cutfmt string (default \(dq\(rs033[7;31m\(dq) dircounts bool (default false) dirfirst bool (default true) dironly bool (default false) dirpreviews bool (default false) drawbox bool (default false) dupfilefmt string (default \(aq%f.\(ti%n\(ti\(aq) errorfmt string (default \(dq\(rs033[7;31;47m\(dq) filesep string (default \(dq\(rsn\(dq) filtermethod string (default \(aqtext\(aq) findlen int (default 1) hidden bool (default false) hiddenfiles []string (default \(aq.*\(aq for Unix and \(aq\(aq for Windows) history bool (default true) icons bool (default false) ifs string (default \(aq\(aq) ignorecase bool (default true) ignoredia bool (default true) incfilter bool (default false) incsearch bool (default false) info []string (default \(aq\(aq) infotimefmtnew string (default \(aqJan _2 15:04\(aq) infotimefmtold string (default \(aqJan _2 2006\(aq) menufmt string (default \(dq\(rs033[0m\(dq) menuheaderfmt string (default \(dq\(rs033[1m\(dq) menuselectfmt string (default \(dq\(rs033[7m\(dq) mergeindicators bool (default false) mouse bool (default false) number bool (default false) numbercursorfmt string (default \(aq\(aq) numberfmt string (default \(dq\(rs033[33m\(dq) period int (default 0) preload bool (default false) preserve []string (default \(dqmode\(dq) preview bool (default true) previewer string (default \(aq\(aq) promptfmt string (default \(dq\(rs033[32;1m%u\(at%h\(rs033[0m:\(rs033[34;1m%d\(rs033[0m\(rs033[1m%f\(rs033[0m\(dq) ratios []int (default \(aq1:2:3\(aq) relativenumber bool (default false) reverse bool (default false) rulerfile string (default \(dq\(dq) rulerfmt string (default \(dq\(dq) scrolloff int (default 0) searchmethod string (default \(aqtext\(aq) selectfmt string (default \(dq\(rs033[7;35m\(dq) selmode string (default \(aqall\(aq) shell string (default \(aqsh\(aq for Unix and \(aqcmd\(aq for Windows) shellflag string (default \(aq\-c\(aq for Unix and \(aq/c\(aq for Windows) shellopts []string (default \(aq\(aq) showbinds bool (default true) sizeunits string (default \(aqbinary\(aq) smartcase bool (default true) smartdia bool (default false) sortby string (default \(aqnatural\(aq) statfmt string (default \(dq\(rs033[36m%p\(rs033[0m| %c| %u| %g| %S| %t| \-> %l\(dq) tabstop int (default 8) tagfmt string (default \(dq\(rs033[31m\(dq) tempmarks string (default \(aq\(aq) terminalcursor string (default \(aqdefault\(aq) timefmt string (default \(aqMon Jan _2 15:04:05 2006\(aq) truncatechar string (default \(aq\(ti\(aq) truncatepct int (default 100) visualfmt string (default \(dq\(rs033[7;36m\(dq) waitmsg string (default \(aqPress any key to continue\(aq) watch bool (default false) wrapscan bool (default true) wrapscroll bool (default false) user_{option} string (default none) .EE .PP The following environment variables are exported for shell commands: .IP .EX f fs fv fx id PWD OLDPWD LF_LEVEL OPENER VISUAL EDITOR PAGER SHELL lf lf_{option} lf_user_{option} lf_flag_{flag} lf_width lf_height lf_count lf_mode .EE .PP The following special shell commands are used to customize the behavior of lf when defined: .IP .EX open paste rename delete pre\-cd on\-cd on\-load on\-focus\-gained on\-focus\-lost on\-init on\-select on\-redraw on\-quit .EE .PP The following commands/keybindings are provided by default: .IP .EX Unix cmd open &$OPENER \(dq$f\(dq map e $$EDITOR \(dq$f\(dq map i $$PAGER \(dq$f\(dq map w $$SHELL cmd help $$lf \-doc | $PAGER map help cmd maps $lf \-remote \(dqquery $id maps\(dq | $PAGER cmd nmaps $lf \-remote \(dqquery $id nmaps\(dq | $PAGER cmd vmaps $lf \-remote \(dqquery $id vmaps\(dq | $PAGER cmd cmaps $lf \-remote \(dqquery $id cmaps\(dq | $PAGER cmd cmds $lf \-remote \(dqquery $id cmds\(dq | $PAGER Windows cmd open &%OPENER% %f% map e $%EDITOR% %f% map i !%PAGER% %f% map w $%SHELL% cmd help !%lf% \-doc | %PAGER% map help cmd maps !%lf% \-remote \(dqquery %id% maps\(dq | %PAGER% cmd nmaps !%lf% \-remote \(dqquery %id% nmaps\(dq | %PAGER% cmd vmaps !%lf% \-remote \(dqquery %id% vmaps\(dq | %PAGER% cmd cmaps !%lf% \-remote \(dqquery %id% cmaps\(dq | %PAGER% cmd cmds !%lf% \-remote \(dqquery %id% cmds\(dq | %PAGER% .EE .PP The defaults for Windows are using \f[CR]cmd\f[R] syntax. A \f[CR]PowerShell\f[R] compatible configuration file can be found at \c .UR https://github.com/gokcehan/lf/blob/master/etc/lfrc.ps1.example .UE \c .PP The following additional keybindings are provided by default: .IP .EX map zh set hidden! map zr set reverse! map zn set info map zs set info size map zt set info time map za set info size:time map sn :set sortby natural; set info map ss :set sortby size; set info size map st :set sortby time; set info time map sa :set sortby atime; set info atime map sb :set sortby btime; set info btime map sc :set sortby ctime; set info ctime map se :set sortby ext; set info map gh cd \(ti nmap :toggle; down .EE .PP If the \f[CR]mouse\f[R] option is enabled, mouse buttons have the following default effects: .IP .EX Left mouse button Click on a file or directory to select it. Right mouse button Enter a directory or open a file. Also works on the preview pane. Scroll wheel Move up or down. If Ctrl is pressed, scroll up or down. .EE .SH CONFIGURATION Configuration files should be located at: .IP .EX OS system\-wide user\-specific Unix /etc/lf/lfrc \(ti/.config/lf/lfrc Windows C:\(rsProgramData\(rslf\(rslfrc C:\(rsUsers\(rs\(rsAppData\(rsRoaming\(rslf\(rslfrc .EE .PP The colors file should be located at: .IP .EX OS system\-wide user\-specific Unix /etc/lf/colors \(ti/.config/lf/colors Windows C:\(rsProgramData\(rslf\(rscolors C:\(rsUsers\(rs\(rsAppData\(rsRoaming\(rslf\(rscolors .EE .PP The icons file should be located at: .IP .EX OS system\-wide user\-specific Unix /etc/lf/icons \(ti/.config/lf/icons Windows C:\(rsProgramData\(rslf\(rsicons C:\(rsUsers\(rs\(rsAppData\(rsRoaming\(rslf\(rsicons .EE .PP The selection file should be located at: .IP .EX Unix \(ti/.local/share/lf/files Windows C:\(rsUsers\(rs\(rsAppData\(rsLocal\(rslf\(rsfiles .EE .PP The marks file should be located at: .IP .EX Unix \(ti/.local/share/lf/marks Windows C:\(rsUsers\(rs\(rsAppData\(rsLocal\(rslf\(rsmarks .EE .PP The tags file should be located at: .IP .EX Unix \(ti/.local/share/lf/tags Windows C:\(rsUsers\(rs\(rsAppData\(rsLocal\(rslf\(rstags .EE .PP The history file should be located at: .IP .EX Unix \(ti/.local/share/lf/history Windows C:\(rsUsers\(rs\(rsAppData\(rsLocal\(rslf\(rshistory .EE .PP You can configure these locations with the following variables given with their order of precedences and their default values: .IP .EX Unix $LF_CONFIG_HOME $XDG_CONFIG_HOME \(ti/.config $LF_DATA_HOME $XDG_DATA_HOME \(ti/.local/share Windows %LF_CONFIG_HOME% %XDG_CONFIG_HOME% %APPDATA% %LF_DATA_HOME% %XDG_DATA_HOME% %LOCALAPPDATA% .EE .PP A sample configuration file can be found at \c .UR https://github.com/gokcehan/lf/blob/master/etc/lfrc.example .UE \c .SH COMMANDS This section shows information about built\-in commands. Modal commands do not take any arguments, but instead change the operation mode to read their input conveniently, and so they are meant to be assigned to keybindings. .SS quit (default \f[CR]q\f[R]) Quit lf and return to the shell. .SS up (default \f[CR]k\f[R] and \f[CR]\f[R]), half\-up (default \f[CR]\f[R]), page\-up (default \f[CR]\f[R] and \f[CR]\f[R]), scroll\-up (default \f[CR]\f[R]), down (default \f[CR]j\f[R] and \f[CR]\f[R]), half\-down (default \f[CR]\f[R]), page\-down (default \f[CR]\f[R] and \f[CR]\f[R]), scroll\-down (default \f[CR]\f[R]) Move/scroll the current file selection upwards/downwards by one/half a page/full page. .SS updir (default \f[CR]h\f[R] and \f[CR]\f[R]) Change the current working directory to the parent directory. .SS open (default \f[CR]l\f[R] and \f[CR]\f[R]) If the current file is a directory, then change the current directory to it, otherwise, execute the \f[CR]open\f[R] command. A default \f[CR]open\f[R] command is provided to call the default system opener asynchronously with the current file as the argument. A custom \f[CR]open\f[R] command can be defined to override this default. .SS jump\-next (default \f[CR]]\f[R]), jump\-prev (default \f[CR][\f[R]) Change the current working directory to the next/previous jumplist item. .SS top (default \f[CR]gg\f[R] and \f[CR]\f[R]), bottom (default \f[CR]G\f[R] and \f[CR]\f[R]) Move the current file selection to the top/bottom of the directory. A count can be specified to move to a specific line, for example, use \f[CR]3G\f[R] to move to the third line. .SS high (default \f[CR]H\f[R]), middle (default \f[CR]M\f[R]), low (default \f[CR]L\f[R]) Move the current file selection to the high/middle/low of the screen. .SS toggle Toggle the selection of the current file or files given as arguments. .SS invert (default \f[CR]v\f[R]) Reverse the selection of all files in the current directory (i.e. \f[CR]toggle\f[R] all files). Selections in other directories are not affected by this command. You can define a new command to select all files in the directory by combining \f[CR]invert\f[R] with \f[CR]unselect\f[R] (i.e. \f[CR]cmd select\-all :unselect; invert\f[R]), though this will also remove selections in other directories. .SS unselect (default \f[CR]u\f[R]) Remove the selection of all files in all directories. .SS glob\-select, glob\-unselect Select/unselect files that match the given glob. .SS copy (default \f[CR]y\f[R]) Save the paths of selected files to the clipboard as files to be copied. If there are no selected files, the path of the current file is used instead. .SS cut (default \f[CR]d\f[R]) Save the paths of selected files to the clipboard as files to be moved. If there are no selected files, the path of the current file is used instead. .SS paste (default \f[CR]p\f[R]) Copy/Move files in the clipboard to the current working directory. A custom \f[CR]paste\f[R] command can be defined to override this default. .SS clear (default \f[CR]c\f[R]) Clear file paths in the clipboard. .SS sync Synchronize copied/cut files with the server. This command is automatically called when required. .SS draw Draw the screen. This command is automatically called when required. .SS redraw (default \f[CR]\f[R]) Synchronize the terminal and redraw the screen. .SS load Load modified files and directories. This command is automatically called when required. .SS reload (default \f[CR]\f[R]) Flush the cache and reload all files and directories. .SS delete (modal) Remove the current file or selected file(s). A custom \f[CR]delete\f[R] command can be defined to override this default. .SS rename (modal) (default \f[CR]r\f[R]) Rename the current file using the built\-in method. A custom \f[CR]rename\f[R] command can be defined to override this default. .SS read (modal) (default \f[CR]:\f[R]) Read a command to evaluate. .SS shell (modal) (default \f[CR]$\f[R]) Read a shell command to execute. .SS shell\-pipe (modal) (default \f[CR]%\f[R]) Read a shell command to execute piping its standard I/O to the bottom statline. .SS shell\-wait (modal) (default \f[CR]!\f[R]) Read a shell command to execute and wait for a key press at the end. .SS shell\-async (modal) (default \f[CR]&\f[R]) Read a shell command to execute asynchronously without standard I/O. .SS find (modal) (default \f[CR]f\f[R]), find\-back (modal) (default \f[CR]F\f[R]), find\-next (default \f[CR];\f[R]), find\-prev (default \f[CR],\f[R]) Read key(s) to find the appropriate filename match in the forward/backward direction and jump to the next/previous match. .SS search (default \f[CR]/\f[R]), search\-back (default \f[CR]?\f[R]), search\-next (default \f[CR]n\f[R]), search\-prev (default \f[CR]N\f[R]) Read a pattern to search for a filename match in the forward/backward direction and jump to the next/previous match. .SS filter (modal), setfilter Command \f[CR]filter\f[R] reads a pattern to filter out and only view files matching the pattern. Command \f[CR]setfilter\f[R] does the same but uses an argument to set the filter immediately. You can supply an argument to \f[CR]filter\f[R] to use as the starting prompt. .SS mark\-save (modal) (default \f[CR]m\f[R]) Save the current directory as a bookmark assigned to the given key. .SS mark\-load (modal) (default \f[CR]\(aq\f[R]) Change the current directory to the bookmark assigned to the given key. A special bookmark \f[CR]\(aq\f[R] holds the previous directory after a \f[CR]mark\-load\f[R], \f[CR]cd\f[R], or \f[CR]select\f[R] command. .SS mark\-remove (modal) (default \f[CR]\(dq\f[R]) Remove a bookmark assigned to the given key. .SS tag Tag a file with \f[CR]*\f[R] or a single\-width character given in the argument. You can define a new tag\-clearing command by combining \f[CR]tag\f[R] with \f[CR]tag\-toggle\f[R] (i.e. \f[CR]cmd tag\-clear :tag; tag\-toggle\f[R]). .SS tag\-toggle (default \f[CR]t\f[R]) Tag a file with \f[CR]*\f[R] or a single\-width character given in the argument if the file is untagged, otherwise remove the tag. .SS echo Print the given arguments to the message line at the bottom. .SS echomsg Print the given arguments to the message line at the bottom and also to the log file. .SS echoerr Print given arguments to the message line at the bottom as \f[CR]errorfmt\f[R] and also to the log file. .SS cd Change the working directory to the given argument. .SS select Change the current file selection to the given argument. .SS source Read the configuration file given in the argument. .SS push Simulate key pushes given in the argument. .SS addcustominfo Update the \f[CR]custom\f[R] info and \f[CR].Stat.CustomInfo\f[R] field of the given file with the given string. The info string may contain ANSI escape codes to further customize its appearance. If no info is provided, clear the file\(aqs info instead. .SS calcdirsize Calculate the total size for each of the selected directories. Option \f[CR]info\f[R] should include \f[CR]size\f[R] and option \f[CR]dircounts\f[R] should be disabled to show this size. If the total size of a directory is not calculated, it will be shown as \f[CR]\-\f[R]. .SS clearmaps Remove all keybindings associated with the \f[CR]map\f[R], \f[CR]nmap\f[R] and \f[CR]vmap\f[R] command. This command can be used in the config file to remove the default keybindings. For safety purposes, \f[CR]:\f[R] is left mapped to the \f[CR]read\f[R] command, and \f[CR]cmap\f[R] keybindings are retained so that it is still possible to exit \f[CR]lf\f[R] using \f[CR]:quit\f[R]. .SS tty\-write Write the given string to the tty. This is useful for sending escape sequences to the terminal to control its behavior (e.g. OSC 0 to set the window title). Using \f[CR]tty\-write\f[R] is preferred over directly writing to \f[CR]/dev/tty\f[R] because the latter is not synchronized and can interfere with drawing the UI. .SS visual (default \f[CR]V\f[R]) Switch to Visual mode. If already in Visual mode, discard the visual selection and stay in Visual mode. .SH VISUAL MODE COMMANDS .SS visual\-accept (default \f[CR]V\f[R]) Add the visual selection to the selection list, quit Visual mode and return to Normal mode. .SS visual\-unselect Remove the visual selection from the selection list, quit Visual mode and return to Normal mode. .SS visual\-discard (default \f[CR]\f[R]) Discard the visual selection, quit Visual mode and return to Normal mode. .SS visual\-change (default \f[CR]o\f[R]) Go to the other end of the current Visual mode selection. .SH COMMAND\-LINE MODE COMMANDS The prompt character specifies which of the several Command\-line modes you are in. For example, the \f[CR]read\f[R] command takes you to the \f[CR]:\f[R] mode. .PP When the cursor is at the first character in \f[CR]:\f[R] mode, pressing one of the keys \f[CR]!\f[R], \f[CR]$\f[R], \f[CR]%\f[R], or \f[CR]&\f[R] takes you to the corresponding mode. You can go back with \f[CR]cmd\-delete\-back\f[R] (\f[CR]\f[R] by default). .PP The command line commands should be mostly compatible with readline keybindings. A character refers to a Unicode code point, a word consists of letters and digits, and a Unix word consists of any non\-blank characters. .SS cmd\-insert Insert the character given in the argument. This command is automatically called when required. .SS cmd\-escape (default \f[CR]\f[R]) Quit Command\-line mode and return to Normal mode. .SS cmd\-complete (default \f[CR]\f[R]) Autocomplete the current word. .SS cmd\-menu\-complete, cmd\-menu\-complete\-back Autocomplete the current word with the menu selection. You need to assign keys to these commands (e.g. \f[CR]cmap cmd\-menu\-complete; cmap cmd\-menu\-complete\-back\f[R]). You can use the assigned keys to display the menu and then cycle through completion options. .SS cmd\-menu\-accept Accept the currently selected match in menu completion and close the menu. .SS cmd\-menu\-discard Discard the currently selected match in menu completion and close the menu. .SS cmd\-enter (default \f[CR]\f[R] and \f[CR]\f[R]) Execute the current line. .SS cmd\-interrupt (default \f[CR]\f[R]) Interrupt the current shell\-pipe command and return to the Normal mode. .SS cmd\-history\-next (default \f[CR]\f[R] and \f[CR]\f[R]), cmd\-history\-prev (default \f[CR]\f[R] and \f[CR]\f[R]) Go to the next/previous entry in the command history. If part of the command is already typed, then only matching entries will be considered, and consecutive duplicate entries are skipped. .SS cmd\-left (default \f[CR]\f[R] and \f[CR]\f[R]), cmd\-right (default \f[CR]\f[R] and \f[CR]\f[R]) Move the cursor to the left/right. .SS cmd\-home (default \f[CR]\f[R] and \f[CR]\f[R]), cmd\-end (default \f[CR]\f[R] and \f[CR]\f[R]) Move the cursor to the beginning/end of the line. .SS cmd\-delete (default \f[CR]\f[R] and \f[CR]\f[R]) Delete the next character. .SS cmd\-delete\-back (default \f[CR]\f[R]) Delete the previous character. When at the beginning of a prompt, returns either to Normal mode or to \f[CR]:\f[R] mode. .SS cmd\-delete\-home (default \f[CR]\f[R]), cmd\-delete\-end (default \f[CR]\f[R]) Delete everything up to the beginning/end of the line. .SS cmd\-delete\-unix\-word (default \f[CR]\f[R]) Delete the previous Unix word. .SS cmd\-yank (default \f[CR]\f[R]) Paste the buffer content containing the last deleted item. .SS cmd\-transpose (default \f[CR]\f[R]) Swap the characters before and after the cursor, then move the cursor forward. If there is no character after the cursor, swap the previous two characters instead. .SS cmd\-transpose\-word (default \f[CR]\f[R]) Swap the words before and after the cursor, then move the cursor forward. If there is no word after the cursor, swap the previous two words instead. .SS cmd\-word (default \f[CR]\f[R]), cmd\-word\-back (default \f[CR]\f[R]) Move the cursor by one word in the forward/backward direction. .SS cmd\-delete\-word (default \f[CR]\f[R]) Delete the next word in the forward direction. .SS cmd\-delete\-word\-back (default \f[CR]\f[R]) Delete the previous word in the backward direction. .SS cmd\-capitalize\-word (default \f[CR]\f[R]), cmd\-uppercase\-word (default \f[CR]\f[R]), cmd\-lowercase\-word (default \f[CR]\f[R]) Capitalize/uppercase/lowercase the current word and jump to the next word. .SH SETTINGS This section shows information about options to customize the behavior. Character \f[CR]:\f[R] is used as the separator for list options \f[CR][]int\f[R] and \f[CR][]string\f[R]. .SS anchorfind (bool) (default true) When this option is enabled, the find command starts matching patterns from the beginning of filenames, otherwise, it can match at an arbitrary position. .SS autoquit (bool) (default true) Automatically quit the server when there are no clients left connected. .SS borderfmt (string) (default \f[CR]\(rs033[0m\f[R]) Format string of border characters. .SS borderstyle (string) (default \f[CR]box\f[R]) Border style used by \f[CR]drawbox\f[R]. .PP The following styles are supported: .IP .EX box outline around all panes and separators between them roundbox like \(gabox\(ga, but with rounded outer corners outline outline around all panes roundoutline like \(gaoutline\(ga, but with rounded outer corners separators separators between panes .EE .SS cleaner (string) (default \(ga\(ga) (not called if empty) Set the path of a cleaner file. The file should be executable. This file is called if previewing is enabled, the previewer is set, and the previously selected file has its preview cache disabled. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position of preview pane and (6) next filename to be previewed respectively. Preview cleaning is disabled when the value of this option is left empty. .SS copyfmt (string) (default \f[CR]\(rs033[7;33m\f[R]) Format string of the indicator for files to be copied. .SS cursoractivefmt (string) (default \f[CR]\(rs033[7m\f[R]), cursorparentfmt (string) (default \f[CR]\(rs033[7m\f[R]), cursorpreviewfmt (string) (default \f[CR]\(rs033[4m\f[R]) Format strings for highlighting the cursor. \f[CR]cursoractivefmt\f[R] applies in the current directory pane, \f[CR]cursorparentfmt\f[R] applies in panes that show parents of the current directory, and \f[CR]cursorpreviewfmt\f[R] applies in panes that preview directories. .PP The default is to make the active cursor and the parent directory cursor inverted. The preview cursor is underlined. .PP Some other possibilities to consider for the preview or parent cursors: an empty string for no cursor, \f[CR]\(rs033[7;2m\f[R] for dimmed inverted text (visibility varies by terminal), \f[CR]\(rs033[7;90m\f[R] for inverted text with grey (aka \(dqbrightblack\(dq) background. .PP If the format string contains the characters \f[CR]%s\f[R], it is interpreted as a format string for \f[CR]fmt.Sprintf\f[R]. Such a string should end with the terminal reset sequence. For example, \f[CR]\(rs033[4m%s\(rs033[0m\f[R] has the same effect as \f[CR]\(rs033[4m\f[R]. .SS cutfmt (string) (default \f[CR]\(rs033[7;31m\f[R]) Format string of the indicator for files to be cut. .SS dircounts (bool) (default false) When this option is enabled, directory sizes show the number of items inside instead of the total size of the directory, which needs to be calculated for each directory using \f[CR]calcdirsize\f[R]. This information needs to be calculated by reading the directory and counting the items inside. Therefore, this option is disabled by default for performance reasons. This option only has an effect when \f[CR]info\f[R] has a \f[CR]size\f[R] field and the pane is wide enough to show the information. 999 items are counted per directory at most, and bigger directories are shown as \f[CR]999+\f[R]. .SS dirfirst (bool) (default true) Show directories first above regular files. With \f[CR]dircounts\f[R] enabled, sorting by \f[CR]size\f[R] always separates directories and files, regardless of \f[CR]dirfirst\f[R]. .SS dironly (bool) (default false) Show only directories. .SS dirpreviews (bool) (default false) If enabled, directories will also be passed to the previewer script. This allows custom previews for directories. .SS drawbox (bool) (default false) Draw borders around panes using box drawing characters. .SS dupfilefmt (string) (default \f[CR]%f.\(ti%n\(ti\f[R]) Format string of filename when creating duplicate files. With the default format, copying a file \f[CR]abc.txt\f[R] to the same directory will result in a duplicate file called \f[CR]abc.txt.\(ti1\(ti\f[R]. Special expansions are provided, \f[CR]%f\f[R] as the file name, \f[CR]%b\f[R] for the base name (file name without extension), \f[CR]%e\f[R] as the extension (including the dot) and \f[CR]%n\f[R] as the number of duplicates. .SS errorfmt (string) (default \f[CR]\(rs033[7;31;47m\f[R]) Format string of error messages shown in the bottom message line. .PP If the format string contains the characters \f[CR]%s\f[R], it is interpreted as a format string for \f[CR]fmt.Sprintf\f[R]. Such a string should end with the terminal reset sequence. For example, \f[CR]\(rs033[4m%s\(rs033[0m\f[R] has the same effect as \f[CR]\(rs033[4m\f[R]. .SS filesep (string) (default \f[CR]\(rsn\f[R]) File separator used in environment variables \f[CR]fs\f[R], \f[CR]fv\f[R] and \f[CR]fx\f[R]. .SS filtermethod (string) (default \f[CR]text\f[R]) How filter command patterns are treated. Currently supported methods are \f[CR]text\f[R] (i.e. string literals), \f[CR]glob\f[R] (i.e. shell globs) and \f[CR]regex\f[R] (i.e. regular expressions). See \f[CR]SEARCHING FILES\f[R] for more details. .SS findlen (int) (default 1) Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left. .SS hidden (bool) (default false) Show hidden files. On Unix systems, hidden files are determined by the value of \f[CR]hiddenfiles\f[R]. On Windows, files with hidden attributes are also considered hidden files. .SS hiddenfiles ([]string) (default \f[CR].*\f[R] for Unix and \(ga\(ga for Windows) List of hidden file glob patterns. Patterns can be given as relative or absolute paths. Globbing supports the usual special characters, \f[CR]*\f[R] to match any sequence, \f[CR]?\f[R] to match any character, and \f[CR][...]\f[R] or \f[CR][\(ha...]\f[R] to match character sets or ranges. In addition, if a pattern starts with \f[CR]!\f[R], then its matches are excluded from hidden files. To add multiple patterns, use \f[CR]:\f[R] as a separator. Example: \f[CR].*:lost+found:*.bak\f[R] .SS history (bool) (default true) Save command history. .SS icons (bool) (default false) Show icons before each item in the list. .SS ifs (string) (default \(ga\(ga) Sets \f[CR]IFS\f[R] variable in shell commands. It works by adding the assignment to the beginning of the command string as \f[CR]IFS=...; ...\f[R]. The reason is that \f[CR]IFS\f[R] variable is not inherited by the shell for security reasons. This method assumes a POSIX shell syntax so it can fail for non\-POSIX shells. This option has no effect when the value is left empty. This option does not have any effect on Windows. .SS ignorecase (bool) (default true) Ignore case in sorting and search patterns. .SS ignoredia (bool) (default true) Ignore diacritics in sorting and search patterns. .SS incfilter (bool) (default false) Apply filter pattern after each keystroke during filtering. .SS incsearch (bool) (default false) Jump to the first match after each keystroke during searching. .SS info ([]string) (default \(ga\(ga) A list of information that is shown for directory items at the right side of the pane. .PP The following information types are supported: .IP .EX perm file permission user user name group group name size file size time time of last data modification atime time of last access btime time of file birth ctime time of last status (inode) change custom property defined via \(gaaddcustominfo\(ga (empty by default) .EE .PP Information is only shown when the pane width is more than twice the width of information. .SS infotimefmtnew (string) (default \f[CR]Jan _2 15:04\f[R]) Format string of the file time shown in the info column when it matches this year. .SS infotimefmtold (string) (default \f[CR]Jan _2 2006\f[R]) Format string of the file time shown in the info column when it doesn\(aqt match this year. .SS menufmt (string) (default \f[CR]\(rs033[0m\f[R]) Format string of the menu. .SS menuheaderfmt (string) (default \f[CR]\(rs033[1m\f[R]) Format string of the header row in the menu. .SS menuselectfmt (string) (default \f[CR]\(rs033[7m\f[R]) Format string of the currently selected item in the menu. .SS mergeindicators (bool) (default false) When \f[CR]mergeindicators\f[R] is enabled, tag and selection indicators are drawn in a single column to reduce the gap before filenames. If a file is both tagged and selected, the tag uses the selection format (e.g. \f[CR]copyfmt\f[R]) instead of \f[CR]tagfmt\f[R]. .SS mouse (bool) (default false) Send mouse events as input. .SS number (bool) (default false) Show the position number for directory items on the left side of the pane. When the \f[CR]relativenumber\f[R] option is enabled, only the current line shows the absolute position and relative positions are shown for the rest. .SS numberfmt (string) (default \f[CR]\(rs033[33m\f[R]), numbercursorfmt (string) (default \(ga\(ga) Format strings for highlighting line numbers. \f[CR]numberfmt\f[R] applies to all lines. \f[CR]numbercursorfmt\f[R] applies to the cursor line and falls back to \f[CR]numberfmt\f[R] when left empty. .SS period (int) (default 0) Set the interval in seconds for periodic checks of directory updates. This works by periodically calling the \f[CR]load\f[R] command. Note that directories are already updated automatically in many cases. This option can be useful when there is an external process changing the displayed directory and you are not doing anything in lf. Periodic checks are disabled when the value of this option is set to zero. .SS preload (bool) (default false) Allow previews to be generated in advance using the \f[CR]previewer\f[R] script as the user navigates through the filesystem. .SS preserve ([]string) (default \f[CR]mode\f[R]) List of attributes that are preserved when copying files. Currently supported attributes are \f[CR]mode\f[R] (i.e. access mode) and \f[CR]timestamps\f[R] (i.e. modification time and access time). Note that preserving other attributes like ownership of change/birth timestamp is desirable, but not portably supported in Go. .SS preview (bool) (default true) Show previews of files and directories at the rightmost pane. If the file has more lines than the preview pane, the rest of the lines are not read. Files containing the null character (U+0000) in the read portion are considered binary files and displayed as \f[CR]binary\f[R]. .SS previewer (string) (default \(ga\(ga) (not filtered if empty) Set the path of a previewer file to filter the content of regular files for previewing. The file should be executable. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode (\(dqpreview\(dq or \(dqpreload\(dq). SIGPIPE signal is sent when enough lines are read. If the previewer returns a non\-zero exit code, then the preview cache for the given file is disabled. This means that if the file is selected in the future, the previewer is called once again. Preview filtering is disabled and files are displayed as they are when the value of this option is left empty. If the \f[CR]preload\f[R] option is enabled, then this will be called with \f[CR]preload\f[R] as the mode when preloading file previews. Refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#previewing-files PREVIEWING FILES section .UE \c \ for more information about how to configure custom previews. .SS promptfmt (string) (default \f[CR]\(rs033[32;1m%u\(at%h\(rs033[0m:\(rs033[34;1m%d\(rs033[0m\(rs033[1m%f\(rs033[0m\f[R]) Format string of the prompt shown in the top line. .PP The following special expansions are supported: .IP .EX %f file name %h host name %u user name %w working directory %d working directory (with trailing path separator) %F current filter %S spacer to right\-align the following parts (can be used once) .EE .PP The home folder is shown as \f[CR]\(ti\f[R] in the working directory expansion. Directory names are automatically shortened to a single character starting from the leftmost parent when the prompt does not fit the screen. .SS ratios ([]int) (default \f[CR]1:2:3\f[R]) List of ratios of pane widths. Number of items in the list determines the number of panes in the UI. When the \f[CR]preview\f[R] option is enabled, the rightmost number is used for the width of the preview pane. .SS relativenumber (bool) (default false) Show the position number relative to the current line. When \f[CR]number\f[R] is enabled, the current line shows the absolute position, otherwise nothing is shown. .SS reverse (bool) (default false) Reverse the direction of sort. .SS rulerfile (string) (default \(ga\(ga) Set the path of the ruler file. If not set, then a default template will be used for the ruler. Refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#ruler RULER section .UE \c \ for more information about how the ruler file works. .SS rulerfmt (string) (default \(ga\(ga) Format string of the ruler shown in the bottom right corner. When set, it will be used along with \f[CR]statfmt\f[R] to draw the ruler, and \f[CR]rulerfile\f[R] will be ignored. However, using \f[CR]rulerfile\f[R] is preferred and this option is provided for backwards compatibility. .PP The following special expansions are supported: .IP .EX %a pressed keys %p progress of file operations %m number of files to be cut (moved) %c number of files to be copied %s number of selected files %v number of visually selected files %t number of shown files in the current directory %h number of hidden files in the current directory %f current filter %i cursor position %P scroll percentage %d amount of free disk space .EE .PP Additional expansions are provided for environment variables exported by lf, in the form \f[CR]%{lf_}\f[R] (e.g. \f[CR]%{lf_selmode}\f[R]). This is useful for displaying the current settings. Expansions are also provided for user\-defined options, in the form \f[CR]%{lf_user_}\f[R] (e.g. \f[CR]%{lf_user_foo}\f[R]). The \f[CR]|\f[R] character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown. .SS scrolloff (int) (default 0) Minimum number of offset lines shown at all times at the top and bottom of the screen when scrolling. The current line is kept in the middle when this option is set to a large value that is bigger than half the number of lines. A smaller offset can be used when the current file is close to the beginning or end of the list to show the maximum number of items. .SS searchmethod (string) (default \f[CR]text\f[R]) How search command patterns are treated. Currently supported methods are \f[CR]text\f[R] (i.e. string literals), \f[CR]glob\f[R] (i.e. shell globs) and \f[CR]regex\f[R] (i.e. regular expressions). See \f[CR]SEARCHING FILES\f[R] for more details. .SS selectfmt (string) (default \f[CR]\(rs033[7;35m\f[R]) Format string of the indicator for files that are selected. .SS selmode (string) (default \f[CR]all\f[R]) Selection mode for commands. When set to \f[CR]all\f[R] it will use the selected files from all directories. When set to \f[CR]dir\f[R] it will only use the selected files in the current directory. .SS shell (string) (default \f[CR]sh\f[R] for Unix and \f[CR]cmd\f[R] for Windows) Shell executable to use for shell commands. Shell commands are executed as \f[CR]shell shellopts shellflag command \-\- arguments\f[R]. .SS shellflag (string) (default \f[CR]\-c\f[R] for Unix and \f[CR]/c\f[R] for Windows) Command line flag used to pass shell commands. .SS shellopts ([]string) (default \(ga\(ga) List of shell options to pass to the shell executable. .SS showbinds (bool) (default true) Show bindings associated with pressed keys. .SS sizeunits (string) (default \f[CR]binary\f[R]) Determines whether file sizes are displayed using binary units (\f[CR]1K\f[R] is 1024 bytes) or decimal units (\f[CR]1K\f[R] is 1000 bytes). .SS smartcase (bool) (default true) Override \f[CR]ignorecase\f[R] option when the pattern contains an uppercase character. This option has no effect when \f[CR]ignorecase\f[R] is disabled. .SS smartdia (bool) (default false) Override \f[CR]ignoredia\f[R] option when the pattern contains a character with diacritic. This option has no effect when \f[CR]ignoredia\f[R] is disabled. .SS sortby (string) (default \f[CR]natural\f[R]) Sort type for directories. .PP The following sort types are supported: .IP .EX natural file name (track_2.flac comes before track_10.flac) name file name (track_10.flac comes before track_2.flac) ext file extension size file size time time of last data modification atime time of last access btime time of file birth ctime time of last status (inode) change custom property defined via \(gaaddcustominfo\(ga (empty by default) .EE .SS statfmt (string) (default \f[CR]\(rs033[36m%p\(rs033[0m| %c| %u| %g| %S| %t| \-> %l\f[R]) Format string of the file info shown in the bottom left corner. This option has no effect unless \f[CR]rulerfmt\f[R] is also set. Using \f[CR]rulerfile\f[R] is preferred and this option is provided for backwards compatibility. .PP The following special expansions are supported: .IP .EX %p file permission %c link count %u user name %g group name %s file size %S file size (left\-padded with spaces to a fixed width of 5 characters) %t time of last data modification %l link target %m current mode %M current mode (displaying \(gaNORMAL\(ga instead of a blank string in Normal mode) .EE .PP The \f[CR]|\f[R] character splits the format string into sections. Any section containing a failed expansion (result is a blank string) is discarded and not shown. .SS tabstop (int) (default 8) Number of space characters to show for horizontal tabulation (U+0009) character. .SS tagfmt (string) (default \f[CR]\(rs033[31m\f[R]) Format string of the tags. .PP If the format string contains the characters \f[CR]%s\f[R], it is interpreted as a format string for \f[CR]fmt.Sprintf\f[R]. Such a string should end with the terminal reset sequence. For example, \f[CR]\(rs033[4m%s\(rs033[0m\f[R] has the same effect as \f[CR]\(rs033[4m\f[R]. .SS tempmarks (string) (default \(ga\(ga) Marks to be considered temporary (e.g. \f[CR]abc\f[R] refers to marks \f[CR]a\f[R], \f[CR]b\f[R], and \f[CR]c\f[R]). These marks are not synced to other clients and they are not saved in the bookmarks file. Note that the special bookmark \f[CR]\(aq\f[R] is always treated as temporary and it does not need to be specified. .SS terminalcursor (string) (default \f[CR]default\f[R]) Set the appearance of the terminal cursor for prompts shown in the bottom line. Currently supported values are \f[CR]default\f[R], \f[CR]block\f[R], \f[CR]underline\f[R], \f[CR]bar\f[R], \f[CR]blinkblock\f[R], \f[CR]blinkunderline\f[R] and \f[CR]blinkbar\f[R]. .SS timefmt (string) (default \f[CR]Mon Jan _2 15:04:05 2006\f[R]) Format string of the file modification time shown in the bottom line. .SS truncatechar (string) (default \f[CR]\(ti\f[R]) The truncate character that is shown at the end when the filename does not fit into the pane. .SS truncatepct (int) (default 100) When a filename is too long to be shown completely, the available space will be partitioned into two parts. \f[CR]truncatepct\f[R] is a percentage value between 0 and 100 that determines the size of the first part, which will be shown at the beginning of the filename. The second part uses the rest of the available space, and will be shown at the end of the filename. Both parts are separated by the truncation character (\f[CR]truncatechar\f[R]). Truncation is not applied to the file extension. .PP For example, with the filename \f[CR]very_long_filename.txt\f[R]: .IP \(bu 2 \f[CR]set truncatepct 100\f[R] \-> \f[CR]very_long_filena\(ti.txt\f[R] (default) .IP \(bu 2 \f[CR]set truncatepct 50\f[R] \-> \f[CR]very_lon\(tifilename.txt\f[R] .IP \(bu 2 \f[CR]set truncatepct 0\f[R] \-> \f[CR]\(tiry_long_filename.txt\f[R] .SS visualfmt (string) (default \f[CR]\(rs033[7;36m\f[R]) Format string of the indicator for files that are visually selected. .SS waitmsg (string) (default \f[CR]Press any key to continue\f[R]) String shown after commands of shell\-wait type. .SS watch (bool) (default false) Watch the filesystem for changes using \f[CR]fsnotify\f[R] to automatically refresh file information. FUSE is currently not supported due to limitations in \f[CR]fsnotify\f[R]. .SS wrapscan (bool) (default true) Searching can wrap around the file list. .SS wrapscroll (bool) (default false) Scrolling can wrap around the file list. .SS user_{option} (string) (default none) Any option that is prefixed with \f[CR]user_\f[R] is a user\-defined option and can be set to any string. Inside a user\-defined command, the value will be provided in the \f[CR]lf_user_{option}\f[R] environment variable. These options are not used by lf and are not persisted. .SH ENVIRONMENT VARIABLES The following variables are exported for shell commands: These are referred to with a \f[CR]$\f[R] prefix on POSIX shells (e.g. \f[CR]$f\f[R]), between \f[CR]%\f[R] characters on Windows cmd (e.g. \f[CR]%f%\f[R]), and with a \f[CR]$env:\f[R] prefix on Windows PowerShell (e.g. \f[CR]$env:f\f[R]). .SS f Current file selection as a full path. .SS fs Selected file(s) separated with the value of \f[CR]filesep\f[R] option as full path(s). .SS fv Visually selected file(s) separated with the value of \f[CR]filesep\f[R] option as full path(s). .SS fx Selected file(s) (i.e. \f[CR]fs\f[R], never \f[CR]fv\f[R]) if there are any selected files, otherwise current file selection (i.e. \f[CR]f\f[R]). .SS id Id of the running client. .SS PWD Present working directory. .SS OLDPWD Initial working directory. .SS LF_LEVEL The value of this variable is set to the current nesting level when you run lf from a shell spawned inside lf. You can add the value of this variable to your shell prompt to make it clear that your shell runs inside lf. For example, with POSIX shells, you can use \f[CR][ \-n \(dq$LF_LEVEL\(dq ] && PS1=\(dq$PS1\(dq\(dq(lf level: $LF_LEVEL) \(dq\f[R] in your shell configuration file (e.g. \f[CR]\(ti/.bashrc\f[R]). .SS OPENER If this variable is set in the environment, use the same value. Otherwise, this is set to \f[CR]start\f[R] in Windows, \f[CR]open\f[R] in macOS, \f[CR]xdg\-open\f[R] in others. .SS EDITOR If VISUAL is set in the environment, use its value. Otherwise, use the value of the environment variable EDITOR. If neither variable is set, this is set to \f[CR]vi\f[R] on Unix, \f[CR]notepad\f[R] in Windows. .SS PAGER If this variable is set in the environment, use the same value. Otherwise, this is set to \f[CR]less\f[R] on Unix, \f[CR]more\f[R] in Windows. .SS SHELL If this variable is set in the environment, use the same value. Otherwise, this is set to \f[CR]sh\f[R] on Unix, \f[CR]cmd\f[R] in Windows. .SS lf Absolute path to the currently running lf binary, if it can be found. Otherwise, this is set to the string \f[CR]lf\f[R]. .SS lf_{option} Value of the {option}. .SS lf_user_{option} Value of the user_{option}. .SS lf_flag_{flag} Value of the command line {flag}. .SS lf_width, lf_height Width/Height of the terminal. .SS lf_count Value of the count associated with the current command. .SS lf_mode Current mode that \f[CR]lf\f[R] is operating in. This is useful for customizing keybindings depending on what the current mode is. Possible values are \f[CR]compmenu\f[R], \f[CR]delete\f[R], \f[CR]rename\f[R], \f[CR]filter\f[R], \f[CR]find\f[R], \f[CR]mark\f[R], \f[CR]search\f[R], \f[CR]command\f[R], \f[CR]shell\f[R], \f[CR]pipe\f[R] (when running a shell\-pipe command), \f[CR]normal\f[R], \f[CR]visual\f[R] and \f[CR]unknown\f[R]. .SH SPECIAL COMMANDS This section shows information about special shell commands. .SS open This shell command can be defined to override the default \f[CR]open\f[R] command when the current file is not a directory. .SS paste This shell command can be defined to override the default \f[CR]paste\f[R] command. .SS rename This shell command can be defined to override the default \f[CR]rename\f[R] command. .SS delete This shell command can be defined to override the default \f[CR]delete\f[R] command. .SS pre\-cd This shell command can be defined to be executed before changing a directory. .SS on\-cd This shell command can be defined to be executed after changing a directory. .SS on\-load This shell command can be defined to be executed after loading a directory. It provides the files inside the directory as arguments. .SS on\-focus\-gained This shell command can be defined to be executed when the terminal gains focus. .SS on\-focus\-lost This shell command can be defined to be executed when the terminal loses focus. .SS on\-init This shell command can be defined to be executed after initializing and connecting to the server. .SS on\-select This shell command can be defined to be executed after the selection changes. .SS on\-redraw This shell command can be defined to be executed after the screen is redrawn or if the terminal is resized. .SS on\-quit This shell command can be defined to be executed before quitting. .SH PREFIXES The following command prefixes are used by lf: .IP .EX : read (default) built\-in/custom command $ shell shell command % shell\-pipe shell command running with the UI ! shell\-wait shell command waiting for a key press & shell\-async shell command running asynchronously .EE .PP The same evaluator is used for the command line and the configuration file for reading shell commands. The difference is that prefixes are not necessary in the command line. Instead, different modes are provided to read corresponding commands. These modes are mapped to the prefix keys above by default. Visual mode mappings are defined the same way Normal mode mappings are defined. .SH SYNTAX Characters from \f[CR]#\f[R] to newline are comments and ignored: .IP .EX # comments start with \(ga#\(ga .EE .PP The following commands (\f[CR]set\f[R], \f[CR]setlocal\f[R], \f[CR]map\f[R], \f[CR]nmap\f[R], \f[CR]vmap\f[R], \f[CR]cmap\f[R], and \f[CR]cmd\f[R]) are used for configuration. .PP Command \f[CR]set\f[R] is used to set an option which can be a boolean, integer, or string: .IP .EX set hidden # boolean enable set hidden true # boolean enable set nohidden # boolean disable set hidden false # boolean disable set hidden! # boolean toggle set scrolloff 10 # integer value set sortby time # string value without quotes set sortby \(aqtime\(aq # string value with single quotes (whitespace) set sortby \(dqtime\(dq # string value with double quotes (backslash escapes) .EE .PP Command \f[CR]setlocal\f[R] is used to set a local option for a directory which can be a boolean or string. Currently supported local options are \f[CR]dircounts\f[R], \f[CR]dirfirst\f[R], \f[CR]dironly\f[R], \f[CR]hidden\f[R], \f[CR]info\f[R], \f[CR]reverse\f[R] and \f[CR]sortby\f[R]. .IP .EX setlocal /foo/bar hidden # boolean enable setlocal /foo/bar hidden true # boolean enable setlocal /foo/bar nohidden # boolean disable setlocal /foo/bar hidden false # boolean disable setlocal /foo/bar hidden! # boolean toggle setlocal /foo/bar sortby time # string value without quotes setlocal /foo/bar sortby \(aqtime\(aq # string value with single quotes (whitespace) setlocal /foo/bar sortby \(dqtime\(dq # string value with double quotes (backslash escapes) .EE .PP Command \f[CR]map\f[R] is used to bind a key in Normal and Visual mode to a command which can be a built\-in command, custom command, or shell command: .IP .EX map gh cd \(ti # built\-in command map D trash # custom command map i $less $f # shell command map U !du \-csh * # waiting shell command .EE .PP Command \f[CR]nmap\f[R] does the same but for Normal mode only. .PP Command \f[CR]vmap\f[R] does the same but for Visual mode only. .PP Overview of which map command works in which mode: .IP .EX map Normal, Visual nmap Normal vmap Visual cmap Command\-line .EE .PP Command \f[CR]cmap\f[R] is used to bind a key on the command line to a command line command or any other command: .IP .EX cmap cmd\-escape cmap set incsearch! .EE .PP You can delete an existing binding by leaving the expression empty: .IP .EX map gh # deletes \(aqgh\(aq mapping in Normal and Visual mode nmap v # deletes \(aqv\(aq mapping in Normal mode vmap o # deletes \(aqo\(aq mapping in Visual mode cmap # deletes \(aq\(aq mapping .EE .PP Command \f[CR]cmd\f[R] is used to define a custom command: .IP .EX cmd usage $du \-h \-d1 | less .EE .PP You can delete an existing command by leaving the expression empty: .IP .EX cmd trash # deletes \(aqtrash\(aq command .EE .PP If there is no prefix then \f[CR]:\f[R] is assumed: .IP .EX map zt set info time .EE .PP An explicit \f[CR]:\f[R] can be provided to group statements until a newline which is especially useful for \f[CR]map\f[R] and \f[CR]cmd\f[R] commands: .IP .EX map st :set sortby time; set info time .EE .PP If you need multiline you can wrap statements in \f[CR]{{\f[R] and \f[CR]}}\f[R] after the proper prefix. .IP .EX map st :{{ set sortby time set info time }} .EE .SH KEY MAPPINGS Regular keys are assigned to a command with the usual syntax: .IP .EX map a down .EE .PP Keys combined with the Shift key simply use the uppercase letter: .IP .EX map A down .EE .PP Special keys are written in between \f[CR]<\f[R] and \f[CR]>\f[R] characters and always use lowercase letters: .IP .EX map down .EE .PP Angle brackets can be assigned with their special names: .IP .EX map down map down .EE .PP Function keys are prefixed with an \f[CR]f\f[R] character: .IP .EX map down .EE .PP Keys combined with the Ctrl key are prefixed with a \f[CR]c\f[R] character: .IP .EX map down .EE .PP Keys combined with the Alt key are assigned in two different ways depending on the behavior of your terminal. Older terminals (e.g. xterm) may set the 8th bit of a character when the Alt key is pressed. On these terminals, you can use the corresponding byte for the mapping: .IP .EX map á down .EE .PP Newer terminals (e.g. gnome\-terminal) may prefix the key with an escape character when the Alt key is pressed. lf uses the escape delaying mechanism to recognize Alt keys in these terminals (delay is 100ms). On these terminals, keys combined with the Alt key are prefixed with an \f[CR]a\f[R] character: .IP .EX map down .EE .PP It is possible to combine special keys with modifiers: .IP .EX map down .EE .PP Combining multiple modifiers (e.g. \f[CR]Ctrl+Shift+Space\f[R]) is not supported. .PP Note that lf\(aqs key mapping syntax is similar to Vim\(aqs, but not identical. Some special keys and modifiers use different names and separators, and key names are matched literally (i.e. no case\-folding, no aliases), so some familiar forms will not work: .IP .EX map down # not , or map down # not map down # not or (Meta) map down # not map down # not .EE .PP WARNING: Some key combinations will likely be intercepted by your OS, window manager, or terminal. Other key combinations cannot be recognized by lf due to the way terminals work (e.g. \f[CR]Ctrl+h\f[R] combination sends a backspace key instead). The easiest way to find out the name of a key combination and whether it will work on your system is to press the key while lf is running and read the name from the \f[CR]unknown mapping\f[R] error. .PP Mouse buttons are prefixed with an \f[CR]m\f[R] character: .IP .EX map down # primary map down # secondary map down # middle map down # thumb next map down # thumb prev map down map down map down .EE .PP Mouse wheel events are also prefixed with an \f[CR]m\f[R] character: .IP .EX map down map down map down map down .EE .SH PUSH MAPPINGS The usual way to map a key sequence is to assign it to a named or unnamed command. While this provides a clean way to remap built\-in keys as well as other commands, it can be limiting at times. For this reason, the \f[CR]push\f[R] command is provided by lf. This command is used to simulate key pushes given as its arguments. You can \f[CR]map\f[R] a key to a \f[CR]push\f[R] command with an argument to create various keybindings. .PP This is mainly useful for two purposes. First, it can be used to map a command with a command count: .IP .EX map push 10j .EE .PP Second, it can be used to avoid typing the name when a command takes arguments: .IP .EX map r push :rename .EE .PP One thing to be careful of is that since the \f[CR]push\f[R] command works with keys instead of commands it is possible to accidentally create recursive bindings: .IP .EX map j push 2j .EE .PP These types of bindings create a deadlock when executed. .SH SHELL COMMANDS Regular shell commands are the most basic command type that is useful for many purposes. For example, we can write a shell command to move the selected file(s) to trash. A first attempt to write such a command may look like this: .IP .EX cmd trash ${{ mkdir \-p \(ti/.trash if [ \-z \(dq$fs\(dq ]; then mv \(dq$f\(dq \(ti/.trash else IFS=\(dq$(printf \(aq\(rsn\(rst\(aq)\(dq; mv $fs \(ti/.trash fi }} .EE .PP We check \f[CR]$fs\f[R] to see if there are any selected files. Otherwise, we just delete the current file. Since this is such a common pattern, a separate \f[CR]$fx\f[R] variable is provided. We can use this variable to get rid of the conditional: .IP .EX cmd trash ${{ mkdir \-p \(ti/.trash IFS=\(dq$(printf \(aq\(rsn\(rst\(aq)\(dq; mv $fx \(ti/.trash }} .EE .PP The trash directory is checked each time the command is executed. We can move it outside of the command so it would only run once at startup: .IP .EX ${{ mkdir \-p \(ti/.trash }} cmd trash ${{ IFS=\(dq$(printf \(aq\(rsn\(rst\(aq)\(dq; mv $fx \(ti/.trash }} .EE .PP Since these are one\-liners, we can drop \f[CR]{{\f[R] and \f[CR]}}\f[R]: .IP .EX $mkdir \-p \(ti/.trash cmd trash $IFS=\(dq$(printf \(aq\(rsn\(rst\(aq)\(dq; mv $fx \(ti/.trash .EE .PP Finally, note that we set the \f[CR]IFS\f[R] variable manually in these commands. Instead, we could use the \f[CR]ifs\f[R] option to set it for all shell commands (i.e. \f[CR]set ifs \(dq\(rsn\(dq\f[R]). This can be especially useful for interactive use (e.g. \f[CR]$rm $f\f[R] or \f[CR]$rm $fs\f[R] would simply work). This option is not set by default as it can behave unexpectedly for new users. However, use of this option is highly recommended and it is assumed in the rest of the documentation. .SH PIPING SHELL COMMANDS Regular shell commands have some limitations in some cases. When an output or error message is given and the command exits afterwards, the UI is immediately resumed and there is no way to see the message without dropping to shell again. Also, even when there is no output or error, the UI still needs to be paused while the command is running. This can cause flickering on the screen for short commands and similar distractions for longer commands. .PP Instead of pausing the UI, piping shell commands connect stdin, stdout, and stderr of the command to the statline at the bottom of the UI. This can be useful for programs following the Unix philosophy to give no output in the success case, and brief error messages or prompts in other cases. .PP For example, the following rename command prompts for overwrite in the statline if there is an existing file with the given name: .IP .EX cmd rename %mv \-i $f $1 .EE .PP You can also output error messages in the command and they will show up in the statline. For example, an alternative rename command may look like this: .IP .EX cmd rename %[ \-e $1 ] && printf \(dqfile exists\(dq || mv $f $1 .EE .PP Note that input is line buffered and output and error are byte buffered. .SH WAITING SHELL COMMANDS Waiting shell commands are similar to regular shell commands except that they wait for a key press when the command is finished. These can be useful to see the output of a program before the UI is resumed. Waiting shell commands are more appropriate than piping shell commands when the command is verbose and the output is best displayed as multiline. .SH ASYNCHRONOUS SHELL COMMANDS Asynchronous shell commands are used to start a command in the background and then resume operation without waiting for the command to finish. Stdin, stdout, and stderr of the command are neither connected to the terminal nor the UI. .SH REMOTE COMMANDS One of the more advanced features in lf is remote commands. All clients connect to a server on startup. It is possible to send commands to all or any of the connected clients over the common server. This is used internally to notify file selection changes to other clients. .PP To use this feature, you need to use a client which supports communicating with a Unix domain socket. OpenBSD implementation of netcat (nc) is one such example. You can use it to send a command to the socket file: .IP .EX echo \(aqsend echo hello world\(aq | nc \-U ${XDG_RUNTIME_DIR:\-/tmp}/lf.${USER}.sock .EE .PP Since such a client may not be available everywhere, lf comes bundled with a command line flag to be used as such. When using lf, you do not need to specify the address of the socket file. This is the recommended way of using remote commands since it is shorter and immune to socket file address changes: .IP .EX lf \-remote \(aqsend echo hello world\(aq .EE .PP In this command \f[CR]send\f[R] is used to send the rest of the string as a command to all connected clients. You can optionally give it an ID number to send a command to a single client: .IP .EX lf \-remote \(aqsend 1234 echo hello world\(aq .EE .PP All clients have a unique ID number but you may not be aware of the ID number when you are writing a command. For this purpose, an \f[CR]$id\f[R] variable is exported to the environment for shell commands. The value of this variable is set to the process ID of the client. You can use it to send a remote command from a client to the server which in return sends a command back to itself. So now you can display a message in the current client by calling the following in a shell command: .IP .EX lf \-remote \(dqsend $id echo hello world\(dq .EE .PP Since lf does not have control flow syntax, remote commands are used for such needs. For example, you can configure the number of columns in the UI with respect to the terminal width as follows: .IP .EX cmd recol %{{ if [ $lf_width \-le 80 ]; then lf \-remote \(dqsend $id set ratios 1:2\(dq elif [ $lf_width \-le 160 ]; then lf \-remote \(dqsend $id set ratios 1:2:3\(dq else lf \-remote \(dqsend $id set ratios 1:2:3:5\(dq fi }} .EE .PP In addition, the \f[CR]query\f[R] command can be used to obtain information about a specific lf instance by providing its ID: .IP .EX lf \-remote \(dqquery $id maps\(dq .EE .PP The following types of information are supported: .IP .EX maps list of mappings created by the \(aqmap\(aq, \(aqnmap\(aq and \(aqvmap\(aq command nmaps list of mappings created by the \(aqnmap\(aq and \(aqmap\(aq command vmaps list of mappings created by the \(aqvmap\(aq and \(aqmap\(aq command cmaps list of mappings created by the \(aqcmap\(aq command cmds list of commands created by the \(aqcmd\(aq command jumps contents of the jump list, showing previously visited locations history list of previously executed commands on the command line files list of files in the currently open directory as displayed by lf, empty if dir is still loading .EE .PP When listing mappings the characters in the first column are: .IP .EX n Normal v Visual c Command\-line .EE .PP This is useful for scripting actions based on the internal state of lf. For example, to select a previous command using fzf and execute it: .IP .EX map ${{ clear cmd=$( lf \-remote \(dqquery $id history\(dq | awk \-F\(aq\(rst\(aq \(aqNR > 1 { print $NF}\(aq | sort \-u | fzf \-\-reverse \-\-prompt=\(aqExecute command: \(aq ) lf \-remote \(dqsend $id $cmd\(dq }} .EE .PP The \f[CR]list\f[R] command prints the IDs of all currently connected clients: .IP .EX lf \-remote \(aqlist\(aq .EE .PP There is also a \f[CR]quit\f[R] command to quit the server when there are no connected clients left, and a \f[CR]quit!\f[R] command to force quit the server by closing client connections first: .IP .EX lf \-remote \(aqquit\(aq lf \-remote \(aqquit!\(aq .EE .PP Lastly, the commands \f[CR]conn\f[R] and \f[CR]drop\f[R] connect or disconnect ID to/from the server: .IP .EX lf \-remote \(aqconn $id\(aq lf \-remote \(aqdrop $id\(aq .EE .PP These are internal and generally not needed by users. .SH FILE OPERATIONS lf uses its own built\-in copy and move operations by default. These are implemented as asynchronous operations and progress is shown in the bottom ruler. These commands do not overwrite existing files or directories with the same name. Instead, a suffix that is compatible with the \f[CR]\-\-backup=numbered\f[R] option in GNU cp is added to the new files or directories. Only file modes and (some) timestamps can be preserved (see \f[CR]preserve\f[R] option), all other attributes are ignored including ownership, context, and xattr. Special files such as character and block devices, named pipes, and sockets are skipped and links are not followed. Moving is performed using the rename operation of the underlying OS. For cross\-device moving, lf falls back to copying and then deletes the original files if there are no errors. Operation errors are shown in the message line as well as the log file and they do not prematurely terminate the corresponding file operation. .PP File operations can be performed on the currently selected file or on multiple files by selecting them first. When you \f[CR]copy\f[R] a file, lf doesn\(aqt actually copy the file on the disk, but only records its name to a file. The actual file copying takes place when you \f[CR]paste\f[R]. Similarly \f[CR]paste\f[R] after a \f[CR]cut\f[R] operation moves the file. .PP You can customize copy and move operations by defining a \f[CR]paste\f[R] command. This is a special command that is called when it is defined instead of the built\-in implementation. You can use the following example as a starting point: .IP .EX cmd paste %{{ load=$(cat \(ti/.local/share/lf/files) mode=$(echo \(dq$load\(dq | sed \-n \(aq1p\(aq) list=$(echo \(dq$load\(dq | sed \(aq1d\(aq) if [ $mode = \(aqcopy\(aq ]; then cp \-R $list . elif [ $mode = \(aqmove\(aq ]; then mv $list . rm \(ti/.local/share/lf/files lf \-remote \(aqsend clear\(aq fi }} .EE .PP Some useful things to be considered are to use the backup (\f[CR]\-\-backup\f[R]) and/or preserve attributes (\f[CR]\-a\f[R]) options with \f[CR]cp\f[R] and \f[CR]mv\f[R] commands if they support it (i.e. GNU implementation), change the command type to asynchronous, or use \f[CR]rsync\f[R] command with progress bar option for copying and feed the progress to the client periodically with remote \f[CR]echo\f[R] calls. .PP By default, lf does not assign \f[CR]delete\f[R] command to a key to protect new users. You can customize file deletion by defining a \f[CR]delete\f[R] command. You can also assign a key to this command if you like. An example command to move selected files to a trash folder and remove files completely after a prompt is provided in the example configuration file. .SH SEARCHING FILES There are two mechanisms implemented in lf to search a file in the current directory. Searching is the traditional method to move the selection to a file matching a given pattern. Finding is an alternative way to search for a pattern possibly using fewer keystrokes. .PP The searching mechanism is implemented with commands \f[CR]search\f[R] (default \f[CR]/\f[R]), \f[CR]search\-back\f[R] (default \f[CR]?\f[R]), \f[CR]search\-next\f[R] (default \f[CR]n\f[R]), and \f[CR]search\-prev\f[R] (default \f[CR]N\f[R]). You can set \f[CR]searchmethod\f[R] to \f[CR]glob\f[R] to match using a glob pattern. Globbing supports \f[CR]*\f[R] to match any sequence, \f[CR]?\f[R] to match any character, and \f[CR][...]\f[R] or \f[CR][\(ha...]\f[R] to match character sets or ranges. You can set \f[CR]searchmethod\f[R] to \f[CR]regex\f[R] to match using a regex pattern. For a full overview of Go\(aqs RE2 syntax, see \c .UR https://pkg.go.dev/regexp/syntax .UE \c \&. You can enable \f[CR]incsearch\f[R] option to jump to the current match at each keystroke while typing. In this mode, you can either use \f[CR]cmd\-enter\f[R] to accept the search or use \f[CR]cmd\-escape\f[R] to cancel the search. You can also map some other commands with \f[CR]cmap\f[R] to accept the search and execute the command immediately afterwards. For example, you can use the right arrow key to finish the search and open the selected file with the following mapping: .IP .EX cmap :cmd\-enter; open .EE .PP The finding mechanism is implemented with commands \f[CR]find\f[R] (default \f[CR]f\f[R]), \f[CR]find\-back\f[R] (default \f[CR]F\f[R]), \f[CR]find\-next\f[R] (default \f[CR];\f[R]), \f[CR]find\-prev\f[R] (default \f[CR],\f[R]). You can disable \f[CR]anchorfind\f[R] option to match a pattern at an arbitrary position in the filename instead of the beginning. You can set the number of keys to match using \f[CR]findlen\f[R] option. If you set this value to zero, then the keys are read until there is only a single match. The default values of these two options are set to jump to the first file with the given initial. .PP Some options affect both searching and finding. You can disable \f[CR]wrapscan\f[R] option to prevent searches from being wrapped around at the end of the file list. You can disable \f[CR]ignorecase\f[R] option to match cases in the pattern and the filename. This option is already automatically overridden if the pattern contains uppercase characters. You can disable \f[CR]smartcase\f[R] option to disable this behavior. Two similar options \f[CR]ignoredia\f[R] and \f[CR]smartdia\f[R] are provided to control matching diacritics in Latin letters. .SH OPENING FILES You can define an \f[CR]open\f[R] command (default \f[CR]l\f[R] and \f[CR]\f[R]) to configure file opening. This command is only called when the current file is not a directory, otherwise, the directory is entered instead. You can define it just as you would define any other command: .IP .EX cmd open $vi $fx .EE .PP It is possible to use different command types: .IP .EX cmd open &xdg\-open $f .EE .PP You may want to use either file extensions or MIME types from \f[CR]file\f[R] command: .IP .EX cmd open ${{ case $(file \-\-mime\-type \-Lb $f) in text/*) vi $fx;; *) for f in $fx; do xdg\-open $f > /dev/null 2> /dev/null & done;; esac }} .EE .PP You may want to use \f[CR]setsid\f[R] before your opener command to have persistent processes that continue to run after lf quits. .PP Regular shell commands (i.e. \f[CR]$\f[R]) drop to the terminal which results in a flicker for commands that finish immediately (e.g. \f[CR]xdg\-open\f[R] in the above example). If you want to use asynchronous shell commands (i.e. \f[CR]&\f[R]) but also want to use the terminal when necessary (e.g. \f[CR]vi\f[R] in the above example), you can use a remote command: .IP .EX cmd open &{{ case $(file \-\-mime\-type \-Lb $f) in text/*) lf \-remote \(dqsend $id \(rs$vi \(rs$fx\(dq;; *) for f in $fx; do xdg\-open $f > /dev/null 2> /dev/null & done;; esac }} .EE .PP Note that asynchronous shell commands run in their own process group by default so they do not require the manual use of \f[CR]setsid\f[R]. .PP The following command is provided by default: .IP .EX cmd open &$OPENER $f .EE .PP You may also use any other existing file openers as you like. Possible options are \f[CR]libfile\-mimeinfo\-perl\f[R] (executable name is \f[CR]mimeopen\f[R]), \f[CR]rifle\f[R] (ranger\(aqs default file opener), or \f[CR]mimeo\f[R] to name a few. .SH PREVIEWING FILES lf previews files on the preview pane by printing the file until the end or until the preview pane is filled. This output can be enhanced by providing a custom preview script for filtering. This can be used to highlight source code, list contents of archive files or view PDF or image files to name a few. For coloring lf recognizes ANSI escape codes. .PP To use this feature, you need to set the value of \f[CR]previewer\f[R] option to the path of an executable file. The following arguments are passed to the file, (1) current filename, (2) width, (3) height, (4) horizontal position, (5) vertical position, and (6) mode (\(dqpreview\(dq or \(dqpreload\(dq). The output of the execution is printed in the preview pane. .PP Different types of files can be handled by matching by extension (or MIME type from the \f[CR]file\f[R] command): .IP .EX #!/bin/sh case \(dq$1\(dq in *.tar*) tar tf \(dq$1\(dq;; *.zip) unzip \-l \(dq$1\(dq;; *.rar) unrar l \(dq$1\(dq;; *.7z) 7z l \(dq$1\(dq;; *.pdf) pdftotext \(dq$1\(dq \-;; *) highlight \-O ansi \(dq$1\(dq;; esac .EE .PP Because files can be large, lf automatically closes the previewer script output pipe with a SIGPIPE when enough lines are read. Note that some programs may not respond well to SIGPIPE and will exit with a non\-zero return code, which avoids caching. You may add a trailing \f[CR]|| true\f[R] command to avoid such errors: .IP .EX highlight \-O ansi \(dq$1\(dq || true .EE .PP You may also want to use the same script in your pager mapping as well: .IP .EX set previewer \(ti/.config/lf/pv.sh map i $\(ti/.config/lf/pv.sh $f | less \-R .EE .PP For \f[CR]less\f[R] pager, you may instead utilize \f[CR]LESSOPEN\f[R] mechanism so that useful information about the file such as the full path of the file can still be displayed in the statusline below: .IP .EX set previewer \(ti/.config/lf/pv.sh map i $LESSOPEN=\(aq| \(ti/.config/lf/pv.sh %s\(aq less \-R $f .EE .PP Since the preview script is called for each file selection change, it may not generate previews fast enough if the user scrolls through files quickly. To deal with this, the \f[CR]preload\f[R] option can be set to enable file previews to be preloaded in advance. If enabled, the preview script will be run on files in advance as the user navigates through them. In this case, if the exit code of the preview script is zero, then the output will be cached in memory and displayed by lf (useful for text or sixel previews). Otherwise, it will fallback to calling the preview script again when the file is actually selected (useful for previews managed by an external program). .SH CHANGING DIRECTORY lf changes the working directory of the process to the current directory so that shell commands always work in the displayed directory. After quitting, it returns to the original directory where it is first launched like all shell programs. If you want to stay in the current directory after quitting, you can use one of the example lfcd wrapper shell scripts provided in the repository at \c .UR https://github.com/gokcehan/lf/tree/master/etc .UE \c .PP There is a special command \f[CR]on\-cd\f[R] that runs a shell command when it is defined and the directory is changed. You can define it just as you would define any other command: .IP .EX cmd on\-cd &{{ bash \-c \(aq # display git repository status in your prompt source /usr/share/git/completion/git\-prompt.sh GIT_PS1_SHOWDIRTYSTATE=auto GIT_PS1_SHOWSTASHSTATE=auto GIT_PS1_SHOWUNTRACKEDFILES=auto GIT_PS1_SHOWUPSTREAM=auto git=$(__git_ps1 \(dq (%s)\(dq) fmt=\(dq\(rs033[32;1m%u\(at%h\(rs033[0m:\(rs033[34;1m%d\(rs033[0m\(rs033[1m%f$git\(rs033[0m\(dq lf \-remote \(dqsend $id set promptfmt \(rs\(dq$fmt\(rs\(dq\(dq \(aq }} .EE .PP If you want to send escape sequences to the terminal, you can use the \f[CR]tty\-write\f[R] command to do so. The following xterm\-specific escape sequence sets the terminal title to the working directory: .IP .EX cmd on\-cd &{{ lf \-remote \(dqsend $id tty\-write \(rs\(dq\(rs033]0;$PWD\(rs007\(rs\(dq\(dq }} .EE .PP This command runs whenever you change the directory but not on startup. You can add an extra call to make it run on startup as well: .IP .EX cmd on\-cd &{{ ... }} on\-cd .EE .PP Note that all shell commands are possible but \f[CR]%\f[R] and \f[CR]&\f[R] are usually more appropriate as \f[CR]$\f[R] and \f[CR]!\f[R] causes flickers and pauses respectively. .PP There is also a \f[CR]pre\-cd\f[R] command, that works like \f[CR]on\-cd\f[R], but is run before the directory is actually changed. Another related command is \f[CR]on\-load\f[R] which gets executed when loading a directory. .SH LOADING DIRECTORY Similar to \f[CR]on\-cd\f[R] there also is \f[CR]on\-load\f[R] that when defined runs a shell command after loading a directory. It works well when combined with \f[CR]addcustominfo\f[R]. .PP The following example can be used to display git indicators in the info column: .IP .EX cmd on\-load &{{ cd \(dq$(dirname \(dq$1\(dq)\(dq || exit 1 [ \(dq$(git rev\-parse \-\-is\-inside\-git\-dir 2>/dev/null)\(dq = false ] || exit 0 cmds=\(dq\(dq for file in \(dq$\(at\(dq; do case \(dq$file\(dq in */.git|*/.git/*) continue;; esac status=$(git status \-\-porcelain \-\-ignored \-\- \(dq$file\(dq | cut \-c1\-2 | head \-n1) if [ \-n \(dq$status\(dq ]; then cmds=\(dq${cmds}addcustominfo \(rs\(dq${file}\(rs\(dq \(rs\(dq$status\(rs\(dq; \(dq else cmds=\(dq${cmds}addcustominfo \(rs\(dq${file}\(rs\(dq \(aq\(aq; \(dq fi done if [ \-n \(dq$cmds\(dq ]; then lf \-remote \(dqsend $id :$cmds\(dq fi }} .EE .PP Another use case could be showing the dimensions of images and videos: .IP .EX cmd on\-load &{{ cmds=\(dq\(dq for file in \(dq$\(at\(dq; do mime=$(file \-\-mime\-type \-Lb \-\- \(dq$file\(dq) case \(dq$mime\(dq in # vector images cause problems image/svg+xml) ;; image/*|video/*) dimensions=$(exiftool \-s3 \-imagesize \-\- \(dq$file\(dq) cmds=\(dq${cmds}addcustominfo \(rs\(dq${file}\(rs\(dq \(rs\(dq$dimensions\(rs\(dq; \(dq ;; esac done if [ \-n \(dq$cmds\(dq ]; then lf \-remote \(dqsend $id :$cmds\(dq fi }} .EE .SH COLORS lf tries to automatically adapt its colors to the environment. It starts with a default color scheme and updates colors using values of existing environment variables possibly by overwriting its previous values. Colors are set in the following order: .IP "1." 3 default .IP "2." 3 LSCOLORS (macOS/BSD ls) .IP "3." 3 LS_COLORS (GNU ls) .IP "4." 3 LF_COLORS (lf specific) .IP "5." 3 colors file (lf specific) .PP Please refer to the corresponding man pages for more information about \f[CR]LSCOLORS\f[R] and \f[CR]LS_COLORS\f[R]. \f[CR]LF_COLORS\f[R] is provided with the same syntax as \f[CR]LS_COLORS\f[R] in case you want to configure colors only for lf but not ls. This can be useful since there are some differences between ls and lf, though one should expect the same behavior for common cases. The colors file (refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#configuration CONFIGURATION section .UE \c ) is provided for easier configuration without environment variables. This file should consist of whitespace\-separated pairs with a \f[CR]#\f[R] character to start comments until the end of the line. .PP You can configure lf colors in two different ways. First, you can only configure 8 basic colors used by your terminal and lf should pick up those colors automatically. Depending on your terminal, you should be able to select your colors from a 24\-bit palette. This is the recommended approach as colors used by other programs will also match each other. .PP Second, you can set the values of environment variables or colors file mentioned above for fine\-grained customization. Note that \f[CR]LS_COLORS/LF_COLORS\f[R] are more powerful than \f[CR]LSCOLORS\f[R] and they can be used even when GNU programs are not installed on the system. You can combine this second method with the first method for the best results. .PP Lastly, you may also want to configure the colors of the prompt line to match the rest of the colors. Colors of the prompt line can be configured using the \f[CR]promptfmt\f[R] option which can include hardcoded colors as ANSI escapes. See the default value of this option to have an idea about how to color this line. .PP It is worth noting that lf uses as many colors advertised by your terminal\(aqs entry in terminfo or infocmp databases on your system. If an entry is not present, it falls back to an internal database. If your terminal supports 24\-bit colors but either does not have a database entry or does not advertise all capabilities, you can enable support by setting the \f[CR]$COLORTERM\f[R] variable to \f[CR]truecolor\f[R] or ensuring \f[CR]$TERM\f[R] is set to a value that ends with \f[CR]\-truecolor\f[R]. .PP Default lf colors are mostly taken from GNU dircolors defaults. These defaults use 8 basic colors and bold attribute. Default dircolors entries with background colors are simplified to avoid confusion with current file selection in lf. Similarly, there are only file type matchings and extension matchings are left out for simplicity. Default values are as follows given with their matching order in lf: .IP .EX ln 01;36 or 31;01 tw 01;34 ow 01;34 st 01;34 di 01;34 pi 33 so 01;35 bd 33;01 cd 33;01 su 01;32 sg 01;32 ex 01;32 fi 00 .EE .PP Note that lf first tries matching file names and then falls back to file types. The full order of matchings from most specific to least are as follows: .IP "1." 3 Full Path (e.g. \f[CR]\(ti/.config/lf/lfrc\f[R]) .IP "2." 3 Dir Name (e.g. \f[CR].git/\f[R]) (only matches dirs with a trailing slash at the end) .IP "3." 3 File Type (e.g. \f[CR]ln\f[R]) (except \f[CR]fi\f[R]) .IP "4." 3 File Name (e.g. \f[CR]README*\f[R]) .IP "5." 3 File Name (e.g. \f[CR]*README\f[R]) .IP "6." 3 Base Name (e.g. \f[CR]README.*\f[R]) .IP "7." 3 Extension (e.g. \f[CR]*.txt\f[R]) .IP "8." 3 Default (i.e. \f[CR]fi\f[R]) .PP For example, given a regular text file \f[CR]/path/to/README.txt\f[R], the following entries are checked in the configuration and the first one to match is used: .IP "1." 3 \f[CR]/path/to/README.txt\f[R] .IP "2." 3 (skipped since the file is not a directory) .IP "3." 3 (skipped since the file is of type \f[CR]fi\f[R]) .IP "4." 3 \f[CR]README.txt*\f[R] .IP "5." 3 \f[CR]*README.txt\f[R] .IP "6." 3 \f[CR]README.*\f[R] .IP "7." 3 \f[CR]*.txt\f[R] .IP "8." 3 \f[CR]fi\f[R] .PP Given a regular directory \f[CR]/path/to/example.d\f[R], the following entries are checked in the configuration and the first one to match is used: .IP "1." 3 \f[CR]/path/to/example.d\f[R] .IP "2." 3 \f[CR]example.d/\f[R] .IP "3." 3 \f[CR]di\f[R] .IP "4." 3 \f[CR]example.d*\f[R] .IP "5." 3 \f[CR]*example.d\f[R] .IP "6." 3 \f[CR]example.*\f[R] .IP "7." 3 \f[CR]*.d\f[R] .IP "8." 3 \f[CR]fi\f[R] .PP Note that glob\-like patterns do not perform glob matching for performance reasons. .PP For example, you can set a variable as follows: .IP .EX export LF_COLORS=\(dq\(ti/Documents=01;31:\(ti/Downloads=01;31:\(ti/.local/share=01;31:\(ti/.config/lf/lfrc=31:.git/=01;32:.git*=32:*.gitignore=32:*Makefile=32:README.*=33:*.txt=34:*.md=34:ln=01;36:di=01;34:ex=01;32:\(dq .EE .PP Having all entries on a single line can make it hard to read. You may instead divide it into multiple lines in between double quotes by escaping newlines with backslashes as follows: .IP .EX export LF_COLORS=\(dq\(rs \(ti/Documents=01;31:\(rs \(ti/Downloads=01;31:\(rs \(ti/.local/share=01;31:\(rs \(ti/.config/lf/lfrc=31:\(rs \&.git/=01;32:\(rs \&.git*=32:\(rs *.gitignore=32:\(rs *Makefile=32:\(rs README.*=33:\(rs *.txt=34:\(rs *.md=34:\(rs ln=01;36:\(rs di=01;34:\(rs ex=01;32:\(rs \(dq .EE .PP The \f[CR]ln\f[R] entry supports the special value \f[CR]target\f[R], which will use the link target to select a style. Filename rules will still apply based on the link\(aqs name \-\- this mirrors GNU\(aqs \f[CR]ls\f[R] and \f[CR]dircolors\f[R] behavior. Having such a long variable definition in a shell configuration file might be undesirable. You may instead use the colors file (refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#configuration CONFIGURATION section .UE \c ) for configuration. A sample colors file can be found at \c .UR https://github.com/gokcehan/lf/blob/master/etc/colors.example .UE \c \ You may also see the wiki page for ANSI escape codes \c .UR https://en.wikipedia.org/wiki/ANSI_escape_code .UE \c .SH ICONS Icons are configured using \f[CR]LF_ICONS\f[R] environment variable or an icons file (refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#configuration CONFIGURATION section .UE \c ). The variable uses the same syntax as \f[CR]LS_COLORS/LF_COLORS\f[R]. Instead of colors, you should use single characters or symbols as values. The \f[CR]ln\f[R] entry supports the special value \f[CR]target\f[R], which will use the link target to select a icon. Filename rules will still apply based on the link\(aqs name \-\- this mirrors GNU\(aqs \f[CR]ls\f[R] and \f[CR]dircolors\f[R] behavior. The icons file (refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#configuration CONFIGURATION section .UE \c ) should consist of whitespace\-separated arrays with a \f[CR]#\f[R] character to start comments until the end of the line. Each line should contain 1\-3 columns: a file type or file name pattern, the icon, and an optional icon color. Using only one column disables all rules for that type or name. Do not forget to add \f[CR]set icons true\f[R] to your \f[CR]lfrc\f[R] to see the icons. Default values are listed below in the order lf matches them: .IP .EX ln l or l tw t ow d st t di d pi p so s bd b cd c su u sg g ex x fi \- .EE .PP A sample icons file can be found at \c .UR https://github.com/gokcehan/lf/blob/master/etc/icons.example .UE \c .PP A sample colored icons file can be found at \c .UR https://github.com/gokcehan/lf/blob/master/etc/icons_colored.example .UE \c .SH RULER The ruler can be configured using the \f[CR]rulerfile\f[R] option (refer to the \c .UR https://github.com/gokcehan/lf/blob/master/doc.md#configuration CONFIGURATION section .UE \c ). The contents of the ruler file should be a Go template which is then rendered to create the actual output (refer to \c .UR https://pkg.go.dev/text/template .UE \c \ for more details on the syntax). .PP The following data fields are exported: .IP .EX \&.Message string Includes internal messages, errors, and messages generated by the \(gaecho\(ga/\(gaechomsg\(ga/\(gaechoerr\(ga commands \&.Keys string Keys pressed by the user \&.Progress []string Progress indicators for copied, moved and deleted files \&.Copy []string List of files in the clipboard to be copied \&.Cut []string List of files in the clipboard to be moved \&.Select []string Selection list \&.Visual []string Visual selection \&.Index int Index of the cursor \&.Total int Number of visible files in the current working directory \&.Hidden int Number of hidden files in the current working directory \&.All int Number of all files in the current working directory \&.LinePercentage string Line percentage (analogous to \(ga%p\(ga for the \(gastatusline\(ga option in Vim) \&.ScrollPercentage string Scroll percentage (analogous to \(ga%P\(ga for the \(gastatusline\(ga option in Vim) \&.Filter []string Filter currently being applied \&.Mode string Current mode (\(dqNORMAL\(dq for Normal mode, and \(dqVISUAL\(dq for Visual mode) \&.Options map[string]string The value of options (e.g. \(ga{{.Options.hidden}}\(ga) \&.UserOptions map[string]string The value of user\-defined options (e.g. \(ga{{.UserOptions.foo}}\(ga) \&.Stat.Path string Path of the current file \&.Stat.Name string Name of the current file \&.Stat.Extension string Extension of the current file \&.Stat.Size int64 Size of the current file \&.Stat.DirSize int64 Total size of the current directory if calculated via \(gacalcdirsize\(ga (\(ga\-1\(ga if not calculated) \&.Stat.DirCount int Number of items in the current directory if the \(gadircounts\(ga option is enabled (\(ga\-1\(ga if the directory cannot be read) \&.Stat.Permissions string Permissions of the current file \&.Stat.ModTime string Last modified time of the current file (formatted based on the \(gatimefmt\(ga option) \&.Stat.AccessTime string Last access time of the current file (formatted based on the \(gatimefmt\(ga option) \&.Stat.BirthTime string Birth time of the current file (formatted based on the \(gatimefmt\(ga option) \&.Stat.ChangeTime string Last status (inode) change time of the current file (formatted based on the \(gatimefmt\(ga option) \&.Stat.LinkCount string Number of hard links for the current file \&.Stat.User string User of the current file \&.Stat.Group string Group of the current file \&.Stat.Target string Target if the current file is a symbolic link, otherwise a blank string \&.Stat.CustomInfo string Custom property if defined via \(gaaddcustominfo\(ga, otherwise a blank string .EE .PP The following functions are exported: .IP .EX df func() string Get an indicator representing the amount of free disk space available env func(string) string Get the value of an environment variable humanize func(int64) string Express a file size in a human\-readable format join func([]string, string) string Join a string array by a separator lower func(string) string Convert a string to lowercase substr func(string, int, int) string Get a substring based on starting index and length upper func(string) string Convert a string to uppercase .EE .PP The special identifier \f[CR]{{.SPACER}}\f[R] can be used to divide the ruler into sections that are spaced evenly from each other. .PP The default ruler file can be found at \c .UR https://github.com/gokcehan/lf/blob/master/etc/ruler.default .UE \c ================================================ FILE: lf.desktop ================================================ [Desktop Entry] Type=Application Name=lf Comment=Launches the lf file manager Icon=utilities-terminal Terminal=true Exec=lf Categories=ConsoleOnly;System;FileTools;FileManager MimeType=inode/directory; ================================================ FILE: main.go ================================================ package main import ( "flag" "fmt" "log" "net" "os" "path/filepath" "reflect" "runtime" "runtime/debug" "runtime/pprof" "strconv" "strings" _ "embed" ) //go:embed doc.txt var genDocString string var ( envPath = os.Getenv("PATH") envLevel = os.Getenv("LF_LEVEL") ) type arrayFlag []string var ( gSingleMode bool gPrintLastDir bool gPrintSelection bool gClientID int gHostname string gLastDirPath string gSelectionPath string gSocketProt string gSocketPath string gLogPath string gSelect string gConfigPath string gCommands arrayFlag gVersion string ) func (a *arrayFlag) Set(v string) error { *a = append(*a, v) return nil } func (a *arrayFlag) String() string { return strings.Join(*a, ", ") } func init() { h, err := os.Hostname() if err != nil { log.Printf("hostname: %s", err) } gHostname = h if envLevel == "" { envLevel = "0" } } func exportEnvVars() { os.Setenv("id", strconv.Itoa(gClientID)) os.Setenv("OPENER", envOpener) os.Setenv("EDITOR", envEditor) os.Setenv("PAGER", envPager) os.Setenv("SHELL", envShell) dir, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "getting current directory: %s\n", err) } os.Setenv("OLDPWD", dir) level, err := strconv.Atoi(envLevel) if err != nil { log.Printf("reading lf level: %s", err) } level++ os.Setenv("LF_LEVEL", strconv.Itoa(level)) } func exportFlags() { os.Setenv("lf_flag_config", gConfigPath) os.Setenv("lf_flag_last_dir_path", gLastDirPath) os.Setenv("lf_flag_log", gLogPath) os.Setenv("lf_flag_print_last_dir", strconv.FormatBool(gPrintLastDir)) os.Setenv("lf_flag_print_selection", strconv.FormatBool(gPrintSelection)) os.Setenv("lf_flag_selection_path", gSelectionPath) os.Setenv("lf_flag_single", strconv.FormatBool(gSingleMode)) } // used by exportOpts below func fieldToString(field reflect.Value) string { // prevent returning if field.Type() == reflect.TypeFor[borderStyle]() { return borderStyle(field.Uint()).String() } var value string switch field.Kind() { case reflect.Int: value = strconv.Itoa(int(field.Int())) case reflect.Bool: value = strconv.FormatBool(field.Bool()) case reflect.Slice: for i := range field.Len() { element := field.Index(i) if i == 0 { value = fieldToString(element) } else { value += ":" + fieldToString(element) } } default: value = field.String() } return value } func getOptsMap() map[string]string { opts := make(map[string]string) v := reflect.ValueOf(gOpts) t := v.Type() for i := range v.NumField() { // Get field name and prefix it with lf_ name := "lf_" + t.Field(i).Name switch name { case "lf_nkeys", "lf_vkeys", "lf_cmdkeys", "lf_cmds": // Skip maps continue case "lf_user": // set each user option for key, value := range gOpts.user { opts[name+"_"+key] = value } default: opts[name] = fieldToString(v.Field(i)) } } return opts } func exportLfPath() { lfPath, err := os.Executable() if err != nil { log.Printf("getting path to lf binary: %s", err) lfPath = "lf" } os.Setenv("lf", quoteString(lfPath)) } func exportOpts() { for key, value := range getOptsMap() { os.Setenv(key, value) } } func startServer() { cmd := detachedCommand(os.Args[0], "-server") if err := cmd.Start(); err != nil { log.Printf("starting server: %s", err) } } func checkServer() { if gSocketProt == "unix" { if _, err := os.Stat(gSocketPath); os.IsNotExist(err) { startServer() } else if _, err := net.Dial(gSocketProt, gSocketPath); err != nil { if err := os.Remove(gSocketPath); err != nil { log.Print(err) } startServer() } } else { if _, err := net.Dial(gSocketProt, gSocketPath); err != nil { startServer() } } } func printVersion() { if gVersion != "" { fmt.Println(gVersion) return } buildInfo, ok := debug.ReadBuildInfo() if !ok { return } var vcsRevision, vcsTime, vcsModified string for _, setting := range buildInfo.Settings { switch setting.Key { case "vcs.revision": vcsRevision = setting.Value case "vcs.time": vcsTime = setting.Value case "vcs.modified": if setting.Value == "true" { vcsModified = " (dirty)" } } } if vcsRevision != "" { fmt.Printf("Built at commit: %s%s %s\n", vcsRevision, vcsModified, vcsTime) } fmt.Printf("Go version: %s\n", buildInfo.GoVersion) } func main() { flag.Usage = func() { f := flag.CommandLine.Output() fmt.Fprintf(f, `lf - Terminal file manager Usage: %s [options] [cd-or-select-path] cd-or-select-path set the initial dir or file selection to the given argument Options: `, os.Args[0]) flag.PrintDefaults() } showDoc := flag.Bool( "doc", false, "show documentation") showHelp := flag.Bool( "help", false, "show help") showVersion := flag.Bool( "version", false, "show version") serverMode := flag.Bool( "server", false, "start server (automatic)") singleMode := flag.Bool( "single", false, "start a client without server") printLastDir := flag.Bool( "print-last-dir", false, "print the last dir to stdout on exit (to use for cd)") printSelection := flag.Bool( "print-selection", false, "print the selected files to stdout on open (to use as open file dialog)") remoteCmd := flag.String( "remote", "", "send remote `command` to server") cpuprofile := flag.String( "cpuprofile", "", "`path` to the file to write the CPU profile") memprofile := flag.String( "memprofile", "", "`path` to the file to write the memory profile") flag.StringVar(&gLastDirPath, "last-dir-path", "", "`path` to the file to write the last dir on exit (to use for cd)") flag.StringVar(&gSelectionPath, "selection-path", "", "`path` to the file to write selected files on open (to use as open file dialog)") flag.StringVar(&gConfigPath, "config", "", "`path` to the config file (instead of the usual paths)") flag.Var(&gCommands, "command", "`command` to execute on client initialization") flag.StringVar(&gLogPath, "log", "", "`path` to the log file to write messages") flag.Parse() gSocketProt = gDefaultSocketProt gSocketPath = gDefaultSocketPath if gLogPath != "" { path, err := filepath.Abs(gLogPath) if err != nil { log.Fatalf("getting log path: %s", err) } gLogPath = path } if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatalf("could not create CPU profile: %s", err) } if err := pprof.StartCPUProfile(f); err != nil { log.Fatalf("could not start CPU profile: %s", err) } defer pprof.StopCPUProfile() } switch { case *showDoc: fmt.Print(genDocString) case *showHelp: flag.Usage() case *showVersion: printVersion() case *remoteCmd != "": resp, err := remote(*remoteCmd) if err != nil { log.Fatalf("remote command: %s", err) return } fmt.Print(resp) case *serverMode: if err := os.Chdir(gUser.HomeDir); err != nil { log.Print(err) } serve() default: gSingleMode = *singleMode gPrintLastDir = *printLastDir gPrintSelection = *printSelection if !gSingleMode { checkServer() } gClientID = os.Getpid() switch flag.NArg() { case 0: _, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "getting current directory: %s\n", err) os.Exit(2) } case 1: gSelect = flag.Arg(0) default: fmt.Fprintln(os.Stderr, "only single file or directory is allowed") os.Exit(2) } exportEnvVars() exportFlags() run() } if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal("could not create memory profile: ", err) } runtime.GC() if err := pprof.WriteHeapProfile(f); err != nil { log.Fatal("could not write memory profile: ", err) } f.Close() } } ================================================ FILE: misc.go ================================================ package main import ( "bufio" "bytes" "cmp" "fmt" "io" "io/fs" "math/big" "os" "path/filepath" "regexp" "slices" "strconv" "strings" "unicode" "github.com/rivo/uniseg" ) var ( reRulerSub = regexp.MustCompile(`%[apmcsvfithPd]|%\{[^}]+\}`) reSixelSize = regexp.MustCompile(`"1;1;(\d+);(\d+)`) ) var ( reWord = regexp.MustCompile(`(\pL|\pN)+`) reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`) reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`) ) func isRoot(name string) bool { return filepath.Dir(name) == name } func replaceTilde(s string) string { if strings.HasPrefix(s, "~") { return gUser.HomeDir + s[1:] } return s } // firstGraphemeCluster returns the string containing the first grapheme cluster // of the input. func firstGraphemeCluster(s string) string { gr := uniseg.NewGraphemes(s) gr.Next() return gr.Str() } // lastGraphemeCluster returns the string containing the last grapheme cluster // of the input. func lastGraphemeCluster(s string) string { gr := uniseg.NewGraphemes(s) var last string for gr.Next() { last = gr.Str() } return last } // truncateRight truncates a string from the right based on Unicode widths, // taking into account grapheme clusters. func truncateRight(s string, maxWidth int) string { buf := make([]byte, 0, len(s)) width := 0 gr := uniseg.NewGraphemes(s) for gr.Next() { width += gr.Width() if width > maxWidth { break } buf = append(buf, gr.Bytes()...) } return string(buf) } // truncateLeft truncates a string from the left based on Unicode widths, // taking into account grapheme clusters. func truncateLeft(s string, maxWidth int) string { type cluster struct { bytes []byte width int } var clusters []cluster totalWidth := 0 gr := uniseg.NewGraphemes(s) for gr.Next() { clusters = append(clusters, cluster{slices.Clone(gr.Bytes()), gr.Width()}) totalWidth += gr.Width() } buf := make([]byte, 0, len(s)) width := 0 for _, cluster := range clusters { if totalWidth-width <= maxWidth { buf = append(buf, cluster.bytes...) } width += cluster.width } return string(buf) } // cmdEscape is used to escape whitespace and special characters with // backslashes in a given string. func cmdEscape(s string) string { buf := make([]rune, 0, len(s)) for _, r := range s { if unicode.IsSpace(r) || r == '\\' || r == ';' || r == '#' { buf = append(buf, '\\') } buf = append(buf, r) } return string(buf) } // cmdUnescape is used to remove backslashes that are used to escape // whitespace and special characters in a given string. func cmdUnescape(s string) string { esc := false buf := make([]rune, 0, len(s)) for _, r := range s { if esc { if !unicode.IsSpace(r) && r != '\\' && r != ';' && r != '#' { buf = append(buf, '\\') } buf = append(buf, r) esc = false continue } if r == '\\' { esc = true continue } esc = false buf = append(buf, r) } if esc { buf = append(buf, '\\') } return string(buf) } // tokenize splits the given string by whitespace. It is aware of escaped // and quoted whitespace so that they are not split unintentionally. func tokenize(s string) []string { esc := false quote := false var buf []rune var toks []string for _, r := range s { switch { case esc: esc = false buf = append(buf, r) case r == '\\': esc = true buf = append(buf, r) case r == '"': quote = !quote buf = append(buf, r) case unicode.IsSpace(r) && !quote: toks = append(toks, string(buf)) buf = nil default: buf = append(buf, r) } } return append(toks, string(buf)) } // splitWord splits the first word of a string delimited by whitespace from // the rest. This is used to tokenize a string one by one without touching the // rest. Whitespace on the left side of both the word and the rest are trimmed. func splitWord(s string) (word, rest string) { s = strings.TrimLeftFunc(s, unicode.IsSpace) ind := len(s) for i, c := range s { if unicode.IsSpace(c) { ind = i break } } word = s[0:ind] rest = strings.TrimLeftFunc(s[ind:], unicode.IsSpace) return } // readArrays reads whitespace-separated string arrays on each line. Single // or double quotes can be used to escape whitespace. Hash characters can be // used to add a comment until the end of line. Leading and trailing space is // trimmed. Empty lines are skipped. func readArrays(r io.Reader, minCols, maxCols int) ([][]string, error) { var arrays [][]string s := bufio.NewScanner(r) for s.Scan() { line := s.Text() squote, dquote := false, false for i := range len(line) { if line[i] == '\'' && !dquote { squote = !squote } else if line[i] == '"' && !squote { dquote = !dquote } if !squote && !dquote && line[i] == '#' { line = line[:i] break } } line = strings.TrimSpace(line) if line == "" { continue } squote, dquote = false, false arr := strings.FieldsFunc(line, func(r rune) bool { if r == '\'' && !dquote { squote = !squote } else if r == '"' && !squote { dquote = !dquote } return !squote && !dquote && unicode.IsSpace(r) }) arrlen := len(arr) if arrlen < minCols || arrlen > maxCols { if minCols == maxCols { return nil, fmt.Errorf("expected %d columns but found: %s", minCols, s.Text()) } return nil, fmt.Errorf("expected %d~%d columns but found: %s", minCols, maxCols, s.Text()) } for i := range arrlen { squote, dquote = false, false buf := make([]rune, 0, len(arr[i])) for _, r := range arr[i] { if r == '\'' && !dquote { squote = !squote continue } if r == '"' && !squote { dquote = !dquote continue } buf = append(buf, r) } arr[i] = string(buf) } arrays = append(arrays, arr) } return arrays, s.Err() } func readPairs(r io.Reader) ([][]string, error) { return readArrays(r, 2, 2) } // humanize converts a size in bytes to a human-readable form using // prefixes for either binary (1 KiB = 1024 B) or decimal (1 KB = 1000 B) // multiples. The output should be no more than 5 characters long. func humanize(size int64) string { var base int64 = 1024 if gOpts.sizeunits == "decimal" { base = 1000 } if size < base { return fmt.Sprintf("%dB", size) } // Note: due to [fs.FileInfo.Size] being `int64`, the maximum // possible representable value would be 8 EiB or 9.2 EB. prefixes := []string{ "K", // kibi (2^10) or kilo (10^3) "M", // mebi (2^20) or mega (10^6) "G", // gibi (2^30) or giga (10^9) "T", // tebi (2^40) or tera (10^2) "P", // pebi (2^50) or peta (10^15) "E", // exbi (2^60) or exa (10^18) "Z", // zebi (2^70) or zetta (10^21) "Y", // yobi (2^80) or yotta (10^24) "R", // robi (2^90) or ronna (10^27) "Q", // quebi (2^100) or quetta (10^30) } curr := big.NewRat(size, base) for _, prefix := range prefixes { // if curr < 99.95 then round to 1 decimal place if curr.Cmp(big.NewRat(9995, 100)) < 0 { return fmt.Sprintf("%s%s", curr.FloatString(1), prefix) } // if curr < base-0.5 then round to the nearest integer if curr.Cmp(new(big.Rat).Sub(big.NewRat(base, 1), big.NewRat(1, 2))) < 0 { return fmt.Sprintf("%s%s", curr.FloatString(0), prefix) } curr.Quo(curr, big.NewRat(base, 1)) } return fmt.Sprintf("+999%s", prefixes[len(prefixes)-1]) } // permString returns an ls(1)-style string representation of the given file // mode, to avoid using [fs.FileMode.String], which differs slightly. func permString(m os.FileMode) string { // re-use Perm()'s "-rwxrwxrwx" output and write type into b[0] b := []byte(m.Perm().String()) switch { case m&os.ModeSymlink != 0: b[0] = 'l' case m&os.ModeDir != 0: b[0] = 'd' case m&os.ModeNamedPipe != 0: b[0] = 'p' case m&os.ModeSocket != 0: b[0] = 's' case m&os.ModeCharDevice != 0: b[0] = 'c' case m&os.ModeDevice != 0: b[0] = 'b' default: b[0] = '-' } // patch exec slots with suid/sgid/sticky flags if m&os.ModeSetuid != 0 { if b[3] == 'x' { b[3] = 's' } else { b[3] = 'S' } } if m&os.ModeSetgid != 0 { if b[6] == 'x' { b[6] = 's' } else { b[6] = 'S' } } if m&os.ModeSticky != 0 { if b[9] == 'x' { b[9] = 't' } else { b[9] = 'T' } } return string(b) } // naturalCmp compares two strings for natural sorting which takes into // account the values of numbers in strings. For example, '2' is ordered before // '10', and similarly 'foo2bar' ordered before 'foo10bar'. When comparing // numbers, if they have the same value then the length of the string is also // compared, so '0' is ordered before '00'. func naturalCmp(s1, s2 string) int { s1len := len(s1) s2len := len(s2) var lo1, lo2, hi1, hi2 int for { switch { case hi1 >= s1len && hi2 >= s2len: return 0 case hi1 >= s1len && hi2 < s2len: return -1 case hi1 < s1len && hi2 >= s2len: return 1 } lo1 = hi1 isDigit1 := isDigit(s1[hi1]) for hi1 < s1len && isDigit(s1[hi1]) == isDigit1 { hi1++ } tok1 := s1[lo1:hi1] lo2 = hi2 isDigit2 := isDigit(s2[hi2]) for hi2 < s2len && isDigit(s2[hi2]) == isDigit2 { hi2++ } tok2 := s2[lo2:hi2] if isDigit1 && isDigit2 { num1, err1 := strconv.Atoi(tok1) num2, err2 := strconv.Atoi(tok2) if err1 == nil && err2 == nil { if num1 != num2 { return cmp.Compare(num1, num2) } else if len(tok1) != len(tok2) { return cmp.Compare(len(tok1), len(tok2)) } } } if tok1 != tok2 { return cmp.Compare(tok1, tok2) } } } // getFileExtension returns the extension of a file with a leading dot. // It returns an empty string if extension could not be determined // i.e. directories, filenames without extensions func getFileExtension(file fs.FileInfo) string { if file.IsDir() { return "" } if strings.Count(file.Name(), ".") == 1 && file.Name()[0] == '.' { // hidden file without extension return "" } return filepath.Ext(file.Name()) } // truncateFilename truncates a filename at a given position. // The position is specified as percentage indicating where the truncation // character will appear (0 means left, 50 means middle, 100 means right). // The file extension is not affected by truncation, however it will be clipped // if it exceeds the allowed width. func truncateFilename(file fs.FileInfo, maxWidth, truncatePct int, truncateChar string) string { filename := file.Name() if uniseg.StringWidth(filename) <= maxWidth { return filename } ext := getFileExtension(file) avail := maxWidth - uniseg.StringWidth(truncateChar) - uniseg.StringWidth(ext) if avail < 0 { return truncateRight(truncateChar+ext, maxWidth) } basename := strings.TrimSuffix(filename, ext) left := truncateRight(basename, avail*truncatePct/100) right := truncateLeft(basename, avail-uniseg.StringWidth(left)) return left + truncateChar + right + ext } // deletePathRecursive deletes entries from a map if the key is either the given // path or a subpath of it. // This is useful for clearing cached data when a directory is moved or deleted. func deletePathRecursive[T any](m map[string]T, path string) { delete(m, path) prefix := path + string(filepath.Separator) for k := range m { if strings.HasPrefix(k, prefix) { delete(m, k) } } } // readLines reads lines from a file to be displayed as a preview. // The number of lines to read is capped since files can be very large. // Lines are split on `\n` characters, and `\r` characters are discarded. // Sixel images are also detected and stored as separate lines. // The presence of a null byte outside a sixel image indicates a binary file. func readLines(reader io.ByteReader, maxLines int) (lines []string, binary bool, sixel bool) { var buf bytes.Buffer var last byte inSixel := false for { b, err := reader.ReadByte() if err != nil { if buf.Len() > 0 { lines = append(lines, buf.String()) } return } if inSixel { buf.WriteByte(b) if b == '\\' && last == '\033' { lines = append(lines, buf.String()) buf.Reset() if len(lines) >= maxLines { return } inSixel = false } } else { switch { case b == 0: return nil, true, false case b == '\033': // withhold as it could be the start of a sixel image case b == 'P' && last == '\033': if buf.Len() > 0 { lines = append(lines, buf.String()) buf.Reset() if len(lines) >= maxLines { return } } buf.WriteByte(last) buf.WriteByte(b) inSixel = true sixel = true case last == '\033': // not a sixel image buf.WriteByte(last) buf.WriteByte(b) case b == '\r': case b == '\n': lines = append(lines, buf.String()) buf.Reset() if len(lines) >= maxLines { return } default: buf.WriteByte(b) } } last = b } } // getWidths calculates the widths of windows as the result of applying the // `ratios` option to the screen width. One column is allocated for each divider // between windows. When `drawbox` is enabled and `borderstyle` includes an outline, // getWidths reserves two additional columns for the left and right borders. func getWidths(wtot int, ratios []int, drawbox bool, borderstyle borderStyle) []int { rlen := len(ratios) wtot -= rlen - 1 if drawbox && borderstyle&borderOutline != 0 { wtot -= 2 } wtot = max(wtot, 0) rtot := 0 for _, r := range ratios { rtot += r } divround := func(x, y int) int { return (x + y/2) / y } widths := make([]int, rlen) rsum := 0 wsum := 0 for i, r := range ratios { rsum += r widths[i] = divround(wtot*rsum, rtot) - wsum wsum += widths[i] } return widths } // We don't need no generic code // We don't need no type control // No dark templates in compiler // Haskell leave them kids alone // Hey Bjarne leave them kids alone // All in all it's just another brick in the code // All in all you're just another brick in the code // // -- Pink Trolled -- ================================================ FILE: misc_test.go ================================================ package main import ( "math" "os" "reflect" "strings" "testing" "time" ) func TestIsRoot(t *testing.T) { sep := string(os.PathSeparator) if !isRoot(sep) { t.Errorf(`"%s" is root`, sep) } paths := []string{ "", "~", "foo", "foo/bar", "foo/bar", "/home", "/home/user", } for _, p := range paths { if isRoot(p) { t.Errorf("'%s' is not root", p) } } } func TestFirstGraphemeCluster(t *testing.T) { tests := []struct { s string exp string }{ {"", ""}, {"a", "a"}, {"世界", "世"}, {"🏳️a", "🏳️"}, } for _, test := range tests { if got := firstGraphemeCluster(test.s); got != test.exp { t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestLastGraphemeCluster(t *testing.T) { tests := []struct { s string exp string }{ {"", ""}, {"a", "a"}, {"世界", "界"}, {"a🏳️", "🏳️"}, } for _, test := range tests { if got := lastGraphemeCluster(test.s); got != test.exp { t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestTruncateRight(t *testing.T) { tests := []struct { s string maxWidth int exp string }{ {"", 0, ""}, {"", 1, ""}, {"a", 0, ""}, {"a", 1, "a"}, {"ab", 1, "a"}, {"世", 0, ""}, {"世", 1, ""}, {"世界", 2, "世"}, {"世界", 3, "世"}, {"a🏳️b", 2, "a"}, {"a🏳️b", 3, "a🏳️"}, {"a🏳️b", 4, "a🏳️b"}, } for _, test := range tests { if got := truncateRight(test.s, test.maxWidth); got != test.exp { t.Errorf("at input ('%v', %v) expected '%v' but got '%v'", test.s, test.maxWidth, test.exp, got) } } } func TestTruncateLeft(t *testing.T) { tests := []struct { s string maxWidth int exp string }{ {"", 0, ""}, {"", 1, ""}, {"a", 0, ""}, {"a", 1, "a"}, {"ab", 1, "b"}, {"世", 0, ""}, {"世", 1, ""}, {"世界", 2, "界"}, {"世界", 3, "界"}, {"a🏳️b", 2, "b"}, {"a🏳️b", 3, "🏳️b"}, {"a🏳️b", 4, "a🏳️b"}, } for _, test := range tests { if got := truncateLeft(test.s, test.maxWidth); got != test.exp { t.Errorf("at input ('%v', %v) expected '%v' but got '%v'", test.s, test.maxWidth, test.exp, got) } } } func TestCmdEscape(t *testing.T) { tests := []struct { s string exp string }{ {"", ""}, {"foo", "foo"}, {"foo bar", `foo\ bar`}, {"foo bar", `foo\ \ bar`}, {`foo\bar`, `foo\\bar`}, {`foo\ bar`, `foo\\\ bar`}, {`foo;bar`, `foo\;bar`}, {`foo#bar`, `foo\#bar`}, {`foo\tbar`, `foo\\tbar`}, {"foo\tbar", "foo\\\tbar"}, {`foo\`, `foo\\`}, } for _, test := range tests { if got := cmdEscape(test.s); !reflect.DeepEqual(got, test.exp) { t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestCmdUnescape(t *testing.T) { tests := []struct { s string exp string }{ {"", ""}, {"foo", "foo"}, {`foo\ bar`, "foo bar"}, {`foo\ \ bar`, "foo bar"}, {`foo\\bar`, `foo\bar`}, {`foo\\\ bar`, `foo\ bar`}, {`foo\;bar`, `foo;bar`}, {`foo\#bar`, `foo#bar`}, {`foo\\tbar`, `foo\tbar`}, {"foo\\\tbar", "foo\tbar"}, {`foo\`, `foo\`}, } for _, test := range tests { if got := cmdUnescape(test.s); !reflect.DeepEqual(got, test.exp) { t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestTokenize(t *testing.T) { tests := []struct { s string exp []string }{ {"", []string{""}}, {"foo", []string{"foo"}}, {`foo\`, []string{`foo\`}}, {`foo"`, []string{`foo"`}}, {"foo bar", []string{"foo", "bar"}}, {`foo\ bar`, []string{`foo\ bar`}}, {`"foo bar"`, []string{`"foo bar"`}}, {`"foo" "bar"`, []string{`"foo"`, `"bar"`}}, {`"foo "bar"`, []string{`"foo "bar"`}}, {`"foo\" bar"`, []string{`"foo\" bar"`}}, {`\"foo bar\"`, []string{`\"foo`, `bar\"`}}, {`:rename foo\ bar`, []string{":rename", `foo\ bar`}}, {`!dir "C:\Program Files"`, []string{"!dir", `"C:\Program Files"`}}, } for _, test := range tests { if got := tokenize(test.s); !reflect.DeepEqual(got, test.exp) { t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestSplitWord(t *testing.T) { tests := []struct { s string word string rest string }{ {"", "", ""}, {"foo", "foo", ""}, {" foo", "foo", ""}, {"foo ", "foo", ""}, {" foo ", "foo", ""}, {"foo bar baz", "foo", "bar baz"}, {" foo bar baz", "foo", "bar baz"}, {"foo bar baz", "foo", "bar baz"}, {" foo bar baz", "foo", "bar baz"}, } for _, test := range tests { if w, r := splitWord(test.s); w != test.word || r != test.rest { t.Errorf("at input '%s' expected '%s' and '%s' but got '%s' and '%s'", test.s, test.word, test.rest, w, r) } } } func TestReadArrays(t *testing.T) { tests := []struct { s string minCols int maxCols int exp [][]string }{ {"foo bar", 2, 2, [][]string{{"foo", "bar"}}}, {"foo bar ", 2, 2, [][]string{{"foo", "bar"}}}, {" foo bar", 2, 2, [][]string{{"foo", "bar"}}}, {" foo bar ", 2, 2, [][]string{{"foo", "bar"}}}, {"foo bar#baz", 2, 2, [][]string{{"foo", "bar"}}}, {"foo bar #baz", 2, 2, [][]string{{"foo", "bar"}}}, {`'foo#baz' bar`, 2, 2, [][]string{{"foo#baz", "bar"}}}, {`"foo#baz" bar`, 2, 2, [][]string{{"foo#baz", "bar"}}}, {"foo bar baz", 3, 3, [][]string{{"foo", "bar", "baz"}}}, {`"foo bar baz"`, 1, 1, [][]string{{"foo bar baz"}}}, } for _, test := range tests { if got, _ := readArrays(strings.NewReader(test.s), test.minCols, test.maxCols); !reflect.DeepEqual(got, test.exp) { t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestHumanize(t *testing.T) { tests := []struct { size int64 expected string }{ {0, "0B"}, {1, "1B"}, {2, "2B"}, {10, "10B"}, {100, "100B"}, {1000, "1000B"}, {1023, "1023B"}, {1024, "1.0K"}, {1025, "1.0K"}, // 1.000976563 KiB {10188, "9.9K"}, // 9.94921875 KiB {10189, "10.0K"}, // 9.950195313 KiB {10240, "10.0K"}, // 10 KiB {10291, "10.0K"}, // 10.049804688 KiB {10292, "10.1K"}, // 10.05078125 KiB {10342, "10.1K"}, // 10.099609375 KiB {102348, "99.9K"}, // 99.94921875 KiB {102349, "100K"}, // 99.950195313 KiB {1023487, "999K"}, // 999.499023438 KiB {1023488, "1000K"}, // 999.5 KiB {1048063, "1023K"}, // 1023.499023438 KiB {1048064, "1.0M"}, // 1023.5 KiB {1072693248, "1023M"}, // 1023 MiB {1073217535, "1023M"}, // 1023.499999046 MiB {1073217536, "1.0G"}, // 1023.5 MiB {1073741824, "1.0G"}, // 1 GiB {1610612736, "1.5G"}, // 1.5 GiB {1319413953332, "1.2T"}, // 1.2 TiB {1463669878895412, "1.3P"}, // 1.3 PiB {7955158381787244544, "6.9E"}, // 6.9 EiB {math.MaxInt64, "8.0E"}, // 8 EiB } gOpts.sizeunits = "binary" for _, test := range tests { if got := humanize(test.size); got != test.expected { t.Errorf("at input ('%d', '%s') expected '%s' but got '%s'", test.size, gOpts.sizeunits, test.expected, got) } } tests = []struct { size int64 expected string }{ {0, "0B"}, {1, "1B"}, {2, "2B"}, {10, "10B"}, {100, "100B"}, {999, "999B"}, {1000, "1.0K"}, {1001, "1.0K"}, {1049, "1.0K"}, {1050, "1.1K"}, {1051, "1.1K"}, {9949, "9.9K"}, {9950, "10.0K"}, {9951, "10.0K"}, {9999, "10.0K"}, {10000, "10.0K"}, {10001, "10.0K"}, {99949, "99.9K"}, {99950, "100K"}, {99951, "100K"}, {999499, "999K"}, {999500, "1.0M"}, {999501, "1.0M"}, {999999, "1.0M"}, {1000000, "1.0M"}, {1000001, "1.0M"}, {999499999, "999M"}, {999500000, "1.0G"}, {999500001, "1.0G"}, {999999999, "1.0G"}, {1000000000, "1.0G"}, {1000000001, "1.0G"}, {math.MaxInt64, "9.2E"}, } gOpts.sizeunits = "decimal" for _, test := range tests { if got := humanize(test.size); got != test.expected { t.Errorf("at input ('%d', '%s') expected '%s' but got '%s'", test.size, gOpts.sizeunits, test.expected, got) } } } func TestPermString(t *testing.T) { tests := []struct { name string m os.FileMode exp string }{ {"none", 0, "----------"}, {"regular file", 0o644, "-rw-r--r--"}, {"executable", 0o755, "-rwxr-xr-x"}, {"directory", 0o755 | os.ModeDir, "drwxr-xr-x"}, {"symbolic link", 0o777 | os.ModeSymlink, "lrwxrwxrwx"}, {"named pipe", 0o644 | os.ModeNamedPipe, "prw-r--r--"}, {"socket", 0o777 | os.ModeSocket, "srwxrwxrwx"}, {"character device", 0o660 | os.ModeCharDevice, "crw-rw----"}, {"block device", 0o660 | os.ModeDevice, "brw-rw----"}, {"setuid", 0o644 | os.ModeSetuid, "-rwSr--r--"}, {"setuid executable", 0o755 | os.ModeSetuid, "-rwsr-xr-x"}, {"setgid", 0o644 | os.ModeSetgid, "-rw-r-Sr--"}, {"setgid executable", 0o755 | os.ModeSetgid, "-rwxr-sr-x"}, {"sticky", 0o644 | os.ModeSticky, "-rw-r--r-T"}, {"sticky executable", 0o755 | os.ModeSticky, "-rwxr-xr-t"}, {"sticky directory", 0o777 | os.ModeDir | os.ModeSticky, "drwxrwxrwt"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := permString(test.m); got != test.exp { t.Errorf("at input '%#o' expected '%s' but got '%s'", test.m, test.exp, got) } }) } } func TestNaturalCmp(t *testing.T) { tests := []struct { s1 string s2 string exp int }{ {"", "", 0}, {"a", "a", 0}, {"", "a", -1}, {"a", "b", -1}, {"a", "ab", -1}, {"0", "0", 0}, {"0", "00", -1}, {"1", "1", 0}, {"1", "01", -1}, {"2", "10", -1}, {"123", "foo", -1}, {"foo", "foo1", -1}, {"foo1", "foobar", -1}, {"foo1", "foobar1", -1}, {"foo2", "foo10", -1}, {"foo2bar", "foo10bar", -1}, {"foo0", "foo00", -1}, {"foo0bar", "foo00bar", -1}, {"foo1", "foo01", -1}, {"foo1bar", "foo01bar", -1}, } for _, test := range tests { if got := naturalCmp(test.s1, test.s2); got != test.exp { t.Errorf("at input '%s' and '%s' expected '%d' but got '%d'", test.s1, test.s2, test.exp, got) } if got := naturalCmp(test.s2, test.s1); got != -test.exp { t.Errorf("at input '%s' and '%s' expected '%d' but got '%d'", test.s2, test.s1, -test.exp, got) } } } type fakeFileInfo struct { name string isDir bool } func (fileinfo fakeFileInfo) Name() string { return fileinfo.name } func (fileinfo fakeFileInfo) Size() int64 { return 0 } func (fileinfo fakeFileInfo) Mode() os.FileMode { return os.FileMode(0o000) } func (fileinfo fakeFileInfo) ModTime() time.Time { return time.Unix(0, 0) } func (fileinfo fakeFileInfo) IsDir() bool { return fileinfo.isDir } func (fileinfo fakeFileInfo) Sys() any { return nil } func TestGetFileExtension(t *testing.T) { tests := []struct { name string fileName string isDir bool expectedExtension string }{ {"normal file", "file.txt", false, ".txt"}, {"file without extension", "file", false, ""}, {"hidden file", ".gitignore", false, ""}, {"hidden file with extension", ".file.txt", false, ".txt"}, {"directory", "dir", true, ""}, {"hidden directory", ".git", true, ""}, {"directory with dot", "profile.d", true, ""}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := getFileExtension(fakeFileInfo{test.fileName, test.isDir}); got != test.expectedExtension { t.Errorf("at input '%s' expected '%s' but got '%s'", test.fileName, test.expectedExtension, got) } }) } } func TestTruncateFilename(t *testing.T) { tests := []struct { file fakeFileInfo maxWidth int truncatePct int exp string }{ {fakeFileInfo{"foo", false}, 0, 0, ""}, {fakeFileInfo{"foo", false}, 0, 100, ""}, {fakeFileInfo{"foo", false}, 1, 0, "~"}, {fakeFileInfo{"foo", false}, 1, 100, "~"}, {fakeFileInfo{"foo", false}, 2, 0, "~o"}, {fakeFileInfo{"foo", false}, 2, 100, "f~"}, {fakeFileInfo{"foo", false}, 3, 0, "foo"}, {fakeFileInfo{"foo", false}, 3, 100, "foo"}, {fakeFileInfo{"foo", false}, 4, 0, "foo"}, {fakeFileInfo{"foo", false}, 4, 100, "foo"}, {fakeFileInfo{"foo.txt", false}, 0, 0, ""}, {fakeFileInfo{"foo.txt", false}, 0, 100, ""}, {fakeFileInfo{"foo.txt", false}, 1, 0, "~"}, {fakeFileInfo{"foo.txt", false}, 1, 100, "~"}, {fakeFileInfo{"foo.txt", false}, 2, 0, "~."}, {fakeFileInfo{"foo.txt", false}, 2, 100, "~."}, {fakeFileInfo{"foo.txt", false}, 3, 0, "~.t"}, {fakeFileInfo{"foo.txt", false}, 3, 100, "~.t"}, {fakeFileInfo{"foo.txt", false}, 4, 0, "~.tx"}, {fakeFileInfo{"foo.txt", false}, 4, 100, "~.tx"}, {fakeFileInfo{"foo.txt", false}, 5, 0, "~.txt"}, {fakeFileInfo{"foo.txt", false}, 5, 100, "~.txt"}, {fakeFileInfo{"foo.txt", false}, 6, 0, "~o.txt"}, {fakeFileInfo{"foo.txt", false}, 6, 100, "f~.txt"}, {fakeFileInfo{"foobarbaz", false}, 7, 0, "~barbaz"}, {fakeFileInfo{"foobarbaz", false}, 7, 50, "foo~baz"}, {fakeFileInfo{"foobarbaz", false}, 7, 100, "foobar~"}, {fakeFileInfo{"foobarbaz.txt", false}, 11, 0, "~barbaz.txt"}, {fakeFileInfo{"foobarbaz.txt", false}, 11, 50, "foo~baz.txt"}, {fakeFileInfo{"foobarbaz.txt", false}, 11, 100, "foobar~.txt"}, {fakeFileInfo{"foobarbaz.d", true}, 9, 0, "~barbaz.d"}, {fakeFileInfo{"foobarbaz.d", true}, 9, 50, "foob~az.d"}, {fakeFileInfo{"foobarbaz.d", true}, 9, 100, "foobarba~"}, {fakeFileInfo{"世界世界.txt", false}, 10, 0, "~世界.txt"}, {fakeFileInfo{"世界世界.txt", false}, 10, 50, "世~界.txt"}, {fakeFileInfo{"世界世界.txt", false}, 10, 100, "世界~.txt"}, {fakeFileInfo{"世界世界.txt", false}, 11, 0, "~界世界.txt"}, {fakeFileInfo{"世界世界.txt", false}, 11, 50, "世~世界.txt"}, {fakeFileInfo{"世界世界.txt", false}, 11, 100, "世界世~.txt"}, } for _, test := range tests { if got := truncateFilename(test.file, test.maxWidth, test.truncatePct, "~"); got != test.exp { t.Errorf("at input (%v, %v, %v) expected '%s' but got '%s'", test.file, test.maxWidth, test.truncatePct, test.exp, got) } } } func TestReadLines(t *testing.T) { tests := []struct { s string maxLines int lines []string binary bool sixel bool }{ {"", 10, nil, false, false}, {"\r", 10, nil, false, false}, {"\r\n", 10, []string{""}, false, false}, {"\r\r\n", 10, []string{""}, false, false}, {"\n\n", 10, []string{"", ""}, false, false}, {"foo", 10, []string{"foo"}, false, false}, {"foo\n", 10, []string{"foo"}, false, false}, {"foo\r\n", 10, []string{"foo"}, false, false}, {"foo\nbar", 10, []string{"foo", "bar"}, false, false}, {"foo\nbar\n", 10, []string{"foo", "bar"}, false, false}, {"foo\r\nbar", 10, []string{"foo", "bar"}, false, false}, {"foo\r\nbar\r\n", 10, []string{"foo", "bar"}, false, false}, {"\033[31mfoo\033[0m", 10, []string{"\033[31mfoo\033[0m"}, false, false}, {"\000", 10, nil, true, false}, {"foo\r\n\000\r\nbar\r\n", 10, nil, true, false}, {"\033P\033\\", 10, []string{"\033P\033\\"}, false, true}, {"\033Pq\"1;1;1;1#0@\033\\", 10, []string{"\033Pq\"1;1;1;1#0@\033\\"}, false, true}, {"\033P\000\033\\", 10, []string{"\033P\000\033\\"}, false, true}, {"\033P\n\033\\", 10, []string{"\033P\n\033\\"}, false, true}, {"\033P\r\n\033\\", 10, []string{"\033P\r\n\033\\"}, false, true}, {"\033P\033\\\033P\033\\", 10, []string{"\033P\033\\", "\033P\033\\"}, false, true}, {"foo\033P\033\\bar", 10, []string{"foo", "\033P\033\\", "bar"}, false, true}, {"foo\033P\033\\bar\033P\033\\baz", 10, []string{"foo", "\033P\033\\", "bar", "\033P\033\\", "baz"}, false, true}, {"foo\nbar\nbaz", 2, []string{"foo", "bar"}, false, false}, {"foo\nbar\nbaz\n", 2, []string{"foo", "bar"}, false, false}, {"foo\nbar\033P\033\\", 2, []string{"foo", "bar"}, false, false}, {"foo\nbar\nbaz", 3, []string{"foo", "bar", "baz"}, false, false}, {"foo\nbar\nbaz\n", 3, []string{"foo", "bar", "baz"}, false, false}, {"foo\nbar\033P\033\\", 3, []string{"foo", "bar", "\033P\033\\"}, false, true}, } for _, test := range tests { lines, binary, sixel := readLines(strings.NewReader(test.s), test.maxLines) if !reflect.DeepEqual(lines, test.lines) || binary != test.binary || sixel != test.sixel { t.Errorf( "at input (%q, %v) expected (%#v, %v, %v) but got (%#v, %v, %v)", test.s, test.maxLines, test.lines, test.binary, test.sixel, lines, binary, sixel, ) } } } func TestGetWidths(t *testing.T) { tests := []struct { wtot int ratios []int drawbox bool borderstyle borderStyle exp []int }{ {0, []int{1}, false, borderBox, []int{0}}, {0, []int{1}, true, borderBox, []int{0}}, {0, []int{1, 3, 2}, false, borderBox, []int{0, 0, 0}}, {0, []int{1, 3, 2}, true, borderBox, []int{0, 0, 0}}, {14, []int{1, 3, 2}, false, borderBox, []int{2, 6, 4}}, {16, []int{1, 3, 2}, true, borderBox, []int{2, 6, 4}}, {16, []int{1, 3, 2}, true, borderSeparators, []int{2, 7, 5}}, {16, []int{1, 3, 2}, true, borderOutline, []int{2, 6, 4}}, {16, []int{1, 3, 2}, true, borderRoundOutline, []int{2, 6, 4}}, {16, []int{1, 3, 2}, true, borderRoundBox, []int{2, 6, 4}}, {23, []int{1, 3, 2, 4}, false, borderBox, []int{2, 6, 4, 8}}, // windows end at 2.0, 8.0, 12.0, 20.0 respectively {24, []int{1, 3, 2, 4}, false, borderBox, []int{2, 6, 5, 8}}, // windows end at 2.1, 8.4, 12.6, 21.0 respectively {25, []int{1, 3, 2, 4}, false, borderBox, []int{2, 7, 4, 9}}, // windows end at 2.2, 8.8, 13.2, 22.0 respectively {26, []int{1, 3, 2, 4}, false, borderBox, []int{2, 7, 5, 9}}, // windows end at 2.3, 9.2, 13.8, 23.0 respectively } for _, test := range tests { widths := getWidths(test.wtot, test.ratios, test.drawbox, test.borderstyle) if !reflect.DeepEqual(widths, test.exp) { t.Errorf("at input (%v, %v, %v, %v) expected %v but got %v", test.wtot, test.ratios, test.drawbox, test.borderstyle, test.exp, widths) } } } ================================================ FILE: nav.go ================================================ package main import ( "bufio" "bytes" "cmp" "errors" "fmt" "io" "log" "maps" "os" "os/exec" "path/filepath" "reflect" "regexp" "slices" "sort" "strconv" "strings" "time" "github.com/djherbis/times" ) // A linkState describes whether a file is a symlink and whether its target exists. type linkState byte const ( notLink linkState = iota // Not a symbolic link. working // Symbolic link with an existing target. broken // Symbolic link with a missing target. ) type file struct { os.FileInfo // stat information linkState linkState // symlink state linkTarget string // path a symlink points to path string // full path including the name dirCount int // number of items inside the directory dirSize int64 // total directory size (needs to be calculated via `calcdirsize`) accessTime time.Time // time of last access birthTime time.Time // time of file birth changeTime time.Time // time of last status (inode) change customInfo string // property defined via `addcustominfo` ext string // file extension (including the dot) err error // potential error returned by [os.Lstat] } func newFile(path string) *file { lstat, err := os.Lstat(path) if err != nil { log.Printf("getting file information: %s", err) return &file{ FileInfo: &fakeStat{name: filepath.Base(path)}, linkState: notLink, path: path, dirCount: -1, dirSize: -1, accessTime: time.Unix(0, 0), birthTime: time.Unix(0, 0), changeTime: time.Unix(0, 0), err: err, } } var linkState linkState var linkTarget string if lstat.Mode()&os.ModeSymlink != 0 { stat, err := os.Stat(path) if err == nil { linkState = working lstat = stat } else { linkState = broken } linkTarget, err = os.Readlink(path) if err != nil { log.Printf("reading link target: %s", err) } } ts := times.Get(lstat) at := ts.AccessTime() // from [times.Timespec] docs: // ChangeTime() panics unless HasChangeTime() is true and // BirthTime() panics unless HasBirthTime() is true. // default to ModTime if BirthTime cannot be determined bt := lstat.ModTime() if ts.HasBirthTime() { bt = ts.BirthTime() } // default to ModTime if ChangeTime cannot be determined ct := lstat.ModTime() if ts.HasChangeTime() { ct = ts.ChangeTime() } dirCount := -1 if lstat.IsDir() && getDirCounts(filepath.Dir(path)) { d, err := os.Open(path) if err != nil { log.Printf("opening file: %s", err) } else { names, err := d.Readdirnames(10000) d.Close() if names == nil && err != io.EOF { log.Printf("reading directory: %s", err) } else { dirCount = len(names) } } } return &file{ FileInfo: lstat, linkState: linkState, linkTarget: linkTarget, path: path, dirCount: dirCount, dirSize: -1, accessTime: at, birthTime: bt, changeTime: ct, ext: getFileExtension(lstat), } } func (file *file) isPreviewable() bool { return !file.IsDir() || gOpts.dirpreviews } type fakeStat struct { name string } func (fs *fakeStat) Name() string { return fs.name } func (fs *fakeStat) Size() int64 { return 0 } func (fs *fakeStat) Mode() os.FileMode { return os.FileMode(0o000) } func (fs *fakeStat) ModTime() time.Time { return time.Unix(0, 0) } func (fs *fakeStat) IsDir() bool { return false } func (fs *fakeStat) Sys() any { return nil } func readdir(path string) ([]*file, error) { f, err := os.Open(path) if err != nil { return nil, err } names, err := f.Readdirnames(-1) f.Close() files := make([]*file, 0, len(names)) for _, fname := range names { file := newFile(filepath.Join(path, fname)) if !os.IsNotExist(file.err) { files = append(files, file) } } return files, err } type dir struct { loading bool // whether directory is loading from disk loadTime time.Time // last load time ind int // 0-based index of current entry in dir.files pos int // 0-based cursor row in directory window path string // full path of directory files []*file // displayed files in directory including or excluding hidden ones allFiles []*file // all files in directory including hidden ones (same array as files) sortby sortMethod // sortby value from last sort dircounts bool // dircounts value from last sort dirfirst bool // dirfirst value from last sort dironly bool // dironly value from last sort hidden bool // hidden value from last sort reverse bool // reverse value from last sort visualAnchor int // index where Visual mode was initiated visualWrap int // wrap direction in Visual mode (0: none, +: bottom->top, -: top->bottom) hiddenfiles []string // hiddenfiles value from last sort filter []string // last filter for this directory ignorecase bool // ignorecase value from last sort ignoredia bool // ignoredia value from last sort noPerm bool // whether lf has no permission to open the directory } func newDir(path string) *dir { files, err := readdir(path) if err != nil { log.Printf("reading directory: %s", err) } return &dir{ loadTime: time.Now(), path: path, files: files, allFiles: files, visualAnchor: -1, noPerm: os.IsPermission(err), } } func (dir *dir) sort() { dir.sortby = getSortBy(dir.path) dir.dircounts = getDirCounts(dir.path) dir.dirfirst = getDirFirst(dir.path) dir.dironly = getDirOnly(dir.path) dir.hidden = getHidden(dir.path) dir.reverse = getReverse(dir.path) dir.hiddenfiles = gOpts.hiddenfiles dir.ignorecase = gOpts.ignorecase dir.ignoredia = gOpts.ignoredia dir.files = dir.allFiles // When applying a filter, move all files not satisfying the predicate to // the beginning, then take the subslice starting from the first file that // does satisfy the predicate applyFilter := func(fn func(f *file) bool) { slices.SortStableFunc(dir.files, func(i, j *file) int { switch { case !fn(i) && fn(j): return -1 case fn(i) && !fn(j): return 1 default: return 0 } }) i := slices.IndexFunc(dir.files, fn) if i == -1 { i = len(dir.files) } dir.files = dir.files[i:] } if dir.dironly { applyFilter(func(f *file) bool { return f.IsDir() }) } if !dir.hidden { applyFilter(func(f *file) bool { return !isHidden(f, dir.path, dir.hiddenfiles) }) } if len(dir.filter) != 0 { applyFilter(func(f *file) bool { return !isFiltered(f, dir.filter) }) } applySort := func(fn func(f1, f2 *file) int) { if !dir.reverse { slices.SortStableFunc(dir.files, fn) } else { slices.SortStableFunc(dir.files, func(f1, f2 *file) int { return fn(f2, f1) }) } } normalize := func(s string) string { if dir.ignorecase { s = strings.ToLower(s) } if dir.ignoredia { s = removeDiacritics(s) } return s } switch dir.sortby { case naturalSort: applySort(func(f1, f2 *file) int { return naturalCmp(normalize(f1.Name()), normalize(f2.Name())) }) case nameSort: applySort(func(f1, f2 *file) int { return cmp.Compare(normalize(f1.Name()), normalize(f2.Name())) }) case sizeSort: sizeVal := func(f *file) int64 { if f.IsDir() && dir.dircounts { return int64(f.dirCount) } if f.dirSize >= 0 { return f.dirSize } return f.Size() } applySort(func(f1, f2 *file) int { return cmp.Compare(sizeVal(f1), sizeVal(f2)) }) case timeSort: applySort(func(f1, f2 *file) int { return f1.ModTime().Compare(f2.ModTime()) }) case atimeSort: applySort(func(f1, f2 *file) int { return f1.accessTime.Compare(f2.accessTime) }) case btimeSort: applySort(func(f1, f2 *file) int { return f1.birthTime.Compare(f2.birthTime) }) case ctimeSort: applySort(func(f1, f2 *file) int { return f1.changeTime.Compare(f2.changeTime) }) case extSort: applySort(func(f1, f2 *file) int { ext1 := normalize(f1.ext) ext2 := normalize(f2.ext) if ext1 != ext2 { return cmp.Compare(ext1, ext2) } return cmp.Compare(normalize(f1.Name()), normalize(f2.Name())) }) case customSort: applySort(func(f1, f2 *file) int { s1 := normalize(stripTermSequence(f1.customInfo)) s2 := normalize(stripTermSequence(f2.customInfo)) return naturalCmp(s1, s2) }) } // when sorting by size while also showing dircounts, we always display files // and directories separately to avoid mixing file sizes and file counts if dir.dirfirst || (dir.sortby == sizeSort && dir.dircounts) { slices.SortStableFunc(dir.files, func(f1, f2 *file) int { switch { case f1.IsDir() && !f2.IsDir(): return -1 case !f1.IsDir() && f2.IsDir(): return 1 default: return 0 } }) } dir.ind = max(dir.ind, 0) dir.ind = min(dir.ind, len(dir.files)-1) } func (dir *dir) name() string { if len(dir.files) == 0 { return "" } return dir.files[dir.ind].Name() } func (nav *nav) isVisualMode() bool { return nav.currDir().visualAnchor != -1 } func (dir *dir) visualSelections() []string { paths := []string{} if dir.visualAnchor == -1 || len(dir.files) == 0 { return paths } var beg, end int switch { case dir.visualWrap == 0: beg = min(dir.ind, dir.visualAnchor) end = max(dir.ind, dir.visualAnchor) case dir.visualWrap < 0: beg = dir.ind end = dir.visualAnchor - dir.visualWrap*len(dir.files) case dir.visualWrap > 0: beg = dir.visualAnchor end = dir.ind + dir.visualWrap*len(dir.files) } for i := beg; i < min(end+1, beg+len(dir.files)); i++ { paths = append(paths, dir.files[i%len(dir.files)].path) } return paths } func (dir *dir) sel(name string, height int) { if len(dir.files) == 0 { dir.ind, dir.pos = 0, 0 return } dir.ind = max(dir.ind, 0) dir.ind = min(dir.ind, len(dir.files)-1) if dir.files[dir.ind].Name() != name { for i, f := range dir.files { if f.Name() == name { dir.ind = i break } } } dir.boundPos(height) } func (dir *dir) boundPos(height int) { if len(dir.files) <= height { dir.pos = dir.ind return } edge := min(height/2, gOpts.scrolloff) dir.pos = max(dir.pos, edge) // use a smaller value for half when the height is even and scrolloff is // maxed in order to stay at the same row while scrolling up and down if height%2 == 0 { edge = min(height/2-1, gOpts.scrolloff) } dir.pos = min(dir.pos, height-1-edge) dir.pos = min(dir.pos, dir.ind) dir.pos = max(dir.pos, height-(len(dir.files)-dir.ind)) } // clipboardMode controls the clipboard's behavior when pasting. type clipboardMode byte const ( clipboardCopy clipboardMode = iota // Copy on paste. clipboardCut // Move on paste. ) type clipboard struct { paths []string mode clipboardMode } type nav struct { dirPaths []string copyJobs int copyBytes int64 copyTotal int64 copyUpdate int moveCount int moveTotal int moveUpdate int deleteCount int deleteTotal int deleteUpdate int copyJobsChan chan int copyBytesChan chan int64 copyTotalChan chan int64 moveCountChan chan int moveTotalChan chan int deleteCountChan chan int deleteTotalChan chan int preloadChan chan string previewChan chan string dirChan chan *dir regChan chan *reg fileChan chan *file delChan chan string dirCache map[string]*dir regCache map[string]*reg clipboard clipboard marks map[string]string renameOldPath string renameNewPath string selections map[string]int tags map[string]string selectionInd int height int previewWidth int find string findBack bool search string searchBack bool searchInd int searchPos int prevFilter []string volatilePreview bool previewTimer *time.Timer preloadTimer *time.Timer jumpList []string jumpListInd int } func (nav *nav) getDir(path string) *dir { if d, ok := nav.dirCache[path]; ok { return d } go func() { nav.dirChan <- newDir(path) }() d := &dir{ loading: true, loadTime: time.Now(), path: path, sortby: getSortBy(path), dircounts: getDirCounts(path), dirfirst: getDirFirst(path), dironly: getDirOnly(path), hidden: getHidden(path), reverse: getReverse(path), visualAnchor: -1, hiddenfiles: gOpts.hiddenfiles, ignorecase: gOpts.ignorecase, ignoredia: gOpts.ignoredia, } nav.dirCache[path] = d return d } func (nav *nav) checkDir(dir *dir) { if dir.loading { return } s, err := os.Stat(dir.path) if err != nil { log.Printf("getting directory info: %s", err) return } switch { case s.ModTime().After(dir.loadTime): // XXX: Linux builtin exFAT drivers are able to predict modifications in the future // https://bugs.launchpad.net/ubuntu/+source/ubuntu-meta/+bug/1872504 if s.ModTime().After(time.Now()) { return } dir.loading = true go func() { nav.dirChan <- newDir(dir.path) }() case dir.dircounts != getDirCounts(dir.path): dir.loading = true go func() { nav.dirChan <- newDir(dir.path) }() // Although toggling dircounts can affect sorting, it is already handled by // reloading the directory which should sort the files anyway, so it is not // checked below. case dir.sortby != getSortBy(dir.path) || dir.dirfirst != getDirFirst(dir.path) || dir.dironly != getDirOnly(dir.path) || dir.hidden != getHidden(dir.path) || dir.reverse != getReverse(dir.path) || !reflect.DeepEqual(dir.hiddenfiles, gOpts.hiddenfiles) || dir.ignorecase != gOpts.ignorecase || dir.ignoredia != gOpts.ignoredia: dir.loading = true sd := *dir go func() { sd.sort() sd.loading = false nav.dirChan <- &sd }() } } func (nav *nav) loadDirs(wd string) { var dirPaths []string for curr, base := wd, ""; !isRoot(base); curr, base = filepath.Dir(curr), filepath.Base(curr) { dirPaths = append(dirPaths, curr) dir := nav.getDir(curr) if base != "" { dir.sel(base, nav.height) } } slices.Reverse(dirPaths) nav.dirPaths = dirPaths } func newNav(ui *ui) *nav { nav := &nav{ copyJobsChan: make(chan int, 1024), copyBytesChan: make(chan int64, 1024), copyTotalChan: make(chan int64, 1024), moveCountChan: make(chan int, 1024), moveTotalChan: make(chan int, 1024), deleteCountChan: make(chan int, 1024), deleteTotalChan: make(chan int, 1024), preloadChan: make(chan string, 1024), previewChan: make(chan string, 1024), dirChan: make(chan *dir), regChan: make(chan *reg), fileChan: make(chan *file), delChan: make(chan string), dirCache: make(map[string]*dir), regCache: make(map[string]*reg), marks: make(map[string]string), selections: make(map[string]int), tags: make(map[string]string), selectionInd: 0, previewTimer: time.NewTimer(0), preloadTimer: time.NewTimer(0), jumpList: make([]string, 0), jumpListInd: -1, } nav.resize(ui) return nav } func (nav *nav) addJumpList() { currPath := nav.currDir().path if nav.jumpListInd >= 0 && nav.jumpListInd < len(nav.jumpList)-1 { if nav.jumpList[nav.jumpListInd] == currPath { // walking the jumpList return } nav.jumpList = nav.jumpList[:nav.jumpListInd+1] } if len(nav.jumpList) == 0 || nav.jumpList[len(nav.jumpList)-1] != currPath { nav.jumpList = append(nav.jumpList, currPath) } nav.jumpListInd = len(nav.jumpList) - 1 } func (nav *nav) cdJumpListPrev() { if nav.jumpListInd > 0 { nav.jumpListInd-- if err := nav.cd(nav.jumpList[nav.jumpListInd]); err != nil { log.Print(err) } } } func (nav *nav) cdJumpListNext() { if nav.jumpListInd < len(nav.jumpList)-1 { nav.jumpListInd++ if err := nav.cd(nav.jumpList[nav.jumpListInd]); err != nil { log.Print(err) } } } func (nav *nav) renew() { for _, path := range nav.dirPaths { dir := nav.getDir(path) nav.checkDir(dir) } for m := range nav.selections { if _, err := os.Lstat(m); os.IsNotExist(err) { delete(nav.selections, m) } } if len(nav.selections) == 0 { nav.selectionInd = 0 } } func (nav *nav) reload() { wd := nav.currDir().path curr := nav.currFile() clear(nav.dirCache) clear(nav.regCache) nav.loadDirs(wd) if curr != nil { dir := nav.currDir() dir.files = append(dir.files, curr) } } func (nav *nav) resize(ui *ui) { previewWin := ui.wins[len(ui.wins)-1] if previewWin.h == nav.height && previewWin.w == nav.previewWidth { return } nav.height = previewWin.h nav.previewWidth = previewWin.w for _, path := range nav.dirPaths { nav.getDir(path).boundPos(nav.height) } clear(nav.regCache) nav.preloadTimer.Reset(200 * time.Millisecond) } func (nav *nav) position() { var path string var base string for i := len(nav.dirPaths) - 1; i >= 0; i-- { path = nav.dirPaths[i] if i < len(nav.dirPaths)-1 { nav.getDir(path).sel(base, nav.height) } base = filepath.Base(path) } } func (nav *nav) exportFiles() { var currFile string if curr := nav.currFile(); curr != nil { currFile = quoteString(curr.path) } var selections []string for _, selection := range nav.currSelections() { selections = append(selections, quoteString(selection)) } currSelections := strings.Join(selections, gOpts.filesep) var vSelections []string for _, selection := range nav.currDir().visualSelections() { vSelections = append(vSelections, quoteString(selection)) } currVSelections := strings.Join(vSelections, gOpts.filesep) os.Setenv("f", currFile) os.Setenv("fs", currSelections) os.Setenv("fv", currVSelections) os.Setenv("PWD", quoteString(nav.currDir().path)) if len(selections) == 0 { os.Setenv("fx", currFile) } else { os.Setenv("fx", currSelections) } } func (nav *nav) preloadLoop(ui *ui) { stack := []string{} push := func(path string) { stack = slices.DeleteFunc(stack, func(s string) bool { return s == path }) stack = append(stack, path) } pop := func() string { path := stack[len(stack)-1] stack = stack[:len(stack)-1] return path } for { if len(stack) == 0 { push(<-nav.preloadChan) } else { select { case path := <-nav.preloadChan: push(path) default: path := pop() nav.preview(path, ui.wins[len(ui.wins)-1], "preload") } } } } func (nav *nav) previewLoop(ui *ui) { var prev string for path := range nav.previewChan { isClear := len(path) == 0 loop: for { select { case path = <-nav.previewChan: isClear = isClear || len(path) == 0 default: break loop } } win := ui.wins[len(ui.wins)-1] if isClear && len(gOpts.previewer) != 0 && len(gOpts.cleaner) != 0 && nav.volatilePreview { cmd := exec.Command( gOpts.cleaner, prev, strconv.Itoa(win.w), strconv.Itoa(win.h), strconv.Itoa(win.x), strconv.Itoa(win.y), path, ) var stderr bytes.Buffer cmd.Stderr = &stderr if err := cmd.Run(); err != nil { var exitErr *exec.ExitError if !errors.As(err, &exitErr) { log.Printf("cleaning preview: %s", err) } } if s := strings.TrimSpace(stderr.String()); s != "" { s = strings.Join(strings.Fields(s), " ") log.Printf("cleaning preview (stderr): %s", s) } nav.volatilePreview = false } if len(path) != 0 { nav.preview(path, win, "preview") prev = path } } } func matchPattern(pattern, name, path string) bool { s := name pattern = replaceTilde(pattern) if filepath.IsAbs(pattern) { s = filepath.Join(path, name) } // pattern errors are checked when 'hiddenfiles' option is set matched, _ := filepath.Match(pattern, s) return matched } func (nav *nav) preload() { if !gOpts.preview || !gOpts.preload { return } dir := nav.currDir() doPreload := func(i int) { if i < 0 || i >= len(dir.files) { return } file := dir.files[i] if !file.isPreviewable() { return } if _, ok := nav.regCache[file.path]; ok { return } nav.regCache[file.path] = ®{loading: true, loadTime: time.Now(), path: file.path} select { case nav.preloadChan <- file.path: default: } } for i := nav.height / 2; i >= 1; i-- { doPreload(dir.ind - i) doPreload(dir.ind + i) } doPreload(dir.ind) } func (nav *nav) preview(path string, win *win, mode string) { reg := ®{loadTime: time.Now(), path: path} defer func() { if (gOpts.preload && mode == "preview") || (!gOpts.preload && reg.volatile) { nav.volatilePreview = true } if gOpts.preload == (mode == "preload") { nav.regChan <- reg } }() var reader *bufio.Reader if len(gOpts.previewer) != 0 { cmd := exec.Command( gOpts.previewer, path, strconv.Itoa(win.w), strconv.Itoa(win.h), strconv.Itoa(win.x), strconv.Itoa(win.y), mode, ) var stderr bytes.Buffer cmd.Stderr = &stderr out, err := cmd.StdoutPipe() if err != nil { log.Printf("previewing file: %s", err) return } if err := cmd.Start(); err != nil { log.Printf("previewing file: %s", err) out.Close() return } defer func() { if err := cmd.Wait(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { reg.volatile = true } else { log.Printf("loading file: %s", err) } } if s := strings.TrimSpace(stderr.String()); s != "" { s = strings.Join(strings.Fields(s), " ") log.Printf("loading file (stderr): %s", s) } }() defer out.Close() reader = bufio.NewReader(out) } else { lstat, err := os.Lstat(path) if err != nil { log.Printf("lstat: %s", err) return } if !lstat.Mode().IsRegular() { return } f, err := os.Open(path) if err != nil { log.Printf("opening file: %s", err) return } defer f.Close() reader = bufio.NewReader(f) } lines, binary, sixel := readLines(reader, win.h) if binary { lines = []string{"\033[7mbinary\033[0m"} } reg.lines = lines reg.sixel = sixel } func (nav *nav) loadReg(path string, volatile bool) *reg { r, ok := nav.regCache[path] if !ok || (!gOpts.preload && r.loading) { r = ®{loading: true, loadTime: time.Now(), path: path} nav.regCache[path] = r if gOpts.preload { select { case nav.preloadChan <- path: default: } } else { nav.previewChan <- path } return r } if volatile && r.volatile { if !gOpts.preload { r.loadTime = time.Now() r.loading = true } nav.previewChan <- path } nav.checkReg(r) return r } func (nav *nav) checkReg(reg *reg) { s, err := os.Stat(reg.path) if err != nil { return } now := time.Now() // XXX: Linux builtin exFAT drivers are able to predict modifications in the future // https://bugs.launchpad.net/ubuntu/+source/ubuntu-meta/+bug/1872504 if s.ModTime().After(now) { return } if s.ModTime().After(reg.loadTime) { reg.loadTime = now nav.previewChan <- reg.path } } func (nav *nav) sort() { for _, path := range nav.dirPaths { dir := nav.getDir(path) name := dir.name() dir.sort() dir.sel(name, nav.height) } if curr := nav.currFile(); curr != nil && curr.IsDir() { dir := nav.getDir(curr.path) name := dir.name() dir.sort() dir.sel(name, nav.height) } } func (nav *nav) setFilter(filter []string) error { newfilter := []string{} for _, tok := range filter { if tok == "" { continue } // check if filter is valid by applying it to a dummy string if _, err := searchMatch("a", strings.TrimPrefix(tok, "!"), gOpts.filtermethod); err != nil { return err } newfilter = append(newfilter, tok) } dir := nav.currDir() dir.filter = newfilter // Apply filter, by sorting current dir (see nav.sort()) name := dir.name() dir.sort() dir.sel(name, nav.height) return nil } func (nav *nav) up(dist int) bool { dir := nav.currDir() old := dir.ind if dir.ind == 0 { if gOpts.wrapscroll { nav.bottom() dir.visualWrap-- } return old != dir.ind } dir.ind -= dist dir.ind = max(0, dir.ind) dir.pos -= dist dir.boundPos(nav.height) return old != dir.ind } func (nav *nav) down(dist int) bool { dir := nav.currDir() old := dir.ind maxind := len(dir.files) - 1 if dir.ind >= maxind { if gOpts.wrapscroll { nav.top() dir.visualWrap++ } return old != dir.ind } dir.ind += dist dir.ind = min(maxind, dir.ind) dir.pos += dist dir.boundPos(nav.height) return old != dir.ind } func (nav *nav) scrollUp(dist int) bool { dir := nav.currDir() old := dir.ind oldPos := dir.pos dir.pos += dist dir.boundPos(nav.height) dir.ind -= dist - (dir.pos - oldPos) dir.ind = max(dir.ind, dir.pos) return old != dir.ind } func (nav *nav) scrollDown(dist int) bool { dir := nav.currDir() old := dir.ind oldPos := dir.pos dir.pos -= dist dir.boundPos(nav.height) dir.ind += dist - (oldPos - dir.pos) dir.ind = min(dir.ind, dir.pos+max(len(dir.files)-nav.height, 0)) return old != dir.ind } func (nav *nav) updir() error { if len(nav.dirPaths) < 2 { return nil } if err := os.Chdir(nav.dirPaths[len(nav.dirPaths)-2]); err != nil { return fmt.Errorf("updir: %w", err) } nav.dirPaths = nav.dirPaths[:len(nav.dirPaths)-1] return nil } func (nav *nav) open() error { curr := nav.currFile() if curr == nil { return nil } if err := os.Chdir(curr.path); err != nil { return fmt.Errorf("open: %w", err) } nav.dirPaths = append(nav.dirPaths, curr.path) return nil } func (nav *nav) top() bool { dir := nav.currDir() old := dir.ind dir.ind = 0 dir.pos = 0 return old != dir.ind } func (nav *nav) bottom() bool { dir := nav.currDir() old := dir.ind dir.ind = max(len(dir.files)-1, 0) dir.pos = min(dir.ind, nav.height-1) return old != dir.ind } func (nav *nav) high() bool { dir := nav.currDir() old := dir.ind beg := max(dir.ind-dir.pos, 0) offs := min(nav.height/2, gOpts.scrolloff) if beg == 0 { offs = 0 } dir.ind = beg + offs dir.pos = offs return old != dir.ind } func (nav *nav) middle() bool { dir := nav.currDir() old := dir.ind beg := max(dir.ind-dir.pos, 0) end := min(beg+nav.height, len(dir.files)) half := (end - beg) / 2 dir.ind = beg + half dir.pos = half return old != dir.ind } func (nav *nav) low() bool { dir := nav.currDir() old := dir.ind beg := max(dir.ind-dir.pos, 0) end := min(beg+nav.height, len(dir.files)) offs := min(nav.height/2, gOpts.scrolloff) // use a smaller value for half when the height is even and scrolloff is // maxed in order to stay at the same row when using both high and low if nav.height%2 == 0 { offs = min(nav.height/2-1, gOpts.scrolloff) } if end == len(dir.files) { offs = 0 } dir.ind = end - 1 - offs dir.pos = end - beg - 1 - offs return old != dir.ind } func (nav *nav) move(index int) bool { old := nav.currDir().ind switch { case index < old: return nav.up(old - index) case index > old: return nav.down(index - old) default: return false } } func (nav *nav) toggleSelection(path string) { if _, ok := nav.selections[path]; ok { delete(nav.selections, path) if len(nav.selections) == 0 { nav.selectionInd = 0 } } else { nav.selections[path] = nav.selectionInd nav.selectionInd++ } } func (nav *nav) toggle() { if curr := nav.currFile(); curr != nil { nav.toggleSelection(curr.path) } } func (nav *nav) tagToggleSelection(path, tag string) { if _, ok := nav.tags[path]; ok { delete(nav.tags, path) } else { nav.tags[path] = tag } } func (nav *nav) tagToggle(tag string) error { list, err := nav.currFileOrSelections() if err != nil { return err } if printLength(tag) != 1 { return errors.New("tag should be single width character") } for _, path := range list { nav.tagToggleSelection(path, tag) } return nil } func (nav *nav) tag(tag string) error { list, err := nav.currFileOrSelections() if err != nil { return err } if printLength(tag) != 1 { return errors.New("tag should be single width character") } for _, path := range list { nav.tags[path] = tag } return nil } func (nav *nav) invert() { for _, file := range nav.currDir().files { nav.toggleSelection(file.path) } } func (nav *nav) unselect() { clear(nav.selections) nav.selectionInd = 0 } func (nav *nav) save(mode clipboardMode) error { list, err := nav.currFileOrSelections() if err != nil { return err } clipboard := clipboard{list, mode} if err := saveFiles(clipboard); err != nil { return err } nav.clipboard = clipboard return nil } func (nav *nav) copyAsync(app *app, srcs []string, dstDir string) { errCount := 0 sendErr := func(format string, a ...any) { errCount++ msg := fmt.Sprintf("copy [%d]: %s", errCount, fmt.Sprintf(format, a...)) app.ui.exprChan <- &callExpr{"echoerr", []string{msg}, 1} } _, err := os.Stat(dstDir) if os.IsNotExist(err) { sendErr("%v", err) return } // Indicate that a copy operation is in progress. Using the total bytes to // determine this instead will mean that it is possible for copySize to take // a while, but not be reflected in the UI until it has finished. nav.copyJobsChan <- 1 total, err := copySize(srcs) if err != nil { sendErr("%v", err) nav.copyJobsChan <- -1 return } nav.copyTotalChan <- total nums, errs := copyAll(srcs, dstDir, gOpts.preserve) loop: for { select { case n := <-nums: nav.copyBytesChan <- n case err, ok := <-errs: if !ok { break loop } sendErr("%v", err) } } nav.copyJobsChan <- -1 nav.copyTotalChan <- -total if gSingleMode { nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { sendErr("%v", err) } } if errCount == 0 { app.ui.exprChan <- &callExpr{"echo", []string{"\033[0;32mCopied successfully\033[0m"}, 1} } } func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) { errCount := 0 sendErr := func(format string, a ...any) { errCount++ msg := fmt.Sprintf("move [%d]: %s", errCount, fmt.Sprintf(format, a...)) app.ui.exprChan <- &callExpr{"echoerr", []string{msg}, 1} } _, err := os.Stat(dstDir) if os.IsNotExist(err) { sendErr("%v", err) return } nav.moveTotalChan <- len(srcs) for _, src := range srcs { nav.moveCountChan <- 1 srcStat, err := os.Lstat(src) if err != nil { sendErr("%v", err) continue } file := filepath.Base(src) dst := filepath.Join(dstDir, file) if dstStat, err := os.Stat(dst); err == nil { if os.SameFile(srcStat, dstStat) { sendErr("rename %s %s: source and destination are the same file", src, dst) continue } ext := getFileExtension(dstStat) basename := file[:len(file)-len(ext)] var newPath string for i := 1; !os.IsNotExist(err); i++ { file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext) file = strings.ReplaceAll(file, "%b", basename) file = strings.ReplaceAll(file, "%e", ext) file = strings.ReplaceAll(file, "%n", strconv.Itoa(i)) newPath = filepath.Join(dstDir, file) _, err = os.Lstat(newPath) } dst = newPath } if err := os.Rename(src, dst); err != nil { if errCrossDevice(err) { nav.copyJobsChan <- 1 total, err := copySize([]string{src}) if err != nil { sendErr("%v", err) nav.copyJobsChan <- -1 continue } nav.copyTotalChan <- total nums, errs := copyAll([]string{src}, dstDir, []string{"mode", "timestamps"}) oldCount := errCount loop: for { select { case n := <-nums: nav.copyBytesChan <- n case err, ok := <-errs: if !ok { break loop } sendErr("%v", err) } } nav.copyJobsChan <- -1 nav.copyTotalChan <- -total if errCount == oldCount { if err := os.RemoveAll(src); err != nil { sendErr("%v", err) } } } else { sendErr("%v", err) } } } nav.moveTotalChan <- -len(srcs) if gSingleMode { nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { sendErr("%v", err) } } if errCount == 0 { app.ui.exprChan <- &callExpr{"clear", nil, 1} app.ui.exprChan <- &callExpr{"echo", []string{"\033[0;32mMoved successfully\033[0m"}, 1} } } func (nav *nav) paste(app *app) error { clipboard, err := loadFiles() if err != nil { return err } if len(clipboard.paths) == 0 { return errors.New("no files in clipboard") } dstDir := nav.currDir().path if clipboard.mode == clipboardCopy { go nav.copyAsync(app, clipboard.paths, dstDir) } else { go nav.moveAsync(app, clipboard.paths, dstDir) } return nil } func (nav *nav) del(app *app) error { list, err := nav.currFileOrSelections() if err != nil { return err } go func() { echo := &callExpr{"echoerr", []string{""}, 1} errCount := 0 nav.deleteTotalChan <- len(list) for _, path := range list { nav.deleteCountChan <- 1 if err := os.RemoveAll(path); err != nil { errCount++ echo.args[0] = fmt.Sprintf("[%d] %s", errCount, err) app.ui.exprChan <- echo } } nav.deleteTotalChan <- -len(list) if gSingleMode { nav.renew() app.ui.loadFile(app, true) } else { if _, err := remote("send load"); err != nil { errCount++ echo.args[0] = fmt.Sprintf("[%d] %s", errCount, err) app.ui.exprChan <- echo } } }() return nil } func (nav *nav) rename() error { oldPath := nav.renameOldPath newPath := nav.renameNewPath if err := os.Rename(oldPath, newPath); err != nil { return err } lstat, err := os.Lstat(newPath) if err != nil { return err } // It is possible for newPath to already have cache entries if it previously // existed and was deleted. In this case the cache entries should be deleted // before loading newPath to prevent displaying a stale preview. However, // this clears only the current instance of lf, and not any other instances. deletePathRecursive(nav.regCache, newPath) deletePathRecursive(nav.dirCache, newPath) dir := nav.getDir(filepath.Dir(newPath)) nav.checkDir(dir) if dir.loading { for i := range dir.allFiles { if dir.allFiles[i].path == oldPath { dir.allFiles[i] = &file{FileInfo: lstat} break } } dir.sort() } dir.sel(lstat.Name(), nav.height) return nil } func (nav *nav) sync() error { clipboard, err := loadFiles() if err != nil { return err } nav.clipboard = clipboard tempmarks := make(map[string]string) for _, ch := range gOpts.tempmarks { k := string(ch) if v, ok := nav.marks[k]; ok { tempmarks[k] = v } } errMarks := nav.readMarks() maps.Copy(nav.marks, tempmarks) err = nav.readTags() if errMarks != nil { return errMarks } return err } func (nav *nav) cd(path string) error { if err := os.Chdir(path); err != nil { return err } nav.loadDirs(path) nav.addJumpList() return nil } func (nav *nav) globSel(pattern string, invert bool) error { dir := nav.currDir() anyMatched := false for i := range dir.files { matched, err := filepath.Match(pattern, dir.files[i].Name()) if err != nil { return fmt.Errorf("glob-select: %w", err) } if matched { anyMatched = true fpath := filepath.Join(dir.path, dir.files[i].Name()) if _, ok := nav.selections[fpath]; ok == invert { nav.toggleSelection(fpath) } } } if !anyMatched { return fmt.Errorf("glob-select: pattern not found: %s", pattern) } return nil } func findMatch(name, pattern string) bool { if gOpts.ignorecase { lpattern := strings.ToLower(pattern) if !gOpts.smartcase || lpattern == pattern { pattern = lpattern name = strings.ToLower(name) } } if gOpts.ignoredia { lpattern := removeDiacritics(pattern) if !gOpts.smartdia || lpattern == pattern { pattern = lpattern name = removeDiacritics(name) } } if gOpts.anchorfind { return strings.HasPrefix(name, pattern) } return strings.Contains(name, pattern) } func (nav *nav) findSingle() int { count := 0 index := 0 dir := nav.currDir() for i := range dir.files { if findMatch(dir.files[i].Name(), nav.find) { count++ if count > 1 { return count } index = i } } if count == 1 { if index > dir.ind { nav.down(index - dir.ind) } else { nav.up(dir.ind - index) } } return count } func (nav *nav) findNext() (bool, bool) { dir := nav.currDir() for i := dir.ind + 1; i < len(dir.files); i++ { if findMatch(dir.files[i].Name(), nav.find) { return nav.down(i - dir.ind), true } } if gOpts.wrapscan { for i := range dir.ind { if findMatch(dir.files[i].Name(), nav.find) { dir.visualWrap++ return nav.up(dir.ind - i), true } } } return false, false } func (nav *nav) findPrev() (bool, bool) { dir := nav.currDir() for i := dir.ind - 1; i >= 0; i-- { if findMatch(dir.files[i].Name(), nav.find) { return nav.up(dir.ind - i), true } } if gOpts.wrapscan { for i := len(dir.files) - 1; i > dir.ind; i-- { if findMatch(dir.files[i].Name(), nav.find) { dir.visualWrap-- return nav.down(i - dir.ind), true } } } return false, false } func searchMatch(name, pattern string, method searchMethod) (matched bool, err error) { if gOpts.ignorecase { lpattern := strings.ToLower(pattern) if !gOpts.smartcase || lpattern == pattern { pattern = lpattern name = strings.ToLower(name) } } if gOpts.ignoredia { lpattern := removeDiacritics(pattern) if !gOpts.smartdia || lpattern == pattern { pattern = lpattern name = removeDiacritics(name) } } switch method { case textSearch: return strings.Contains(name, pattern), nil case globSearch: return filepath.Match(pattern, name) case regexSearch: return regexp.MatchString(pattern, name) default: return false, errors.New("searchMatch: invalid searchMethod") } } func (nav *nav) searchNext() (bool, error) { dir := nav.currDir() for i := dir.ind + 1; i < len(dir.files); i++ { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { return nav.down(i - dir.ind), nil } } if gOpts.wrapscan { for i := range dir.ind { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { dir.visualWrap++ return nav.up(dir.ind - i), nil } } } return false, nil } func (nav *nav) searchPrev() (bool, error) { dir := nav.currDir() for i := dir.ind - 1; i >= 0; i-- { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { return nav.up(dir.ind - i), nil } } if gOpts.wrapscan { for i := len(dir.files) - 1; i > dir.ind; i-- { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { dir.visualWrap-- return nav.down(i - dir.ind), nil } } } return false, nil } func isFiltered(f os.FileInfo, filter []string) bool { for _, pattern := range filter { matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!"), gOpts.filtermethod) if err != nil { log.Printf("Filter Error: %s", err) return false } if strings.HasPrefix(pattern, "!") && matched { return true } else if !strings.HasPrefix(pattern, "!") && !matched { return true } } return false } func (nav *nav) removeMark(mark string) error { if _, ok := nav.marks[mark]; ok { delete(nav.marks, mark) return nil } return errors.New("no such mark") } func (nav *nav) readMarks() error { clear(nav.marks) f, err := os.Open(gMarksPath) if os.IsNotExist(err) { return nil } if err != nil { return fmt.Errorf("opening marks file: %w", err) } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { mark, path, found := strings.Cut(scanner.Text(), ":") if !found { return fmt.Errorf("invalid marks file entry: %s", scanner.Text()) } if _, ok := nav.marks[mark]; !ok { nav.marks[mark] = path } } if err := scanner.Err(); err != nil { return fmt.Errorf("reading marks file: %w", err) } return nil } func (nav *nav) writeMarks() error { if err := os.MkdirAll(filepath.Dir(gMarksPath), os.ModePerm); err != nil { return fmt.Errorf("creating data directory: %w", err) } f, err := os.Create(gMarksPath) if err != nil { return fmt.Errorf("creating marks file: %w", err) } defer f.Close() var keys []string for k := range nav.marks { if !strings.Contains(gOpts.tempmarks, k) { keys = append(keys, k) } } sort.Strings(keys) for _, k := range keys { _, err = fmt.Fprintf(f, "%s:%s\n", k, nav.marks[k]) if err != nil { return fmt.Errorf("writing marks file: %w", err) } } return nil } func (nav *nav) readTags() error { clear(nav.tags) f, err := os.Open(gTagsPath) if os.IsNotExist(err) { return nil } if err != nil { return fmt.Errorf("opening tags file: %w", err) } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { text := scanner.Text() ind := strings.LastIndex(text, ":") if ind == -1 { return fmt.Errorf("invalid tags file entry: %s", text) } path := text[0:ind] tag := text[ind+1:] if _, ok := nav.tags[path]; !ok { nav.tags[path] = tag } } if err := scanner.Err(); err != nil { return fmt.Errorf("reading tags file: %w", err) } return nil } func (nav *nav) writeTags() error { if err := os.MkdirAll(filepath.Dir(gTagsPath), os.ModePerm); err != nil { return fmt.Errorf("creating data directory: %w", err) } f, err := os.Create(gTagsPath) if err != nil { return fmt.Errorf("creating tags file: %w", err) } defer f.Close() var keys []string for k := range nav.tags { keys = append(keys, k) } sort.Strings(keys) for _, k := range keys { _, err = fmt.Fprintf(f, "%s:%s\n", k, nav.tags[k]) if err != nil { return fmt.Errorf("writing tags file: %w", err) } } return nil } func (nav *nav) currDir() *dir { if len(nav.dirPaths) == 0 { wd, err := os.Getwd() if err != nil { log.Printf("getting current directory: %s", err) } nav.loadDirs(wd) } path := nav.dirPaths[len(nav.dirPaths)-1] return nav.getDir(path) } func (nav *nav) currFile() *file { dir := nav.currDir() if len(dir.files) == 0 { return nil } return dir.files[dir.ind] } type indexedSelections struct { paths []string indices []int } func (m indexedSelections) Len() int { return len(m.paths) } func (m indexedSelections) Swap(i, j int) { m.paths[i], m.paths[j] = m.paths[j], m.paths[i] m.indices[i], m.indices[j] = m.indices[j], m.indices[i] } func (m indexedSelections) Less(i, j int) bool { return m.indices[i] < m.indices[j] } func (nav *nav) currSelections() []string { currDirOnly := gOpts.selmode == "dir" currDirPath := "" if currDirOnly { // select only from this directory currDirPath = nav.currDir().path } paths := make([]string, 0, len(nav.selections)) indices := make([]int, 0, len(nav.selections)) for path, index := range nav.selections { if !currDirOnly || filepath.Dir(path) == currDirPath { paths = append(paths, path) indices = append(indices, index) } } sort.Sort(indexedSelections{paths: paths, indices: indices}) return paths } func (nav *nav) currFileOrSelections() (list []string, err error) { if sel := nav.currSelections(); len(sel) > 0 { return sel, nil } if curr := nav.currFile(); curr != nil { return []string{curr.path}, nil } return nil, errors.New("no file selected") } func (nav *nav) calcDirSize() error { calc := func(f *file) error { if f.IsDir() { total, err := copySize([]string{f.path}) if err != nil { return err } f.dirSize = total } return nil } if len(nav.selections) == 0 { curr := nav.currFile() if curr == nil { return errors.New("no file selected") } return calc(curr) } for sel := range nav.selections { lstat, err := os.Lstat(sel) if err != nil || !lstat.IsDir() { continue } path, name := filepath.Dir(sel), filepath.Base(sel) dir := nav.getDir(path) for _, f := range dir.files { if f.Name() == name { err := calc(f) if err != nil { return err } break } } } return nil } ================================================ FILE: opts.go ================================================ package main import ( "fmt" "maps" "time" ) // String values match the sortby string sent by the user at startup type sortMethod string const ( naturalSort sortMethod = "natural" nameSort sortMethod = "name" sizeSort sortMethod = "size" timeSort sortMethod = "time" atimeSort sortMethod = "atime" btimeSort sortMethod = "btime" ctimeSort sortMethod = "ctime" extSort sortMethod = "ext" customSort sortMethod = "custom" ) func isValidSortMethod(method sortMethod) bool { switch method { case naturalSort, nameSort, sizeSort, timeSort, atimeSort, btimeSort, ctimeSort, extSort, customSort: return true } return false } const invalidSortErrorMessage = `sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'btime', 'ctime', 'ext' or 'custom'` type searchMethod string const ( textSearch searchMethod = "text" globSearch searchMethod = "glob" regexSearch searchMethod = "regex" ) type cursorStyle string const ( defaultCursor cursorStyle = "default" blockCursor cursorStyle = "block" underlineCursor cursorStyle = "underline" barCursor cursorStyle = "bar" blinkBlockCursor cursorStyle = "blinkblock" blinkUnderlineCursor cursorStyle = "blinkunderline" blinkBarCursor cursorStyle = "blinkbar" ) type borderStyle byte const ( borderOutline borderStyle = 1 << iota borderSeparators borderRound borderBox = borderOutline | borderSeparators borderRoundOutline = borderOutline | borderRound borderRoundBox = borderBox | borderRound ) func (s borderStyle) String() string { switch s { case borderBox: return "box" case borderRoundBox: return "roundbox" case borderOutline: return "outline" case borderRoundOutline: return "roundoutline" case borderSeparators: return "separators" default: return fmt.Sprintf("borderStyle(%d)", s) } } var gOpts struct { anchorfind bool autoquit bool borderfmt string borderstyle borderStyle cleaner string copyfmt string cursoractivefmt string cursorparentfmt string cursorpreviewfmt string cutfmt string dircounts bool dirfirst bool dironly bool dirpreviews bool drawbox bool dupfilefmt string errorfmt string filesep string filtermethod searchMethod findlen int hidden bool hiddenfiles []string history bool icons bool ifs string ignorecase bool ignoredia bool incfilter bool incsearch bool info []string infotimefmtnew string infotimefmtold string menufmt string menuheaderfmt string menuselectfmt string mergeindicators bool mouse bool number bool numbercursorfmt string numberfmt string period int preload bool preserve []string preview bool previewer string promptfmt string ratios []int relativenumber bool reverse bool rulerfile string rulerfmt string scrolloff int searchmethod searchMethod selectfmt string selmode string shell string shellflag string shellopts []string showbinds bool sizeunits string smartcase bool smartdia bool sortby sortMethod statfmt string tabstop int tagfmt string tempmarks string terminalcursor cursorStyle timefmt string truncatechar string truncatepct int visualfmt string waitmsg string watch bool wrapscan bool wrapscroll bool nkeys map[string]expr vkeys map[string]expr cmdkeys map[string]expr cmds map[string]expr user map[string]string } var gLocalOpts struct { dircounts map[string]bool dirfirst map[string]bool dironly map[string]bool hidden map[string]bool info map[string][]string reverse map[string]bool sortby map[string]sortMethod } func getDirCounts(path string) bool { if val, ok := gLocalOpts.dircounts[path]; ok { return val } return gOpts.dircounts } func getDirFirst(path string) bool { if val, ok := gLocalOpts.dirfirst[path]; ok { return val } return gOpts.dirfirst } func getDirOnly(path string) bool { if val, ok := gLocalOpts.dironly[path]; ok { return val } return gOpts.dironly } func getHidden(path string) bool { if val, ok := gLocalOpts.hidden[path]; ok { return val } return gOpts.hidden } func getInfo(path string) []string { if val, ok := gLocalOpts.info[path]; ok { return val } return gOpts.info } func getReverse(path string) bool { if val, ok := gLocalOpts.reverse[path]; ok { return val } return gOpts.reverse } func getSortBy(path string) sortMethod { if val, ok := gLocalOpts.sortby[path]; ok { return val } return gOpts.sortby } func init() { gOpts.anchorfind = true gOpts.autoquit = true gOpts.borderfmt = "\033[0m" gOpts.borderstyle = borderBox gOpts.cleaner = "" gOpts.copyfmt = "\033[7;33m" gOpts.cursoractivefmt = "\033[7m" gOpts.cursorparentfmt = "\033[7m" gOpts.cursorpreviewfmt = "\033[4m" gOpts.cutfmt = "\033[7;31m" gOpts.dircounts = false gOpts.dirfirst = true gOpts.dironly = false gOpts.dirpreviews = false gOpts.drawbox = false gOpts.dupfilefmt = "%f.~%n~" gOpts.errorfmt = "\033[7;31;47m" gOpts.filesep = "\n" gOpts.filtermethod = textSearch gOpts.findlen = 1 gOpts.hidden = false gOpts.hiddenfiles = gDefaultHiddenFiles gOpts.history = true gOpts.icons = false gOpts.ifs = "" gOpts.ignorecase = true gOpts.ignoredia = true gOpts.incfilter = false gOpts.incsearch = false gOpts.info = nil gOpts.infotimefmtnew = "Jan _2 15:04" gOpts.infotimefmtold = "Jan _2 2006" gOpts.menufmt = "\033[0m" gOpts.menuheaderfmt = "\033[1m" gOpts.menuselectfmt = "\033[7m" gOpts.mergeindicators = false gOpts.mouse = false gOpts.number = false gOpts.numbercursorfmt = "" gOpts.numberfmt = "\033[33m" gOpts.period = 0 gOpts.preload = false gOpts.preserve = []string{"mode"} gOpts.preview = true gOpts.previewer = "" gOpts.promptfmt = "\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m" gOpts.ratios = []int{1, 2, 3} gOpts.relativenumber = false gOpts.reverse = false gOpts.rulerfile = "" gOpts.rulerfmt = "" gOpts.scrolloff = 0 gOpts.searchmethod = textSearch gOpts.selectfmt = "\033[7;35m" gOpts.selmode = "all" gOpts.shell = gDefaultShell gOpts.shellflag = gDefaultShellFlag gOpts.shellopts = nil gOpts.showbinds = true gOpts.sizeunits = "binary" gOpts.smartcase = true gOpts.smartdia = false gOpts.sortby = naturalSort gOpts.statfmt = "\033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l" gOpts.tabstop = 8 gOpts.tagfmt = "\033[31m" gOpts.tempmarks = "'" gOpts.terminalcursor = defaultCursor gOpts.timefmt = time.ANSIC gOpts.truncatechar = "~" gOpts.truncatepct = 100 gOpts.visualfmt = "\033[7;36m" gOpts.waitmsg = "Press any key to continue" gOpts.watch = false gOpts.wrapscan = true gOpts.wrapscroll = false // Normal and Visual mode keys := map[string]expr{ "k": &callExpr{"up", nil, 1}, "": &callExpr{"up", nil, 1}, "": &callExpr{"up", nil, 1}, "": &callExpr{"half-up", nil, 1}, "": &callExpr{"page-up", nil, 1}, "": &callExpr{"page-up", nil, 1}, "": &callExpr{"scroll-up", nil, 1}, "": &callExpr{"scroll-up", nil, 1}, "j": &callExpr{"down", nil, 1}, "": &callExpr{"down", nil, 1}, "": &callExpr{"down", nil, 1}, "": &callExpr{"half-down", nil, 1}, "": &callExpr{"page-down", nil, 1}, "": &callExpr{"page-down", nil, 1}, "": &callExpr{"scroll-down", nil, 1}, "": &callExpr{"scroll-down", nil, 1}, "h": &callExpr{"updir", nil, 1}, "": &callExpr{"updir", nil, 1}, "l": &callExpr{"open", nil, 1}, "": &callExpr{"open", nil, 1}, "q": &callExpr{"quit", nil, 1}, "gg": &callExpr{"top", nil, 1}, "": &callExpr{"top", nil, 1}, "G": &callExpr{"bottom", nil, 1}, "": &callExpr{"bottom", nil, 1}, "H": &callExpr{"high", nil, 1}, "M": &callExpr{"middle", nil, 1}, "L": &callExpr{"low", nil, 1}, "[": &callExpr{"jump-prev", nil, 1}, "]": &callExpr{"jump-next", nil, 1}, "t": &callExpr{"tag-toggle", nil, 1}, "u": &callExpr{"unselect", nil, 1}, "y": &callExpr{"copy", nil, 1}, "d": &callExpr{"cut", nil, 1}, "c": &callExpr{"clear", nil, 1}, "p": &callExpr{"paste", nil, 1}, "": &callExpr{"redraw", nil, 1}, "": &callExpr{"reload", nil, 1}, ":": &callExpr{"read", nil, 1}, "$": &callExpr{"shell", nil, 1}, "%": &callExpr{"shell-pipe", nil, 1}, "!": &callExpr{"shell-wait", nil, 1}, "&": &callExpr{"shell-async", nil, 1}, "f": &callExpr{"find", nil, 1}, "F": &callExpr{"find-back", nil, 1}, ";": &callExpr{"find-next", nil, 1}, ",": &callExpr{"find-prev", nil, 1}, "/": &callExpr{"search", nil, 1}, "?": &callExpr{"search-back", nil, 1}, "n": &callExpr{"search-next", nil, 1}, "N": &callExpr{"search-prev", nil, 1}, "m": &callExpr{"mark-save", nil, 1}, "'": &callExpr{"mark-load", nil, 1}, `"`: &callExpr{"mark-remove", nil, 1}, `r`: &callExpr{"rename", nil, 1}, "": &callExpr{"cmd-history-next", nil, 1}, "": &callExpr{"cmd-history-prev", nil, 1}, "zh": &setExpr{"hidden!", ""}, "zr": &setExpr{"reverse!", ""}, "zn": &setExpr{"info", ""}, "zs": &setExpr{"info", "size"}, "zt": &setExpr{"info", "time"}, "za": &setExpr{"info", "size:time"}, "sn": &listExpr{[]expr{&setExpr{"sortby", "natural"}, &setExpr{"info", ""}}, 1}, "ss": &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1}, "st": &listExpr{[]expr{&setExpr{"sortby", "time"}, &setExpr{"info", "time"}}, 1}, "sa": &listExpr{[]expr{&setExpr{"sortby", "atime"}, &setExpr{"info", "atime"}}, 1}, "sb": &listExpr{[]expr{&setExpr{"sortby", "btime"}, &setExpr{"info", "btime"}}, 1}, "sc": &listExpr{[]expr{&setExpr{"sortby", "ctime"}, &setExpr{"info", "ctime"}}, 1}, "se": &listExpr{[]expr{&setExpr{"sortby", "ext"}, &setExpr{"info", ""}}, 1}, "gh": &callExpr{"cd", []string{"~"}, 1}, } // insert bindings that apply to both Normal & Visual mode first gOpts.nkeys = maps.Clone(keys) // now add Normal mode specific ones gOpts.nkeys[""] = &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1} gOpts.nkeys["V"] = &callExpr{"visual", nil, 1} gOpts.nkeys["v"] = &callExpr{"invert", nil, 1} // now do the same for Visual mode gOpts.vkeys = maps.Clone(keys) gOpts.vkeys[""] = &callExpr{"visual-discard", nil, 1} gOpts.vkeys["V"] = &callExpr{"visual-accept", nil, 1} gOpts.vkeys["o"] = &callExpr{"visual-change", nil, 1} // Command-line mode bindings can be assigned directly gOpts.cmdkeys = map[string]expr{ "": &callExpr{"cmd-insert", []string{" "}, 1}, "": &callExpr{"cmd-escape", nil, 1}, "": &callExpr{"cmd-complete", nil, 1}, "": &callExpr{"cmd-enter", nil, 1}, "": &callExpr{"cmd-enter", nil, 1}, "": &callExpr{"cmd-history-next", nil, 1}, "": &callExpr{"cmd-history-next", nil, 1}, "": &callExpr{"cmd-history-prev", nil, 1}, "": &callExpr{"cmd-history-prev", nil, 1}, "": &callExpr{"cmd-delete", nil, 1}, "": &callExpr{"cmd-delete", nil, 1}, "": &callExpr{"cmd-delete-back", nil, 1}, "": &callExpr{"cmd-left", nil, 1}, "": &callExpr{"cmd-left", nil, 1}, "": &callExpr{"cmd-right", nil, 1}, "": &callExpr{"cmd-right", nil, 1}, "": &callExpr{"cmd-home", nil, 1}, "": &callExpr{"cmd-home", nil, 1}, "": &callExpr{"cmd-end", nil, 1}, "": &callExpr{"cmd-end", nil, 1}, "": &callExpr{"cmd-delete-home", nil, 1}, "": &callExpr{"cmd-delete-end", nil, 1}, "": &callExpr{"cmd-delete-unix-word", nil, 1}, "": &callExpr{"cmd-yank", nil, 1}, "": &callExpr{"cmd-transpose", nil, 1}, "": &callExpr{"cmd-interrupt", nil, 1}, "": &callExpr{"cmd-word", nil, 1}, "": &callExpr{"cmd-word-back", nil, 1}, "": &callExpr{"cmd-capitalize-word", nil, 1}, "": &callExpr{"cmd-delete-word", nil, 1}, "": &callExpr{"cmd-delete-word-back", nil, 1}, "": &callExpr{"cmd-uppercase-word", nil, 1}, "": &callExpr{"cmd-lowercase-word", nil, 1}, "": &callExpr{"cmd-transpose-word", nil, 1}, } gOpts.cmds = make(map[string]expr) gOpts.user = make(map[string]string) gLocalOpts.dircounts = make(map[string]bool) gLocalOpts.dirfirst = make(map[string]bool) gLocalOpts.dironly = make(map[string]bool) gLocalOpts.hidden = make(map[string]bool) gLocalOpts.info = make(map[string][]string) gLocalOpts.reverse = make(map[string]bool) gLocalOpts.sortby = make(map[string]sortMethod) setDefaults() } ================================================ FILE: os.go ================================================ //go:build !windows package main import ( "cmp" "fmt" "log" "os" "os/exec" "os/user" "path/filepath" "runtime" "strconv" "strings" "syscall" "golang.org/x/sys/unix" ) var ( envOpener = os.Getenv("OPENER") envEditor = os.Getenv("VISUAL") envPager = os.Getenv("PAGER") envShell = os.Getenv("SHELL") ) var ( gDefaultShell = "sh" gDefaultShellFlag = "-c" gDefaultSocketProt = "unix" gDefaultSocketPath string gDefaultHiddenFiles = []string{".*"} ) var ( gUser *user.User gConfigPaths []string gColorsPaths []string gIconsPaths []string gFilesPath string gMarksPath string gTagsPath string gHistoryPath string ) func init() { if envOpener == "" { if runtime.GOOS == "darwin" { envOpener = "open" } else { envOpener = "xdg-open" } } if envEditor == "" { envEditor = cmp.Or(os.Getenv("EDITOR"), "vi") } if envPager == "" { envPager = "less" } if envShell == "" { envShell = "sh" } u, err := user.Current() if err != nil { // When the user is not in /etc/passwd (for e.g. LDAP) and CGO_ENABLED=1 in go env, // the cgo implementation of user.Current() fails even when HOME and USER are set. log.Printf("user: %s", err) if os.Getenv("HOME") == "" { panic("$HOME variable is empty or not set") } if os.Getenv("USER") == "" { panic("$USER variable is empty or not set") } u = &user.User{ Username: os.Getenv("USER"), HomeDir: os.Getenv("HOME"), } } gUser = u config := cmp.Or( os.Getenv("LF_CONFIG_HOME"), os.Getenv("XDG_CONFIG_HOME"), filepath.Join(gUser.HomeDir, ".config"), ) gConfigPaths = []string{ filepath.Join("/etc", "lf", "lfrc"), filepath.Join(config, "lf", "lfrc"), } gColorsPaths = []string{ filepath.Join("/etc", "lf", "colors"), filepath.Join(config, "lf", "colors"), } gIconsPaths = []string{ filepath.Join("/etc", "lf", "icons"), filepath.Join(config, "lf", "icons"), } data := cmp.Or( os.Getenv("LF_DATA_HOME"), os.Getenv("XDG_DATA_HOME"), filepath.Join(gUser.HomeDir, ".local", "share"), ) gFilesPath = filepath.Join(data, "lf", "files") gMarksPath = filepath.Join(data, "lf", "marks") gTagsPath = filepath.Join(data, "lf", "tags") gHistoryPath = filepath.Join(data, "lf", "history") runtime := cmp.Or(os.Getenv("XDG_RUNTIME_DIR"), os.TempDir()) gDefaultSocketPath = filepath.Join(runtime, fmt.Sprintf("lf.%s.sock", gUser.Username)) } func detachedCommand(name string, arg ...string) *exec.Cmd { cmd := exec.Command(name, arg...) cmd.SysProcAttr = &unix.SysProcAttr{Setsid: true} return cmd } func shellCommand(s string, args []string) *exec.Cmd { if len(gOpts.ifs) != 0 { s = fmt.Sprintf("IFS='%s'; %s", gOpts.ifs, s) } args = append([]string{gOpts.shellflag, s, "--"}, args...) args = append(gOpts.shellopts, args...) return exec.Command(gOpts.shell, args...) } func shellSetPG(cmd *exec.Cmd) { cmd.SysProcAttr = &unix.SysProcAttr{Setpgid: true} } func shellKill(cmd *exec.Cmd) error { pgid, err := unix.Getpgid(cmd.Process.Pid) if err == nil && cmd.Process.Pid == pgid { // kill the process group err = unix.Kill(-pgid, syscall.SIGTERM) if err == nil { return nil } } return cmd.Process.Kill() } func setDefaults() { gOpts.cmds["open"] = &execExpr{"&", `$OPENER "$f"`} gOpts.nkeys["e"] = &execExpr{"$", `$EDITOR "$f"`} gOpts.vkeys["e"] = &execExpr{"$", `$EDITOR "$f"`} gOpts.nkeys["i"] = &execExpr{"$", `$PAGER "$f"`} gOpts.vkeys["i"] = &execExpr{"$", `$PAGER "$f"`} gOpts.nkeys["w"] = &execExpr{"$", "$SHELL"} gOpts.vkeys["w"] = &execExpr{"$", "$SHELL"} gOpts.cmds["help"] = &execExpr{"$", `"$lf" -doc | $PAGER`} gOpts.nkeys[""] = &callExpr{"help", nil, 1} gOpts.vkeys[""] = &callExpr{"help", nil, 1} gOpts.cmds["maps"] = &execExpr{"$", `"$lf" -remote "query $id maps" | $PAGER`} gOpts.cmds["nmaps"] = &execExpr{"$", `"$lf" -remote "query $id nmaps" | $PAGER`} gOpts.cmds["vmaps"] = &execExpr{"$", `"$lf" -remote "query $id vmaps" | $PAGER`} gOpts.cmds["cmaps"] = &execExpr{"$", `"$lf" -remote "query $id cmaps" | $PAGER`} gOpts.cmds["cmds"] = &execExpr{"$", `"$lf" -remote "query $id cmds" | $PAGER`} } func setUserUmask() { unix.Umask(0o077) } func isExecutable(f os.FileInfo) bool { return f.Mode()&0o111 != 0 } func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool { hidden := false for _, pattern := range hiddenfiles { if matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) { hidden = !strings.HasPrefix(pattern, "!") } } return hidden } func userName(f os.FileInfo) string { if stat, ok := f.Sys().(*syscall.Stat_t); ok { uid := strconv.FormatUint(uint64(stat.Uid), 10) if u, err := user.LookupId(uid); err == nil { return u.Username } return uid } return "" } func groupName(f os.FileInfo) string { if stat, ok := f.Sys().(*syscall.Stat_t); ok { gid := strconv.FormatUint(uint64(stat.Gid), 10) if g, err := user.LookupGroupId(gid); err == nil { return g.Name } return gid } return "" } func linkCount(f os.FileInfo) string { if stat, ok := f.Sys().(*syscall.Stat_t); ok { return strconv.FormatUint(uint64(stat.Nlink), 10) } return "" } func errCrossDevice(err error) bool { return err.(*os.LinkError).Err.(unix.Errno) == unix.EXDEV } func quoteString(s string) string { return s } func shellEscape(s string) string { buf := make([]rune, 0, len(s)) for _, r := range s { if strings.ContainsRune(" !\"$&'()*,:;<=>?@[\\]^`{|}", r) { buf = append(buf, '\\') } buf = append(buf, r) } return string(buf) } func shellUnescape(s string) string { esc := false buf := make([]rune, 0, len(s)) for _, r := range s { if r == '\\' && !esc { esc = true continue } esc = false buf = append(buf, r) } if esc { buf = append(buf, '\\') } return string(buf) } ================================================ FILE: os_windows.go ================================================ package main import ( "cmp" "fmt" "os" "os/exec" "os/user" "path/filepath" "strings" "syscall" "golang.org/x/sys/windows" ) var ( envOpener = os.Getenv("OPENER") envEditor = os.Getenv("VISUAL") envPager = os.Getenv("PAGER") envShell = os.Getenv("SHELL") ) var envPathExts []string var ( gDefaultShell = "cmd" gDefaultShellFlag = "/c" gDefaultSocketProt = "unix" gDefaultSocketPath string gDefaultHiddenFiles []string ) var ( gUser *user.User gConfigPaths []string gColorsPaths []string gIconsPaths []string gFilesPath string gTagsPath string gMarksPath string gHistoryPath string ) func init() { if envOpener == "" { envOpener = `start ""` } if envEditor == "" { envEditor = cmp.Or(os.Getenv("EDITOR"), "notepad") } if envPager == "" { envPager = "more" } if envShell == "" { envShell = "cmd" } homeDir, err := os.UserHomeDir() if err != nil { panic(err) } username := os.Getenv("USERNAME") if username == "" { panic("$USERNAME variable is empty or not set") } gUser = &user.User{ HomeDir: homeDir, Username: username, } config := cmp.Or( os.Getenv("LF_CONFIG_HOME"), os.Getenv("XDG_CONFIG_HOME"), os.Getenv("APPDATA"), ) gConfigPaths = []string{ filepath.Join(os.Getenv("ProgramData"), "lf", "lfrc"), filepath.Join(config, "lf", "lfrc"), } gColorsPaths = []string{ filepath.Join(os.Getenv("ProgramData"), "lf", "colors"), filepath.Join(config, "lf", "colors"), } gIconsPaths = []string{ filepath.Join(os.Getenv("ProgramData"), "lf", "icons"), filepath.Join(config, "lf", "icons"), } data := cmp.Or( os.Getenv("LF_DATA_HOME"), os.Getenv("XDG_DATA_HOME"), os.Getenv("LOCALAPPDATA"), ) gFilesPath = filepath.Join(data, "lf", "files") gMarksPath = filepath.Join(data, "lf", "marks") gTagsPath = filepath.Join(data, "lf", "tags") gHistoryPath = filepath.Join(data, "lf", "history") socket, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) if err != nil { gDefaultSocketProt = "tcp" gDefaultSocketPath = "127.0.0.1:12345" } else { runtime := os.TempDir() gDefaultSocketPath = filepath.Join(runtime, fmt.Sprintf("lf.%s.sock", gUser.Username)) syscall.Close(socket) } s := cmp.Or(os.Getenv("PATHEXT"), ".COM;.EXE;.BAT;.CMD") for ext := range strings.SplitSeq(s, ";") { if ext == "" { continue } envPathExts = append(envPathExts, strings.ToLower(ext)) } } func detachedCommand(name string, arg ...string) *exec.Cmd { cmd := exec.Command(name, arg...) cmd.SysProcAttr = &windows.SysProcAttr{CreationFlags: 8} return cmd } func shellCommand(s string, args []string) *exec.Cmd { // Windows CMD requires special handling to deal with quoted arguments if strings.ToLower(gOpts.shell) == "cmd" { var builder strings.Builder builder.WriteString(s) for _, arg := range args { fmt.Fprintf(&builder, ` "%s"`, arg) } shellOpts := strings.Join(gOpts.shellopts, " ") cmdline := fmt.Sprintf(`%s %s %s "%s"`, gOpts.shell, shellOpts, gOpts.shellflag, builder.String()) cmd := exec.Command(gOpts.shell) cmd.SysProcAttr = &windows.SysProcAttr{CmdLine: cmdline} return cmd } args = append([]string{gOpts.shellflag, s}, args...) args = append(gOpts.shellopts, args...) return exec.Command(gOpts.shell, args...) } func shellSetPG(_ *exec.Cmd) { } func shellKill(cmd *exec.Cmd) error { return cmd.Process.Kill() } func setDefaults() { gOpts.cmds["open"] = &execExpr{"&", "%OPENER% %f%"} gOpts.nkeys["e"] = &execExpr{"$", "%EDITOR% %f%"} gOpts.vkeys["e"] = &execExpr{"$", "%EDITOR% %f%"} gOpts.nkeys["i"] = &execExpr{"!", "%PAGER% %f%"} gOpts.vkeys["i"] = &execExpr{"!", "%PAGER% %f%"} gOpts.nkeys["w"] = &execExpr{"$", "%SHELL%"} gOpts.vkeys["w"] = &execExpr{"$", "%SHELL%"} gOpts.cmds["help"] = &execExpr{"!", "%lf% -doc | %PAGER%"} gOpts.nkeys[""] = &callExpr{"help", nil, 1} gOpts.vkeys[""] = &callExpr{"help", nil, 1} gOpts.cmds["maps"] = &execExpr{"!", `%lf% -remote "query %id% maps" | %PAGER%`} gOpts.cmds["nmaps"] = &execExpr{"!", `%lf% -remote "query %id% nmaps" | %PAGER%`} gOpts.cmds["vmaps"] = &execExpr{"!", `%lf% -remote "query %id% vmaps" | %PAGER%`} gOpts.cmds["cmaps"] = &execExpr{"!", `%lf% -remote "query %id% cmaps" | %PAGER%`} gOpts.cmds["cmds"] = &execExpr{"!", `%lf% -remote "query %id% cmds" | %PAGER%`} } func setUserUmask() {} func isExecutable(f os.FileInfo) bool { ext := filepath.Ext(f.Name()) if ext == "" { return false } ext = strings.ToLower(ext) for _, x := range envPathExts { if ext == x { return true } } return false } func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool { ptr, err := windows.UTF16PtrFromString(filepath.Join(path, f.Name())) if err != nil { return false } attrs, err := windows.GetFileAttributes(ptr) if err != nil { return false } if attrs&windows.FILE_ATTRIBUTE_HIDDEN != 0 { return true } hidden := false for _, pattern := range hiddenfiles { if matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) { hidden = !strings.HasPrefix(pattern, "!") } } return hidden } func userName(_ os.FileInfo) string { return "" } func groupName(_ os.FileInfo) string { return "" } func linkCount(_ os.FileInfo) string { return "" } func errCrossDevice(err error) bool { return err.(*os.LinkError).Err.(windows.Errno) == windows.ERROR_NOT_SAME_DEVICE } func quoteString(s string) string { // Windows CMD requires special handling to deal with quoted arguments if strings.ToLower(gOpts.shell) == "cmd" { return fmt.Sprintf(`"%s"`, s) } return s } func shellEscape(s string) string { for _, r := range s { if strings.ContainsRune(" !%&'()+,;=[]^`{}~", r) { return fmt.Sprintf(`"%s"`, s) } } return s } func shellUnescape(s string) string { return strings.ReplaceAll(s, `"`, "") } ================================================ FILE: parse.go ================================================ package main // Grammar of the language used in the evaluator // // Expr = SetExpr // | SetLocalExpr // | MapExpr // | NMapExpr // | VMapExpr // | CMapExpr // | CmdExpr // | CallExpr // | ExecExpr // | ListExpr // // SetExpr = 'set' ';' // // SetLocalExpr = 'setlocal' ';' // // MapExpr = 'map' Expr // // NMapExpr = 'nmap' Expr // // VMapExpr = 'vmap' Expr // // CMapExpr = 'cmap' Expr // // CmdExpr = 'cmd' Expr // // CallExpr = ';' // // ExecExpr = Prefix '\n' // | Prefix '{{' '}}' ';' // // Prefix = '$' | '%' | '!' | '&' // // ListExpr = ':' Expr ListRest '\n' // | ':' '{{' Expr ListRest '}}' ';' // // ListRest = Nil // | Expr ListExpr import ( "bytes" "fmt" "io" "strings" ) type expr interface { String() string eval(app *app, args []string) } type setExpr struct { opt string val string } func (e *setExpr) String() string { if e.val == "" { return fmt.Sprintf("set %s", e.opt) } return fmt.Sprintf("set %s %s", e.opt, e.val) } type setLocalExpr struct { path string opt string val string } func (e *setLocalExpr) String() string { if e.val == "" { return fmt.Sprintf("setlocal %s %s", e.path, e.opt) } return fmt.Sprintf("setlocal %s %s %s", e.path, e.opt, e.val) } type mapExpr struct { keys string expr expr } func (e *mapExpr) String() string { if e.expr == nil { return fmt.Sprintf("map %s", e.keys) } return fmt.Sprintf("map %s %s", e.keys, e.expr) } type nmapExpr struct { keys string expr expr } func (e *nmapExpr) String() string { if e.expr == nil { return fmt.Sprintf("nmap %s", e.keys) } return fmt.Sprintf("nmap %s %s", e.keys, e.expr) } type vmapExpr struct { keys string expr expr } func (e *vmapExpr) String() string { if e.expr == nil { return fmt.Sprintf("vmap %s", e.keys) } return fmt.Sprintf("vmap %s %s", e.keys, e.expr) } type cmapExpr struct { key string expr expr } func (e *cmapExpr) String() string { if e.expr == nil { return fmt.Sprintf("cmap %s", e.key) } return fmt.Sprintf("cmap %s %s", e.key, e.expr) } type cmdExpr struct { name string expr expr } func (e *cmdExpr) String() string { if e.expr == nil { return fmt.Sprintf("cmd %s", e.name) } return fmt.Sprintf("cmd %s %s", e.name, e.expr) } type callExpr struct { name string args []string count int } func (e *callExpr) String() string { return strings.Join(append([]string{e.name}, e.args...), " ") } type execExpr struct { prefix string value string } func (e *execExpr) String() string { var buf bytes.Buffer buf.WriteString(e.prefix) buf.WriteString("{{ ") lines := strings.Split(e.value, "\n") for _, line := range lines { trimmed := strings.TrimSpace(line) if trimmed == "" { continue } buf.WriteString(trimmed) if len(lines) > 1 { buf.WriteString(" ...") } break } buf.WriteString(" }}") return buf.String() } type listExpr struct { exprs []expr count int } func (e *listExpr) String() string { var buf bytes.Buffer buf.WriteString(":{{ ") for _, expr := range e.exprs { buf.WriteString(expr.String()) buf.WriteString("; ") } buf.WriteString("}}") return buf.String() } type parser struct { scanner *scanner expr expr err error } func newParser(r io.Reader) *parser { scanner := newScanner(r) scanner.scan() return &parser{ scanner: scanner, } } func (p *parser) parseExpr() expr { s := p.scanner var result expr switch s.typ { case tokenEOF: return nil case tokenIdent: switch s.tok { case "set": var val string s.scan() if s.typ != tokenIdent { p.err = fmt.Errorf("expected identifier: %s", s.tok) } opt := s.tok s.scan() if s.typ != tokenSemicolon { val = s.tok s.scan() } s.scan() result = &setExpr{opt, val} case "setlocal": var val string s.scan() if s.typ != tokenIdent { p.err = fmt.Errorf("expected directory: %s", s.tok) } dir := s.tok s.scan() if s.typ != tokenIdent { p.err = fmt.Errorf("expected identifier: %s", s.tok) } opt := s.tok s.scan() if s.typ != tokenSemicolon { val = s.tok s.scan() } s.scan() result = &setLocalExpr{dir, opt, val} case "map": var expr expr s.scan() keys := s.tok s.scan() if s.typ != tokenSemicolon { expr = p.parseExpr() } else { s.scan() } result = &mapExpr{keys, expr} case "nmap": var expr expr s.scan() keys := s.tok s.scan() if s.typ != tokenSemicolon { expr = p.parseExpr() } else { s.scan() } result = &nmapExpr{keys, expr} case "vmap": var expr expr s.scan() keys := s.tok s.scan() if s.typ != tokenSemicolon { expr = p.parseExpr() } else { s.scan() } result = &vmapExpr{keys, expr} case "cmap": var expr expr s.scan() key := s.tok s.scan() if s.typ != tokenSemicolon { expr = p.parseExpr() } else { s.scan() } result = &cmapExpr{key, expr} case "cmd": var expr expr s.scan() name := s.tok s.scan() if s.typ != tokenSemicolon { expr = p.parseExpr() } else { s.scan() } result = &cmdExpr{name, expr} default: name := s.tok var args []string for s.scan() && s.typ != tokenSemicolon { args = append(args, s.tok) } s.scan() result = &callExpr{name, args, 1} } case tokenColon: s.scan() var exprs []expr if s.typ == tokenLBraces { s.scan() for { e := p.parseExpr() if e == nil { return nil } exprs = append(exprs, e) if s.typ == tokenRBraces { break } } s.scan() } else { for { e := p.parseExpr() if e == nil { return nil } exprs = append(exprs, e) if s.tok == "\n" { break } } } s.scan() result = &listExpr{exprs, 1} case tokenPrefix: var expr string prefix := s.tok s.scan() if s.typ == tokenLBraces { s.scan() expr = s.tok s.scan() } else { expr = s.tok } s.scan() s.scan() result = &execExpr{prefix, expr} default: p.err = fmt.Errorf("unexpected token: %s", s.tok) } return result } func (p *parser) parse() bool { p.expr = p.parseExpr() return p.expr != nil } ================================================ FILE: ruler.go ================================================ package main import ( "fmt" "os" "path/filepath" "strings" "text/template" _ "embed" ) //go:embed etc/ruler.default var gDefaultRuler string type statData struct { Path string Name string Extension string Size int64 DirSize int64 DirCount int Permissions string ModTime string AccessTime string BirthTime string ChangeTime string LinkCount string User string Group string Target string CustomInfo string } type rulerData struct { SPACER string Message string Keys string Progress []string Copy []string Cut []string Select []string Visual []string Index int Total int Hidden int All int LinePercentage string ScrollPercentage string Filter []string Mode string Options map[string]string UserOptions map[string]string Stat *statData } func parseRuler(path string) (*template.Template, error) { funcs := template.FuncMap{ "df": func() string { return diskFree(".") }, "env": os.Getenv, "humanize": humanize, "join": strings.Join, "lower": strings.ToLower, "substr": func(s string, start, length int) string { return string([]rune(s)[start : start+length]) }, "upper": strings.ToUpper, } if path == "" { return template.New("ruler").Funcs(funcs).Parse(gDefaultRuler) } return template.New(filepath.Base(path)).Funcs(funcs).ParseFiles(path) } func renderRuler(ruler *template.Template, data rulerData, width int) (string, string, error) { var b strings.Builder if err := ruler.Execute(&b, data); err != nil { return "", "", err } s := strings.ReplaceAll(b.String(), "\n", "") sections := strings.Split(s, "\x1f") if len(sections) == 1 { return s, "", nil } wtot := 0 for _, section := range sections { wtot += printLength(section) } if wtot > width { return sections[0], strings.Join(sections[1:], ""), nil } wspacer := max(width-wtot, 0) / (len(sections) - 1) wspacerLast := max(width-wtot-wspacer*(len(sections)-2), 0) b.Reset() for i, section := range sections { switch i { case 0: b.WriteString(section) case len(sections) - 1: fmt.Fprintf(&b, "%*s%s", wspacerLast, "", section) default: fmt.Fprintf(&b, "%*s%s", wspacer, "", section) } } return b.String(), "", nil } ================================================ FILE: scan.go ================================================ package main import ( "io" "log" "strconv" ) type tokenType byte const ( tokenEOF tokenType = iota // no explicit keyword type tokenIdent // e.g. set, ratios, 1:2:3 tokenColon // : tokenPrefix // $, %, !, & tokenLBraces // {{ tokenRBraces // }} tokenCommand // in between a prefix to \n or between {{ and }} tokenSemicolon // ; // comments are stripped ) type scanner struct { buf []byte // input buffer off int // current offset in buf chr byte // current character in buf sem bool // insert semicolon nln bool // insert newline eof bool // buffer ended key bool // scanning keys blk bool // scanning block cmd bool // scanning command typ tokenType // scanned token type tok string // scanned token value // TODO: pos } func newScanner(r io.Reader) *scanner { buf, err := io.ReadAll(r) if err != nil { log.Printf("scanning: %s", err) } var eof bool var chr byte if len(buf) == 0 { eof = true } else { eof = false chr = buf[0] } return &scanner{ buf: buf, eof: eof, chr: chr, } } func (s *scanner) next() { if s.off+1 < len(s.buf) { s.off++ s.chr = s.buf[s.off] return } s.off = len(s.buf) s.chr = 0 s.eof = true } func (s *scanner) peek() byte { if s.off+1 < len(s.buf) { return s.buf[s.off+1] } return 0 } func isSpace(b byte) bool { switch b { case '\t', '\n', '\v', '\f', '\r', ' ': return true } return false } func isDigit(b byte) bool { return '0' <= b && b <= '9' } func isPrefix(b byte) bool { switch b { case '$', '%', '!', '&': return true } return false } func (s *scanner) scan() bool { scan: switch { case s.eof: s.next() if s.sem { s.typ = tokenSemicolon s.tok = "\n" s.sem = false return true } if s.nln { s.typ = tokenSemicolon s.tok = "\n" s.nln = false return true } s.typ = tokenEOF s.tok = "EOF" return false case s.key: beg := s.off for !s.eof && !isSpace(s.chr) { s.next() } s.typ = tokenIdent s.tok = string(s.buf[beg:s.off]) s.key = false case s.blk: // return here by setting s.cmd to false // after scanning the command in the loop below if !s.cmd { s.next() s.next() s.typ = tokenRBraces s.tok = "}}" s.blk = false s.sem = true return true } beg := s.off for !s.eof { s.next() if s.chr == '}' { if !s.eof && s.peek() == '}' { s.typ = tokenCommand s.tok = string(s.buf[beg:s.off]) s.cmd = false return true } } } s.typ = tokenEOF s.tok = "EOF" return false case s.cmd: for !s.eof && isSpace(s.chr) { s.next() } if !s.eof && s.chr == '{' { if s.peek() == '{' { s.next() s.next() s.typ = tokenLBraces s.tok = "{{" s.blk = true return true } } beg := s.off for !s.eof && s.chr != '\n' { s.next() } s.typ = tokenCommand s.tok = string(s.buf[beg:s.off]) s.cmd = false s.sem = true case s.chr == '\n': if s.sem { s.typ = tokenSemicolon s.tok = "\n" s.sem = false return true } s.next() if s.nln { s.typ = tokenSemicolon s.tok = "\n" s.nln = false return true } goto scan case isSpace(s.chr): for !s.eof && isSpace(s.chr) && s.chr != '\n' { s.next() } goto scan case s.chr == ';': s.typ = tokenSemicolon s.tok = ";" s.sem = false s.next() case s.chr == '#': for !s.eof && s.chr != '\n' { s.next() } goto scan case s.chr == '"': s.next() var buf []byte for !s.eof && s.chr != '"' { if s.chr == '\\' { s.next() switch { case s.chr == '"' || s.chr == '\\': buf = append(buf, s.chr) case s.chr == 'a': buf = append(buf, '\a') case s.chr == 'b': buf = append(buf, '\b') case s.chr == 'f': buf = append(buf, '\f') case s.chr == 'n': buf = append(buf, '\n') case s.chr == 'r': buf = append(buf, '\r') case s.chr == 't': buf = append(buf, '\t') case s.chr == 'v': buf = append(buf, '\v') case isDigit(s.chr): var oct []byte for isDigit(s.chr) { oct = append(oct, s.chr) s.next() } n, err := strconv.ParseInt(string(oct), 8, 0) if err != nil { log.Printf("scanning: %s", err) } buf = append(buf, byte(n)) continue default: buf = append(buf, '\\', s.chr) } s.next() continue } buf = append(buf, s.chr) s.next() } s.typ = tokenIdent s.tok = string(buf) s.next() case s.chr == '\'': s.next() beg := s.off for !s.eof && s.chr != '\'' { s.next() } s.typ = tokenIdent s.tok = string(s.buf[beg:s.off]) s.next() case s.chr == ':': s.typ = tokenColon s.tok = ":" s.nln = true s.next() case s.chr == '{' && s.peek() == '{': s.next() s.next() s.typ = tokenLBraces s.tok = "{{" s.sem = false s.nln = false case s.chr == '}' && s.peek() == '}': s.next() s.next() s.typ = tokenRBraces s.tok = "}}" s.sem = true case isPrefix(s.chr): s.typ = tokenPrefix s.tok = string(s.chr) s.cmd = true s.next() default: var buf []byte for !s.eof && !isSpace(s.chr) && s.chr != ';' && s.chr != '#' { if s.chr == '\\' { s.next() buf = append(buf, s.chr) s.next() continue } buf = append(buf, s.chr) s.next() } s.typ = tokenIdent s.tok = string(buf) s.sem = true if s.tok == "push" { s.key = true for !s.eof && isSpace(s.chr) && s.chr != '\n' { s.next() } } } return true } ================================================ FILE: server.go ================================================ package main import ( "bufio" "fmt" "log" "net" "os" "sort" "strconv" ) var ( gConnList = make(map[int]net.Conn) gQuitChan = make(chan struct{}, 1) gListener net.Listener ) func serve() { if gLogPath != "" { f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600) if err != nil { log.Fatalf("failed to open log file: %s", err) } defer f.Close() log.SetOutput(f) } log.Print("*************** starting server ***************") if gSocketProt == "unix" { setUserUmask() } l, err := net.Listen(gSocketProt, gSocketPath) if err != nil { log.Printf("listening socket: %s", err) return } defer l.Close() gListener = l listen(l) } func listen(l net.Listener) { for { c, err := l.Accept() if err != nil { select { case <-gQuitChan: log.Print("*************** closing server ***************") return default: log.Printf("accepting connection: %s", err) } } go handleConn(c) } } func echoerr(c net.Conn, msg string) { fmt.Fprintln(c, msg) log.Print(msg) } func echoerrf(c net.Conn, format string, a ...any) { echoerr(c, fmt.Sprintf(format, a...)) } func handleConn(c net.Conn) { s := bufio.NewScanner(c) Loop: for s.Scan() { log.Printf("listen: %s", s.Text()) word, rest := splitWord(s.Text()) switch word { case "conn": if rest != "" { word2, _ := splitWord(rest) id, err := strconv.Atoi(word2) if err != nil { echoerr(c, "listen: conn: client id should be a number") } else { // lifetime of the connection is managed by the server and // will be cleaned up via the `drop` command gConnList[id] = c return } } else { echoerr(c, "listen: conn: requires a client id") } case "drop": if rest != "" { word2, _ := splitWord(rest) id, err := strconv.Atoi(word2) if err != nil { echoerr(c, "listen: drop: client id should be a number") } else { if c2, ok := gConnList[id]; ok { c2.Close() } delete(gConnList, id) } } else { echoerr(c, "listen: drop: requires a client id") } case "list": ids := make([]int, 0, len(gConnList)) for id := range gConnList { ids = append(ids, id) } sort.Ints(ids) for _, id := range ids { fmt.Fprintln(c, id) } case "send": if rest != "" { word2, rest2 := splitWord(rest) id, err := strconv.Atoi(word2) if err != nil { for id, c2 := range gConnList { if _, err := fmt.Fprintln(c2, rest); err != nil { echoerrf(c, "failed to send command to client %v: %s", id, err) } } } else { if c2, ok := gConnList[id]; ok { if _, err := fmt.Fprintln(c2, rest2); err != nil { echoerrf(c, "failed to send command to client %v: %s", id, err) } } else { echoerr(c, "listen: send: no such client id is connected") } } } case "query": if rest == "" { echoerr(c, "listen: query: requires a client id") break } word2, rest2 := splitWord(rest) id, err := strconv.Atoi(word2) if err != nil { echoerr(c, "listen: query: client id should be a number") break } c2, ok := gConnList[id] if !ok { echoerr(c, "listen: query: no such client id is connected") break } if _, err := fmt.Fprintln(c2, "query "+rest2); err != nil { echoerrf(c, "failed to send query to client %v: %s", id, err) break } s2 := bufio.NewScanner(c2) for s2.Scan() && s2.Text() != "" { if _, err := fmt.Fprintln(c, s2.Text()); err != nil { log.Printf("failed to forward query response from client %v: %s", id, err) } } if s2.Err() != nil { echoerrf(c, "failed to read query response from client %v: %s", id, s2.Err()) } case "quit": if len(gConnList) == 0 { gQuitChan <- struct{}{} gListener.Close() break Loop } case "quit!": gQuitChan <- struct{}{} for _, c := range gConnList { fmt.Fprintln(c, "echo server is quitting...") c.Close() } gListener.Close() break Loop default: echoerrf(c, "listen: unexpected command: %s", word) } } if s.Err() != nil { echoerrf(c, "listening: %s", s.Err()) } c.Close() } ================================================ FILE: sixel.go ================================================ package main import ( "errors" "fmt" "log" "os" "strconv" "strings" "github.com/gdamore/tcell/v3" ) type sixelScreen struct { lastFile string lastWin win forceClear bool } func (sxs *sixelScreen) clearSixel(win *win, screen tcell.Screen, filePath string) { if sxs.lastFile != "" && (filePath != sxs.lastFile || *win != sxs.lastWin || sxs.forceClear) { screen.LockRegion(sxs.lastWin.x, sxs.lastWin.y, sxs.lastWin.w, sxs.lastWin.h, false) } } func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) { if reg.path == sxs.lastFile && *win == sxs.lastWin && !sxs.forceClear { return } cw, ch, err := cellSize(screen) if err != nil { log.Printf("sixel: %s", err) return } y := win.y var b strings.Builder for _, line := range reg.lines { if !strings.HasPrefix(line, "\033P") { if y >= win.y+win.h { break } screen.LockRegion(win.x, y, printLength(line), 1, true) fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1) b.WriteString(line) y++ continue } matches := reSixelSize.FindStringSubmatch(line) if matches == nil { log.Print("sixel: failed to get image size") continue } iw, _ := strconv.Atoi(matches[1]) ih, _ := strconv.Atoi(matches[2]) if os.Getenv("TMUX") != "" { // tmux rounds the image height up to a multiple of 6, so we // need to do the same to avoid overwriting the image, as tmux // would remove the image if we touched it. ih = (ih + 5) / 6 * 6 } sw := (iw + cw - 1) / cw sh := (ih + ch - 1) / ch if y+sh-1 >= win.y+win.h { break } screen.LockRegion(win.x, y, sw, sh, true) fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1) b.WriteString(line) y += sh } fmt.Fprint(os.Stderr, "\033[?2026h") // Begin synchronized update fmt.Fprint(os.Stderr, "\0337") // Save cursor position fmt.Fprint(os.Stderr, b.String()) // Write data fmt.Fprint(os.Stderr, "\0338") // Restore cursor position fmt.Fprint(os.Stderr, "\033[?2026l") // End synchronized update sxs.lastFile = reg.path sxs.lastWin = *win sxs.forceClear = false } func cellSize(screen tcell.Screen) (int, int, error) { tty, ok := screen.Tty() if !ok { // fallback for Windows Terminal return 10, 20, nil } ws, err := tty.WindowSize() if err != nil { return -1, -1, fmt.Errorf("failed to get window size: %w", err) } cw, ch := ws.CellDimensions() if cw <= 0 || ch <= 0 { return -1, -1, errors.New("cell dimensions should be greater than 0") } return cw, ch, nil } ================================================ FILE: termseq.go ================================================ package main import ( "log" "strconv" "strings" "unicode/utf8" "github.com/gdamore/tcell/v3" ) // gEscapeCode is the byte that starts ANSI control sequences. const gEscapeCode byte = '\x1b' // stripTermSequence is used to remove style-related ANSI escape sequences from // a given string. // // Note: this function is based on [printLength] and only strips style-related // sequences, `erase in line`, and `OSC 8` sequences. Other codes (e.g. cursor // moves), as well as broken escape sequences, are not removed. This prevents // mismatches between the two functions and avoids misalignment when rendering // the UI. func stripTermSequence(s string) string { var b strings.Builder slen := len(s) for i := 0; i < slen; { seq := readTermSequence(s[i:]) if seq != "" { i += len(seq) // skip known sequence continue } r, w := utf8.DecodeRuneInString(s[i:]) i += w b.WriteRune(r) } return b.String() } // readTermSequence is used to extract and return a terminal sequence from a // given string. If no supported sequence could be found, an empty string is // returned. // // CSI (Control Sequence Introducer): // - SGR (Select Graphic Rendition) `m`, used for text styling // - EL (Erase in Line) `K`, returned only so we can skip it // // OSC (Operating System Command): // - OSC 8, hyperlinks func readTermSequence(s string) string { slen := len(s) // must start with ESC if slen < 2 || s[0] != gEscapeCode { return "" } switch s[1] { case '[': // CSI i := strings.IndexAny(s[:min(slen, 64)], "mK") if i == -1 { return "" } return s[:i+1] case ']': // OSC if slen < 4 || s[2] != '8' || s[3] != ';' { return "" } // find string terminator for i := 4; i < slen; i++ { b := s[i] // BEL (XTerm) if b == 0x07 { return s[:i+1] } // ESC\ (ECMA-48) if b == gEscapeCode && i+1 < slen && s[i+1] == '\\' { return s[:i+2] } } // TODO: C1 forms? return "" default: return "" } } // optionToFmtstr takes an escape sequence option (e.g. `\033[1m`) and outputs // a complete format string (e.g. `\033[1m%s\033[0m`). func optionToFmtstr(optstr string) string { if !strings.Contains(optstr, "%s") { return optstr + "%s\033[0m" } else { return optstr } } // parseEscapeSequence takes an escape sequence option (e.g. `\033[1m`) and // converts it to a [tcell.Style] object. // Legacy function that only accepts SGR. Kept for convenience. func parseEscapeSequence(s string) tcell.Style { s = strings.TrimPrefix(s, "\033[") if i := strings.IndexByte(s, 'm'); i >= 0 { s = s[:i] } return applySGR(s, tcell.StyleDefault) } // applyTermSequence takes an escape sequence (e.g. `\033[1m`) and applies it // to the given [tcell.Style] object. // Accepts SGR and OSC sequences. func applyTermSequence(s string, st tcell.Style) tcell.Style { slen := len(s) if slen < 2 || s[0] != gEscapeCode { return st } switch s[1] { case '[': if s[slen-1] == 'm' { return applySGR(s[2:slen-1], st) } return st case ']': // trim terminator (BEL or ESC\), then parse body if s[slen-1] == 0x07 { return applyOSC(s[2:slen-1], st) } else if slen >= 2 && s[slen-2] == gEscapeCode && s[slen-1] == '\\' { return applyOSC(s[2:slen-2], st) } return st default: return st } } // applySGR takes an SGR sequence and applies it to the given [tcell.Style] // object. func applySGR(s string, st tcell.Style) tcell.Style { toks := strings.Split(s, ";") // ECMA-48 details the standard tokslen := len(toks) loop: for i := 0; i < tokslen; i++ { switch strings.TrimLeft(toks[i], "0") { case "": st = tcell.StyleDefault case "1": st = st.Bold(true) case "2": st = st.Dim(true) case "3": st = st.Italic(true) case "4:0": st = st.Underline(false) case "4", "4:1": st = st.Underline(true) case "4:2": st = st.Underline(tcell.UnderlineStyleDouble) case "4:3": st = st.Underline(tcell.UnderlineStyleCurly) case "4:4": st = st.Underline(tcell.UnderlineStyleDotted) case "4:5": st = st.Underline(tcell.UnderlineStyleDashed) case "5", "6": st = st.Blink(true) case "7": st = st.Reverse(true) case "8": // TODO: tcell PR for proper conceal st = st.Foreground(st.GetBackground()) case "9": st = st.StrikeThrough(true) case "22": st = st.Bold(false).Dim(false) case "23": st = st.Italic(false) case "24": st = st.Underline(false) case "25": st = st.Blink(false) case "27": st = st.Reverse(false) case "29": st = st.StrikeThrough(false) case "30", "31", "32", "33", "34", "35", "36", "37": n, _ := strconv.Atoi(toks[i]) st = st.Foreground(tcell.PaletteColor(n - 30)) case "90", "91", "92", "93", "94", "95", "96", "97": n, _ := strconv.Atoi(toks[i]) st = st.Foreground(tcell.PaletteColor(n - 82)) case "38": color, offset, err := parseColor(toks[i+1:]) if err != nil { log.Printf("error processing ansi code 38: %s", err) break loop } st = st.Foreground(color) i += offset case "40", "41", "42", "43", "44", "45", "46", "47": n, _ := strconv.Atoi(toks[i]) st = st.Background(tcell.PaletteColor(n - 40)) case "100", "101", "102", "103", "104", "105", "106", "107": n, _ := strconv.Atoi(toks[i]) st = st.Background(tcell.PaletteColor(n - 92)) case "48": color, offset, err := parseColor(toks[i+1:]) if err != nil { log.Printf("error processing ansi code 48: %s", err) break loop } st = st.Background(color) i += offset case "58": color, offset, err := parseColor(toks[i+1:]) if err != nil { log.Printf("error processing ansi code 58: %s", err) break loop } st = st.Underline(color) i += offset default: log.Printf("unknown ansi code: %s", toks[i]) } } return st } // applyOSC takes an OSC sequence and applies it to the given [tcell.Style] // object. // It currently supports OSC 8 hyperlinks only, implemented as specified by // https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda. func applyOSC(body string, st tcell.Style) tcell.Style { extractID := func(params string) string { for seg := range strings.SplitSeq(params, ":") { if seg == "" { continue } if k, v, ok := strings.Cut(seg, "="); ok && k == "id" { return v } } return "" } toks := strings.SplitN(body, ";", 3) if len(toks) < 2 { return st } switch toks[0] { case "8": if len(toks) < 3 { return st } url := toks[2] if url == "" { return st } st = st.Url(url) // Optional property used to identify grouped hyperlinks. if id := extractID(toks[1]); id != "" { st = st.UrlId(id) } return st default: return st } } ================================================ FILE: termseq_test.go ================================================ package main import ( "reflect" "testing" "github.com/gdamore/tcell/v3" "github.com/gdamore/tcell/v3/color" ) func TestStripTermSequence(t *testing.T) { tests := []struct { s string exp string }{ {"", ""}, // empty {"foo bar", "foo bar"}, // plain text {"\033[31mRed\033[0m", "Red"}, // octal syntax {"\x1b[31mRed\x1b[0m", "Red"}, // hexadecimal syntax {"foo\x1b[31mRed", "fooRed"}, // no reset parameter { "foo\x1b[1;31;102mBoldRedGreen\x1b[0mbar", "fooBoldRedGreenbar", }, // multiple attributes { "misc.go:func \x1b[01;31m\x1b[KstripTermSequence\x1b[m\x1b[K(s string) string {", "misc.go:func stripTermSequence(s string) string {", }, // `grep` output containing `erase in line` sequence // OSC 8 hyperlinks { "\x1b]8;;https://example.com\x1b\\example.com\x1b]8;;\x1b\\", "example.com", }, // open/close with ST (ESC\) { "\x1b]8;;https://example.com\x07example.com\x1b]8;;\x07", "example.com", }, // open/close with BEL { "\x1b]8;id=42;https://example.com\x1b\\label\x1b]8;;\x1b\\", "label", }, // params present { "\x1b]8;;https://example.com\x1b\\example.com", "example.com", }, // open without close } for _, test := range tests { if got := stripTermSequence(test.s); got != test.exp { t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got) } // we rely on both functions extracting the same runes // to avoid misalignment if printLength(test.s) != len(stripTermSequence(test.s)) { t.Errorf("at input %q expected '%d' but got '%d'", test.s, printLength(test.s), len(stripTermSequence(test.s))) } } } func TestReadTermSequence(t *testing.T) { tests := []struct { s, exp string }{ {"", ""}, // empty {"foo bar", ""}, // plain text {"\x1b", ""}, // lone ESC {"\x1bX", ""}, // unknown ESC sequence {"\x1b[31m", "\x1b[31m"}, // CSI SGR {"\x1b[K", "\x1b[K"}, // CSI EL {"\x1b[1;31m", "\x1b[1;31m"}, // CSI SGR (multiple params) {"\x1b[31", ""}, // CSI incomplete (no terminator) {"foo\x1b[31m", ""}, // doesn't start with ESC {"\x1b]8;;https://example.com\x1b\\", "\x1b]8;;https://example.com\x1b\\"}, // OSC 8 (ST terminator) {"\x1b]8;;https://example.com\x07", "\x1b]8;;https://example.com\x07"}, // OSC 8 (BEL terminator) {"\x1b]0;title\x07", ""}, // non-OSC8 OSC (ignored) } for _, tc := range tests { if got := readTermSequence(tc.s); got != tc.exp { t.Errorf("input %q: got %q, want %q", tc.s, got, tc.exp) } } } func TestOptionToFmtstr(t *testing.T) { tests := []struct { s string exp string }{ {"\033[1m", "\033[1m%s\033[0m"}, {"\033[1;7;31;42m", "\033[1;7;31;42m%s\033[0m"}, } for _, test := range tests { if got := optionToFmtstr(test.s); got != test.exp { t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got) } } } func TestParseEscapeSequence(t *testing.T) { tests := []struct { s string exp tcell.Style }{ {"\033[1m", tcell.StyleDefault.Bold(true)}, {"\033[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(color.XTerm1).Background(color.XTerm2)}, } for _, test := range tests { if got := parseEscapeSequence(test.s); got != test.exp { t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestApplyTermSequence(t *testing.T) { tests := []struct { s string exp tcell.Style }{ {"", tcell.StyleDefault}, {"\x1b[1m", tcell.StyleDefault.Bold(true)}, {"\x1b[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(color.XTerm1).Background(color.XTerm2)}, {"\x1b]8;;https://example.com\x1b\\", tcell.StyleDefault.Url("https://example.com")}, // OSC 8 terminated with ST (ESC\), no `id` provided {"\x1b]8;;https://example.com\x07", tcell.StyleDefault.Url("https://example.com")}, // OSC 8 terminated with BEL, no `id` provided {"\x1b]8;id=42;https://example.com\x1b\\", tcell.StyleDefault.Url("https://example.com").UrlId("42")}, // OSC 8, `id` provided } for _, test := range tests { if got := applyTermSequence(test.s, tcell.StyleDefault); !reflect.DeepEqual(got, test.exp) { t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got) } } } func TestApplySGR(t *testing.T) { none := tcell.StyleDefault tests := []struct { s string st tcell.Style stExp tcell.Style }{ {"", none, none}, {"", none.Foreground(color.XTerm1).Background(color.XTerm1), none}, {"", none.Bold(true), none}, {"", none.Foreground(color.XTerm1).Bold(true), none}, {"0", none, none}, {"0", none.Foreground(color.XTerm1).Background(color.XTerm1), none}, {"0", none.Bold(true), none}, {"0", none.Foreground(color.XTerm1).Bold(true), none}, {"1", none, none.Bold(true)}, {"4", none, none.Underline(true)}, {"7", none, none.Reverse(true)}, {"1", none.Foreground(color.XTerm1), none.Foreground(color.XTerm1).Bold(true)}, {"4", none.Foreground(color.XTerm1), none.Foreground(color.XTerm1).Underline(true)}, {"7", none.Foreground(color.XTerm1), none.Foreground(color.XTerm1).Reverse(true)}, {"4", none.Bold(true), none.Bold(true).Underline(true)}, {"4", none.Foreground(color.XTerm1).Bold(true), none.Foreground(color.XTerm1).Bold(true).Underline(true)}, {"4:0", none, none}, {"4:0", none.Underline(true), none}, {"4:1", none, none.Underline(true)}, {"4:2", none, none.Underline(tcell.UnderlineStyleDouble)}, {"4:3", none, none.Underline(tcell.UnderlineStyleCurly)}, {"4:4", none, none.Underline(tcell.UnderlineStyleDotted)}, {"4:5", none, none.Underline(tcell.UnderlineStyleDashed)}, {"22", none.Italic(true).Bold(true).Dim(true), none.Italic(true)}, {"23", none.Bold(true).Italic(true), none.Bold(true)}, {"24", none.Bold(true).Underline(true), none.Bold(true)}, {"25", none.Bold(true).Blink(true), none.Bold(true)}, {"27", none.Bold(true).Reverse(true), none.Bold(true)}, {"29", none.Bold(true).StrikeThrough(true), none.Bold(true)}, {"31", none, none.Foreground(color.XTerm1)}, {"31", none.Foreground(color.XTerm2), none.Foreground(color.XTerm1)}, {"31", none.Foreground(color.XTerm2).Bold(true), none.Foreground(color.XTerm1).Bold(true)}, {"41", none, none.Background(color.XTerm1)}, {"41", none.Background(color.XTerm2), none.Background(color.XTerm1)}, {"1;31", none, none.Foreground(color.XTerm1).Bold(true)}, {"1;31", none.Foreground(color.XTerm2), none.Foreground(color.XTerm1).Bold(true)}, {"01;31", none, none.Foreground(color.XTerm1).Bold(true)}, {"01;31", none.Foreground(color.XTerm2), none.Foreground(color.XTerm1).Bold(true)}, {"38;5;0", none, none.Foreground(color.XTerm0)}, {"38;5;1", none, none.Foreground(color.XTerm1)}, {"38;5;8", none, none.Foreground(color.XTerm8)}, {"38;5;16", none, none.Foreground(color.XTerm16)}, {"38;5;232", none, none.Foreground(color.XTerm232)}, {"38;5;1", none.Foreground(color.XTerm2), none.Foreground(color.XTerm1)}, {"38;5;1", none.Foreground(color.XTerm2).Bold(true), none.Foreground(color.XTerm1).Bold(true)}, {"48;5;0", none, none.Background(color.XTerm0)}, {"48;5;1", none, none.Background(color.XTerm1)}, {"48;5;8", none, none.Background(color.XTerm8)}, {"48;5;16", none, none.Background(color.XTerm16)}, {"48;5;232", none, none.Background(color.XTerm232)}, {"48;5;1", none.Background(color.XTerm2), none.Background(color.XTerm1)}, {"1;38;5;1", none, none.Foreground(color.XTerm1).Bold(true)}, {"1;38;5;1", none.Foreground(color.XTerm2), none.Foreground(color.XTerm1).Bold(true)}, {"38;2;5;102;8", none, none.Foreground(tcell.NewRGBColor(5, 102, 8))}, {"48;2;0;48;143", none, none.Background(tcell.NewRGBColor(0, 48, 143))}, // Fixes color construction issue: https://github.com/gokcehan/lf/pull/439#issuecomment-674409446 {"38;5;34;1", none, none.Foreground(color.XTerm34).Bold(true)}, } for _, test := range tests { if stGot := applySGR(test.s, test.st); stGot != test.stExp { t.Errorf("at input '%s' with '%v' expected '%v' but got '%v'", test.s, test.st, test.stExp, stGot) } } } ================================================ FILE: ui.go ================================================ package main import ( "bytes" "fmt" "log" "os" "path/filepath" "reflect" "slices" "sort" "strconv" "strings" "text/tabwriter" "text/template" "time" "unicode/utf8" "github.com/gdamore/tcell/v3" "github.com/rivo/uniseg" "golang.org/x/term" ) const previewLoadingDelay = 100 * time.Millisecond type win struct { w, h, x, y int } func newWin(w, h, x, y int) *win { return &win{w, h, x, y} } func (win *win) renew(w, h, x, y int) { win.w, win.h, win.x, win.y = w, h, x, y } // printLength returns the display width of s in terminal cells. // // It ignores supported terminal control sequences (see [readTermSequence]) // and accounts for tab expansions using the `tabstop` option. func printLength(s string) int { length := 0 slen := len(s) for i := 0; i < slen; { seq := readTermSequence(s[i:]) if seq != "" { i += len(seq) continue } gc, _, w, _ := uniseg.FirstGraphemeClusterInString(s[i:], -1) i += len(gc) if gc == "\t" { length += gOpts.tabstop - length%gOpts.tabstop } else { length += w } } return length } func (win *win) print(screen tcell.Screen, x, y int, st tcell.Style, s string) tcell.Style { var b strings.Builder off := 0 put := func() { if b.Len() > 0 { s := b.String() screen.PutStrStyled(win.x+x+off, win.y+y, s, st) off += printLength(s) b.Reset() } } slen := len(s) for i := 0; i < slen; { seq := readTermSequence(s[i:]) if seq != "" { put() st = applyTermSequence(seq, st) i += len(seq) continue } gc, _, _, _ := uniseg.FirstGraphemeClusterInString(s[i:], -1) if gc == "\t" { w := gOpts.tabstop - (x+off+printLength(b.String()))%gOpts.tabstop b.WriteString(strings.Repeat(" ", w)) } else if gc != "\r" && gc != "\n" { b.WriteString(gc) } i += len(gc) } put() return st } func (win *win) printf(screen tcell.Screen, x, y int, st tcell.Style, format string, a ...any) { win.print(screen, x, y, st, fmt.Sprintf(format, a...)) } func (win *win) printLine(screen tcell.Screen, x, y int, st tcell.Style, s string) { win.printf(screen, x, y, st, "%s%*s", s, win.w-printLength(s), "") } func (win *win) printRight(screen tcell.Screen, y int, st tcell.Style, s string) { win.print(screen, win.w-printLength(s), y, st, s) } func (win *win) printMsg(screen tcell.Screen, s string) { pad := 1 if gOpts.mergeindicators { pad-- } st := tcell.StyleDefault.Reverse(true) win.print(screen, pad, 0, st, s) } func (win *win) printReg(screen tcell.Screen, reg *reg, sxs *sixelScreen, previewTimer *time.Timer) { switch { case reg.loading: if time.Since(reg.loadTime) > previewLoadingDelay { win.printMsg(screen, "loading...") } else { previewTimer.Reset(previewLoadingDelay) } case reg.sixel: sxs.printSixel(win, screen, reg) default: st := tcell.StyleDefault for i, l := range reg.lines { if i > win.h-1 { break } st = win.print(screen, 0, i, st, l) } } if !reg.sixel { sxs.lastFile = "" } } var gThisYear = time.Now().Year() func infotimefmt(t time.Time) string { if t.Year() == gThisYear { return t.Format(gOpts.infotimefmtnew) } return t.Format(gOpts.infotimefmtold) } func fileInfo(f *file, d *dir, userWidth, groupWidth, customWidth int) (string, string, int) { var info strings.Builder var custom string var off int for _, s := range getInfo(d.path) { switch s { case "size": if f.IsDir() && getDirCounts(d.path) { switch { case f.dirCount < 0: info.WriteString(" !") case f.dirCount < 10000: fmt.Fprintf(&info, " %5d", f.dirCount) default: info.WriteString(" 9999+") } } else { switch { case f.dirSize >= 0: fmt.Fprintf(&info, " %5s", humanize(f.dirSize)) case f.IsDir(): info.WriteString(" -") default: fmt.Fprintf(&info, " %5s", humanize(f.Size())) } } case "time": fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.ModTime())) case "atime": fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.accessTime)) case "btime": fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.birthTime)) case "ctime": fmt.Fprintf(&info, " %*s", max(len(gOpts.infotimefmtnew), len(gOpts.infotimefmtold)), infotimefmt(f.changeTime)) case "perm": info.WriteString(" " + permString(f.Mode())) case "user": fmt.Fprintf(&info, " %-*s", userWidth, userName(f.FileInfo)) case "group": fmt.Fprintf(&info, " %-*s", groupWidth, groupName(f.FileInfo)) case "custom": // Prevent useless spacers, as `custom` allows empty values if customWidth < 1 { continue } // To allow for the usage of escape sequences, store `custom` // separately and print it later using the offset. off = info.Len() fmt.Fprintf(&info, " %*s", customWidth, "") custom = fmt.Sprintf(" %s%*s", f.customInfo, customWidth-printLength(f.customInfo), "") default: log.Printf("unknown info type: %s", s) } } return info.String(), custom, off } type dirContext struct { selections map[string]int clipboard clipboard tags map[string]string } // dirRole describes what kind of directory pane is being drawn. type dirRole byte const ( Active dirRole = iota // Current directory pane. Parent // Parent or ancestor directory pane. Preview // Preview pane when it shows a directory listing. ) type dirStyle struct { colors styleMap icons iconMap role dirRole } func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirStyle, previewTimer *time.Timer) { if win.w < 5 || dir == nil { return } fileslen := len(dir.files) switch { case dir.loading && fileslen == 0: if time.Since(dir.loadTime) > previewLoadingDelay { win.printMsg(ui.screen, "loading...") } else { previewTimer.Reset(previewLoadingDelay) } return case dir.noPerm: win.printMsg(ui.screen, "permission denied") return case fileslen == 0: win.printMsg(ui.screen, "empty") return } beg := max(dir.ind-dir.pos, 0) end := min(beg+win.h, fileslen) if beg > end { return } var lnwidth int if dirStyle.role == Active && (gOpts.number || gOpts.relativenumber) { lnwidth = 1 for j := 10; j <= fileslen; j *= 10 { lnwidth++ } if gOpts.number && gOpts.relativenumber { lnwidth = max(lnwidth, 2) } } var userWidth, groupWidth, customWidth int var fetchedCustom bool // Only fetch user/group/custom widths if configured to display them for _, s := range getInfo(dir.path) { switch s { case "user": userWidth = getUserWidth(dir, beg, end) case "group": groupWidth = getGroupWidth(dir, beg, end) case "custom": customWidth = getCustomWidth(dir, beg, end) fetchedCustom = true // Can have a length of 0 } if userWidth > 0 && groupWidth > 0 && fetchedCustom { break } } indOff, tagOff, nameOff := lnwidth, lnwidth, lnwidth+1 if !gOpts.mergeindicators { tagOff++ nameOff++ } visualSelections := dir.visualSelections() for i, f := range dir.files[beg:end] { st := dirStyle.colors.get(f) if lnwidth > 0 { var ln string if gOpts.number && !gOpts.relativenumber { ln = fmt.Sprintf("%*d", lnwidth, i+1+beg) } else if gOpts.relativenumber { switch { case i < dir.pos: ln = fmt.Sprintf("%*d", lnwidth, dir.pos-i) case i > dir.pos: ln = fmt.Sprintf("%*d", lnwidth, i-dir.pos) case gOpts.number: ln = fmt.Sprintf("%-*d", lnwidth, i+1+beg) default: ln = fmt.Sprintf("%*d", lnwidth, 0) } } fmtStr := optionToFmtstr(gOpts.numberfmt) if i == dir.pos && gOpts.numbercursorfmt != "" { fmtStr = optionToFmtstr(gOpts.numbercursorfmt) } win.print(ui.screen, 0, i, tcell.StyleDefault, fmt.Sprintf(fmtStr, ln)) } path := filepath.Join(dir.path, f.Name()) var fmtStr string if slices.Contains(visualSelections, path) { fmtStr = gOpts.visualfmt } else if _, ok := context.selections[path]; ok { fmtStr = gOpts.selectfmt } else if slices.Contains(context.clipboard.paths, path) { if context.clipboard.mode == clipboardCopy { fmtStr = gOpts.copyfmt } else { fmtStr = gOpts.cutfmt } } tag := " " if val, ok := context.tags[path]; ok && len(val) > 0 { tag = val } if fmtStr != "" { ind := " " if gOpts.mergeindicators { ind = tag } win.print(ui.screen, indOff, i, parseEscapeSequence(fmtStr), ind) } // make space for select marker, and leave another space at the end maxWidth := win.w - lnwidth - 2 var icon string var iconDef iconDef if gOpts.icons { iconDef = dirStyle.icons.get(f) icon = iconDef.icon + " " } // subtract space for icon maxFilenameWidth := maxWidth - uniseg.StringWidth(icon) // subtract space for tag if not merged with selection marker if !gOpts.mergeindicators { maxFilenameWidth-- } info, custom, customOff := fileInfo(f, dir, userWidth, groupWidth, customWidth) infolen := len(info) showInfo := infolen > 0 && 2*infolen < maxWidth if showInfo { maxFilenameWidth -= infolen } filename := truncateFilename(f, maxFilenameWidth, gOpts.truncatepct, gOpts.truncatechar) spacing := maxFilenameWidth - uniseg.StringWidth(filename) if spacing > 0 { filename += strings.Repeat(" ", spacing) } if showInfo { filename += info customOff += nameOff + uniseg.StringWidth(icon) + maxFilenameWidth } if i == dir.pos { var cursorFmt string switch dirStyle.role { case Active: cursorFmt = optionToFmtstr(gOpts.cursoractivefmt) case Parent: cursorFmt = optionToFmtstr(gOpts.cursorparentfmt) case Preview: cursorFmt = optionToFmtstr(gOpts.cursorpreviewfmt) } // print tag separately as it can contain color escape sequences if !gOpts.mergeindicators || fmtStr == "" { win.print(ui.screen, tagOff, i, st, fmt.Sprintf(cursorFmt, tag)) } win.print(ui.screen, nameOff, i, st, fmt.Sprintf(cursorFmt, icon+filename+" ")) // print over the empty space we reserved for the custom info if showInfo && custom != "" { win.print(ui.screen, customOff, i, st, fmt.Sprintf(cursorFmt, stripTermSequence(custom))) } } else { if !gOpts.mergeindicators || fmtStr == "" { if tag == " " { win.print(ui.screen, tagOff, i, st, " ") } else { tagStr := fmt.Sprintf(optionToFmtstr(gOpts.tagfmt), tag) win.print(ui.screen, tagOff, i, tcell.StyleDefault, tagStr) } } if len(icon) > 0 { iconStyle := st if iconDef.hasStyle { iconStyle = iconDef.style } win.print(ui.screen, nameOff, i, iconStyle, icon) } win.print(ui.screen, nameOff+uniseg.StringWidth(icon), i, st, filename+" ") // print over the empty space we reserved for the custom info if showInfo && custom != "" { win.print(ui.screen, customOff, i, st, custom) } } } } func getUserWidth(dir *dir, beg, end int) int { maxw := 0 for _, f := range dir.files[beg:end] { maxw = max(len(userName(f.FileInfo)), maxw) } return maxw } func getGroupWidth(dir *dir, beg, end int) int { maxw := 0 for _, f := range dir.files[beg:end] { maxw = max(len(groupName(f.FileInfo)), maxw) } return maxw } func getCustomWidth(dir *dir, beg, end int) int { maxw := 0 for _, f := range dir.files[beg:end] { maxw = max(printLength(f.customInfo), maxw) } return maxw } func getWins(screen tcell.Screen) []*win { wtot, htot := screen.Size() h := max(htot-2, 0) x, y := 0, 1 if gOpts.drawbox && gOpts.borderstyle&borderOutline != 0 { h = max(htot-4, 0) x, y = 1, 2 } widths := getWidths(wtot, gOpts.ratios, gOpts.drawbox, gOpts.borderstyle) wins := make([]*win, 0, len(widths)) for _, w := range widths { wins = append(wins, newWin(w, h, x, y)) x += w + 1 } return wins } type menuSelect struct { x, y int // selection position in menuWin (cells) s string // selected entry text to draw with `menuselectfmt` } type ui struct { screen tcell.Screen // primary screen used for drawing and event polling sxScreen sixelScreen // sixel preview state wins []*win // pane windows from `ratios` (last is `preview` when enabled) promptWin *win // prompt line window msgWin *win // status line window menuWin *win // menu window msg string // message/output shown in msgWin exprChan chan expr // expr queue evChan chan tcell.Event // merged event queue (keyChan + tevChan) menu string // rendered (multiline) menu text (i.e. completions, binds, marks) menuSelect *menuSelect // selected menu entry (completion menu only) cmdPrefix string // command prefix/prompt (empty: Normal mode) cmdAccLeft string // command buffer left of cursor cmdAccRight string // command buffer right of cursor cmdYankBuf string // yank buffer for command line editing keyAcc string // keys typed so far for mapping lookup keyCount string // count prefix for next command styles styleMap // parsed styles icons iconMap // parsed icons ruler *template.Template // compiled `rulerfile` rulerErr error // `rulerfile` parse error (if any) currentFile string // last path passed to `on-select` pasteEvent bool // whether paste event is active (to ignore pasted input in Normal mode) } func newUI(screen tcell.Screen) *ui { wtot, htot := screen.Size() ui := &ui{ screen: screen, wins: getWins(screen), promptWin: newWin(wtot, 1, 0, 0), msgWin: newWin(wtot, 1, 0, htot-1), menuWin: newWin(wtot, 1, 0, htot-2), exprChan: make(chan expr, 1000), evChan: make(chan tcell.Event, 1000), styles: parseStyles(), icons: parseIcons(), currentFile: "", sxScreen: sixelScreen{}, } ui.ruler, ui.rulerErr = parseRuler(gOpts.rulerfile) return ui } func (ui *ui) winAt(x, y int) (int, *win) { for i := len(ui.wins) - 1; i >= 0; i-- { w := ui.wins[i] if x >= w.x && y >= w.y && y < w.y+w.h { return i, w } } return -1, nil } func (ui *ui) renew() { ui.wins = getWins(ui.screen) wtot, htot := ui.screen.Size() ui.promptWin.renew(wtot, 1, 0, 0) ui.msgWin.renew(wtot, 1, 0, htot-1) ui.menuWin.renew(wtot, 1, 0, htot-2) } func (ui *ui) echo(msg string) { ui.msg = msg } func (ui *ui) echomsg(msg string) { ui.echo(msg) log.Print(msg) } func (ui *ui) echoerr(msg string) { ui.echo(fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), msg)) log.Printf("error: %s", msg) } func (ui *ui) echoerrf(format string, a ...any) { ui.echoerr(fmt.Sprintf(format, a...)) } // reg represents the preview for a file. // This can also be used to represent the preview of a directory if // `dirpreviews` is enabled. // // Note: the name `reg` is historical. It originally meant "regular" // file preview, but `previewer` now also supports non-regular files. type reg struct { loading bool volatile bool loadTime time.Time path string lines []string sixel bool } func (ui *ui) loadFile(app *app, volatile bool) { if volatile { app.nav.previewChan <- "" } curr := app.nav.currFile() if curr == nil { return } if curr.path != ui.currentFile { ui.currentFile = curr.path onSelect(app) } if !gOpts.preview { return } if curr.isPreviewable() { app.nav.loadReg(curr.path, volatile) } else if curr.IsDir() { dir := app.nav.getDir(curr.path) app.nav.checkDir(dir) } } func (ui *ui) drawPromptLine(nav *nav) { st := tcell.StyleDefault dir := nav.currDir() pwd := dir.path if after, ok := strings.CutPrefix(pwd, gUser.HomeDir); ok { pwd = filepath.Join("~", after) } sep := string(filepath.Separator) var fname string if curr := nav.currFile(); curr != nil { fname = filepath.Base(curr.path) } var prompt string prompt = strings.ReplaceAll(gOpts.promptfmt, "%u", gUser.Username) prompt = strings.ReplaceAll(prompt, "%h", gHostname) prompt = strings.ReplaceAll(prompt, "%f", fname) if printLength(strings.ReplaceAll(strings.ReplaceAll(prompt, "%w", pwd), "%d", pwd)) > ui.promptWin.w { names := strings.Split(pwd, sep) for i := range names { if names[i] == "" { continue } r, _ := utf8.DecodeRuneInString(names[i]) names[i] = string(r) if printLength(strings.ReplaceAll(strings.ReplaceAll(prompt, "%w", strings.Join(names, sep)), "%d", strings.Join(names, sep))) <= ui.promptWin.w { break } } pwd = strings.Join(names, sep) } prompt = strings.ReplaceAll(prompt, "%w", pwd) if !strings.HasSuffix(pwd, sep) { pwd += sep } prompt = strings.ReplaceAll(prompt, "%d", pwd) if len(dir.filter) != 0 { prompt = strings.ReplaceAll(prompt, "%F", fmt.Sprint(dir.filter)) } else { prompt = strings.ReplaceAll(prompt, "%F", "") } // spacer avail := ui.promptWin.w - printLength(prompt) + 2 if avail > 0 { prompt = strings.Replace(prompt, "%S", strings.Repeat(" ", avail), 1) } prompt = strings.ReplaceAll(prompt, "%S", "") ui.promptWin.print(ui.screen, 0, 0, st, prompt) } func formatRulerOpt(name, val string) string { // handle escape character so it doesn't mess up the ruler val = strings.ReplaceAll(val, "\033", "\033[7m\\033\033[0m") // display name of builtin options for clarity if !strings.HasPrefix(name, "lf_user_") { return fmt.Sprintf("%s=%s", strings.TrimPrefix(name, "lf_"), val) } return val } func (ui *ui) drawStat(nav *nav) { if ui.msg != "" { ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, ui.msg) return } curr := nav.currFile() if curr == nil { return } if curr.err != nil { ui.echoerrf("stat: %s", curr.err) ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, ui.msg) return } statfmt := strings.ReplaceAll(gOpts.statfmt, "|", "\x1f") replace := func(s, val string) { if val == "" { val = "\x00" } statfmt = strings.ReplaceAll(statfmt, s, val) } if nav.isVisualMode() { replace("%m", "VISUAL") replace("%M", "VISUAL") } else { replace("%m", "") replace("%M", "NORMAL") } replace("%p", permString(curr.Mode())) replace("%c", linkCount(curr)) replace("%u", userName(curr)) replace("%g", groupName(curr)) replace("%s", humanize(curr.Size())) replace("%S", fmt.Sprintf("%5s", humanize(curr.Size()))) replace("%t", curr.ModTime().Format(gOpts.timefmt)) replace("%l", curr.linkTarget) var fileInfo strings.Builder for section := range strings.SplitSeq(statfmt, "\x1f") { if !strings.Contains(section, "\x00") { fileInfo.WriteString(section) } } ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, fileInfo.String()) } func (ui *ui) drawRuler(nav *nav) { st := tcell.StyleDefault dir := nav.currDir() tot := len(dir.files) ind := min(dir.ind+1, tot) hid := len(dir.allFiles) - tot acc := ui.keyCount + ui.keyAcc var percentage string beg := max(dir.ind-dir.pos, 0) switch { case tot <= nav.height: percentage = "All" case beg == 0: percentage = "Top" case beg == tot-nav.height: percentage = "Bot" default: percentage = fmt.Sprintf("%2d%%", beg*100/(tot-nav.height)) } numClipCopy := 0 numClipMove := 0 if nav.clipboard.mode == clipboardCopy { numClipCopy = len(nav.clipboard.paths) } else { numClipMove = len(nav.clipboard.paths) } currSelections := nav.currSelections() currVSelections := nav.currDir().visualSelections() progress := []string{} if nav.copyJobs > 0 { if nav.copyTotal == 0 { progress = append(progress, fmt.Sprintf("[0%%]")) } else { progress = append(progress, fmt.Sprintf("[%d%%]", nav.copyBytes*100/nav.copyTotal)) } } if nav.moveTotal > 0 { progress = append(progress, fmt.Sprintf("[%d/%d]", nav.moveCount, nav.moveTotal)) } if nav.deleteTotal > 0 { progress = append(progress, fmt.Sprintf("[%d/%d]", nav.deleteCount, nav.deleteTotal)) } opts := getOptsMap() rulerfmt := strings.ReplaceAll(gOpts.rulerfmt, "|", "\x1f") rulerfmt = reRulerSub.ReplaceAllStringFunc(rulerfmt, func(s string) string { var result string switch s { case "%a": result = acc case "%p": result = strings.Join(progress, " ") case "%m": result = fmt.Sprintf("%.d", numClipMove) case "%c": result = fmt.Sprintf("%.d", numClipCopy) case "%s": result = fmt.Sprintf("%.d", len(currSelections)) case "%v": result = fmt.Sprintf("%.d", len(currVSelections)) case "%f": result = strings.Join(dir.filter, " ") case "%i": result = strconv.Itoa(ind) case "%t": result = strconv.Itoa(tot) case "%h": result = strconv.Itoa(hid) case "%P": result = percentage case "%d": result = diskFree(dir.path) default: s = strings.TrimSuffix(strings.TrimPrefix(s, "%{"), "}") if val, ok := opts[s]; ok { result = formatRulerOpt(s, val) } } if result == "" { return "\x00" } return result }) var ruler strings.Builder for section := range strings.SplitSeq(rulerfmt, "\x1f") { if !strings.Contains(section, "\x00") { ruler.WriteString(section) } } ui.msgWin.printRight(ui.screen, 0, st, ruler.String()) } func (ui *ui) drawRulerFile(nav *nav) { if ui.rulerErr != nil { err := fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), fmt.Errorf("parsing ruler: %w", ui.rulerErr)) ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, err) return } var stat *statData curr := nav.currFile() if curr != nil { if curr.err == nil { stat = &statData{ Path: curr.path, Name: curr.Name(), Extension: curr.ext, Size: curr.Size(), DirSize: curr.dirSize, DirCount: curr.dirCount, Permissions: permString(curr.Mode()), ModTime: curr.ModTime().Format(gOpts.timefmt), AccessTime: curr.accessTime.Format(gOpts.timefmt), BirthTime: curr.birthTime.Format(gOpts.timefmt), ChangeTime: curr.changeTime.Format(gOpts.timefmt), LinkCount: linkCount(curr), User: userName(curr), Group: groupName(curr), Target: curr.linkTarget, CustomInfo: curr.customInfo, } } else { ui.echoerrf("stat: %s", curr.err) } } dir := nav.currDir() tot := len(dir.files) ind := min(dir.ind+1, tot) all := len(dir.allFiles) hid := all - tot var linePercentage string if tot == 0 { linePercentage = "100%" } else { linePercentage = fmt.Sprintf("%d%%", ind*100/tot) } var scrollPercentage string beg := max(dir.ind-dir.pos, 0) switch { case tot <= nav.height: scrollPercentage = "All" case beg == 0: scrollPercentage = "Top" case beg == tot-nav.height: scrollPercentage = "Bot" default: scrollPercentage = fmt.Sprintf("%2d%%", beg*100/(tot-nav.height)) } var copiedPaths []string var cutPaths []string if nav.clipboard.mode == clipboardCopy { copiedPaths = nav.clipboard.paths } else { cutPaths = nav.clipboard.paths } currSelections := nav.currSelections() currVSelections := nav.currDir().visualSelections() progress := []string{} if nav.copyJobs > 0 { if nav.copyTotal == 0 { progress = append(progress, fmt.Sprintf("[0%%]")) } else { progress = append(progress, fmt.Sprintf("[%d%%]", nav.copyBytes*100/nav.copyTotal)) } } if nav.moveTotal > 0 { progress = append(progress, fmt.Sprintf("[%d/%d]", nav.moveCount, nav.moveTotal)) } if nav.deleteTotal > 0 { progress = append(progress, fmt.Sprintf("[%d/%d]", nav.deleteCount, nav.deleteTotal)) } mode := "NORMAL" if nav.isVisualMode() { mode = "VISUAL" } options := make(map[string]string) v := reflect.ValueOf(gOpts) t := v.Type() for i := range v.NumField() { name := t.Field(i).Name switch name { case "nkeys", "vkeys", "cmdkeys", "cmds", "user": continue default: options[name] = fieldToString(v.Field(i)) } } data := rulerData{ SPACER: "\x1f", Message: ui.msg, Keys: ui.keyCount + ui.keyAcc, Progress: progress, Copy: copiedPaths, Cut: cutPaths, Select: currSelections, Visual: currVSelections, Index: ind, Total: tot, Hidden: hid, All: all, LinePercentage: linePercentage, ScrollPercentage: scrollPercentage, Filter: dir.filter, Mode: mode, Options: options, UserOptions: gOpts.user, Stat: stat, } left, right, err := renderRuler(ui.ruler, data, ui.msgWin.w) if err != nil { err := fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), fmt.Errorf("rendering ruler: %w", err)) ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, err) return } ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, left) ui.msgWin.printRight(ui.screen, 0, tcell.StyleDefault, right) } func (ui *ui) drawPreview(nav *nav, context *dirContext) { curr := nav.currFile() if curr == nil { return } win := ui.wins[len(ui.wins)-1] ui.sxScreen.clearSixel(win, ui.screen, curr.path) if gOpts.preview { if curr.isPreviewable() { if reg, ok := nav.regCache[curr.path]; ok { win.printReg(ui.screen, reg, &ui.sxScreen, nav.previewTimer) } } else if curr.IsDir() { ui.sxScreen.lastFile = "" dir := nav.getDir(curr.path) dirStyle := &dirStyle{colors: ui.styles, icons: ui.icons, role: Preview} win.printDir(ui, dir, context, dirStyle, nav.previewTimer) } } } func (ui *ui) drawBox() { st := parseEscapeSequence(gOpts.borderfmt) w, h := ui.screen.Size() style := gOpts.borderstyle if style&borderOutline != 0 { for i := 1; i < w-1; i++ { ui.screen.PutStrStyled(i, 1, string(tcell.RuneHLine), st) ui.screen.PutStrStyled(i, h-2, string(tcell.RuneHLine), st) } for i := 2; i < h-2; i++ { ui.screen.PutStrStyled(0, i, string(tcell.RuneVLine), st) ui.screen.PutStrStyled(w-1, i, string(tcell.RuneVLine), st) } if style&borderRound != 0 { ui.screen.PutStrStyled(0, 1, "╭", st) ui.screen.PutStrStyled(w-1, 1, "╮", st) ui.screen.PutStrStyled(0, h-2, "╰", st) ui.screen.PutStrStyled(w-1, h-2, "╯", st) } else { ui.screen.PutStrStyled(0, 1, string(tcell.RuneULCorner), st) ui.screen.PutStrStyled(w-1, 1, string(tcell.RuneURCorner), st) ui.screen.PutStrStyled(0, h-2, string(tcell.RuneLLCorner), st) ui.screen.PutStrStyled(w-1, h-2, string(tcell.RuneLRCorner), st) } } if style&borderSeparators == 0 { return } top, bot := 1, h-1 if style&borderOutline != 0 { top, bot = 2, h-2 } for wind := range len(ui.wins) - 1 { x := ui.wins[wind].x + ui.wins[wind].w if style&borderOutline != 0 { ui.screen.PutStrStyled(x, 1, string(tcell.RuneTTee), st) } for y := top; y < bot; y++ { ui.screen.PutStrStyled(x, y, string(tcell.RuneVLine), st) } if style&borderOutline != 0 { ui.screen.PutStrStyled(x, h-2, string(tcell.RuneBTee), st) } } } func (ui *ui) drawMenu() { if ui.menu == "" { return } lines := strings.Split(ui.menu, "\n") lines = lines[:len(lines)-1] ui.menuWin.h = len(lines) ui.menuWin.y = ui.msgWin.y - ui.menuWin.h // clear sixel image if it overlaps with the menu ui.screen.LockRegion(ui.menuWin.x, ui.menuWin.y, ui.menuWin.w, ui.menuWin.h, false) ui.sxScreen.forceClear = true for i, line := range lines { var st tcell.Style if i == 0 { st = parseEscapeSequence(gOpts.menuheaderfmt) } else { st = parseEscapeSequence(gOpts.menufmt) } ui.menuWin.printLine(ui.screen, 0, i, st, line) } if ui.menuSelect != nil { st := parseEscapeSequence(gOpts.menuselectfmt) ui.menuWin.print(ui.screen, ui.menuSelect.x, ui.menuSelect.y, st, ui.menuSelect.s) } } func (ui *ui) dirOfWin(nav *nav, wind int) *dir { wins := len(ui.wins) if gOpts.preview { wins-- } ind := len(nav.dirPaths) - wins + wind if ind < 0 { return nil } return nav.getDir(nav.dirPaths[ind]) } func (ui *ui) draw(nav *nav) { st := tcell.StyleDefault context := dirContext{selections: nav.selections, clipboard: nav.clipboard, tags: nav.tags} ui.screen.Clear() ui.drawPromptLine(nav) wins := len(ui.wins) if gOpts.preview { wins-- } for i := range wins { role := Parent if i == wins-1 { role = Active } if dir := ui.dirOfWin(nav, i); dir != nil { dirStyle := &dirStyle{colors: ui.styles, icons: ui.icons, role: role} ui.wins[i].printDir(ui, dir, &context, dirStyle, nav.previewTimer) } } switch ui.cmdPrefix { case "": if gOpts.rulerfmt == "" { ui.drawRulerFile(nav) } else { ui.drawStat(nav) ui.drawRuler(nav) } ui.screen.HideCursor() case ">": maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end prefix := truncateRight(ui.cmdPrefix, maxWidth) left := truncateLeft(ui.cmdAccLeft, maxWidth-uniseg.StringWidth(prefix)-printLength(ui.msg)) ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+ui.msg) ui.msgWin.print(ui.screen, uniseg.StringWidth(prefix)+printLength(ui.msg), 0, st, left+ui.cmdAccRight) ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+printLength(ui.msg)+uniseg.StringWidth(left), ui.msgWin.y) default: maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end prefix := truncateRight(ui.cmdPrefix, maxWidth) left := truncateLeft(ui.cmdAccLeft, maxWidth-uniseg.StringWidth(prefix)) ui.msgWin.printLine(ui.screen, 0, 0, st, prefix+left+ui.cmdAccRight) ui.screen.ShowCursor(ui.msgWin.x+uniseg.StringWidth(prefix)+uniseg.StringWidth(left), ui.msgWin.y) } ui.drawPreview(nav, &context) if gOpts.drawbox { ui.drawBox() } ui.drawMenu() ui.screen.Show() } func findBinds(keys map[string]expr, prefix string) (binds map[string]expr, ok bool) { binds = make(map[string]expr) for key, expr := range keys { if !strings.HasPrefix(key, prefix) { continue } binds[key] = expr if key == prefix { ok = true } } return } func listBinds(binds map[string]map[string]expr) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) // merge keys by command across modes m := make(map[string]map[string]string) for mode, keys := range binds { for key, expr := range keys { if _, ok := m[key]; !ok { m[key] = make(map[string]string) } m[key][expr.String()] += mode } } type entry struct { mode, key, cmd string } // collect normalized entries var entries []entry for key, cmds := range m { for cmd, modes := range cmds { tmp := []rune(modes) slices.Sort(tmp) entries = append(entries, entry{string(tmp), key, cmd}) } } sort.Slice(entries, func(i, j int) bool { if entries[i].key != entries[j].key { return entries[i].key < entries[j].key } return entries[i].mode < entries[j].mode }) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "mode\tkey\tcommand") for _, e := range entries { fmt.Fprintf(t, "%s\t%s\t%s\n", e.mode, e.key, e.cmd) } t.Flush() return b.String() } func listMatchingBinds(binds map[string]expr, prefix string) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) keys := make([]string, 0, len(binds)) for k := range binds { keys = append(keys, k) } sort.Strings(keys) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "key\tcommand") for _, k := range keys { remain, _ := strings.CutPrefix(k, prefix) fmt.Fprintf(t, "%s\t%v\n", remain, binds[k]) } t.Flush() return b.String() } func listCmds(cmds map[string]expr) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) keys := make([]string, 0, len(cmds)) for k := range cmds { keys = append(keys, k) } sort.Strings(keys) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "name\tcommand") for _, k := range keys { fmt.Fprintf(t, "%s\t%v\n", k, cmds[k]) } t.Flush() return b.String() } func listJumps(jumps []string, ind int) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) maxlength := len(strconv.Itoa(max(ind, len(jumps)-1-ind))) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, " jump\tpath") // print jumps in order of most recent, Vim uses the opposite order for i := len(jumps) - 1; i >= 0; i-- { switch { case i < ind: fmt.Fprintf(t, " %*d\t%s\n", maxlength, ind-i, jumps[i]) case i > ind: fmt.Fprintf(t, " %*d\t%s\n", maxlength, i-ind, jumps[i]) default: fmt.Fprintf(t, "> %*d\t%s\n", maxlength, 0, jumps[i]) } } t.Flush() return b.String() } func listHistory(history []string) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) maxlength := len(strconv.Itoa(len(history))) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "number\tcommand") for i, cmd := range history { fmt.Fprintf(t, "%*d\t%s\n", maxlength, i+1, cmd) } t.Flush() return b.String() } func listMarks(marks map[string]string) string { t := new(tabwriter.Writer) b := new(bytes.Buffer) keys := make([]string, 0, len(marks)) for k := range marks { keys = append(keys, k) } sort.Strings(keys) t.Init(b, 0, gOpts.tabstop, 2, '\t', 0) fmt.Fprintln(t, "mark\tpath") for _, k := range keys { fmt.Fprintf(t, "%s\t%s\n", k, marks[k]) } t.Flush() return b.String() } func listFilesInCurrDir(nav *nav) string { dir := nav.currDir() if dir.loading { log.Printf("listFilesInCurrDir(): %s is still loading, `files` isn't ready for remote query", dir.path) return "" } b := new(strings.Builder) for _, file := range dir.files { fmt.Fprintln(b, file.path) } return b.String() } // readNormalEvent is used to read a normal event on the client side. For keys, // digits are interpreted as command counts but this is only done for digits // preceding any non-digit characters (e.g. "42y2k" as 42 times "y2k"). func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr { draw := &callExpr{"draw", nil, 1} count := 0 keys := gOpts.nkeys if nav.isVisualMode() { keys = gOpts.vkeys } switch tev := ev.(type) { case *tcell.EventKey: if ui.pasteEvent { return nil } isDigitKey := func(*tcell.EventKey) bool { if tev.Key() != tcell.KeyRune || tev.Modifiers() != tcell.ModNone { return false } s := tev.Str() return len(s) == 1 && s[0] >= '0' && s[0] <= '9' } switch { case tev.Key() == tcell.KeyEsc && ui.keyAcc != "": ui.keyAcc = "" ui.keyCount = "" ui.menu = "" return draw case isDigitKey(tev) && ui.keyAcc == "": ui.keyCount += tev.Str() return draw default: ui.keyAcc += readKey(tev) } binds, ok := findBinds(keys, ui.keyAcc) switch len(binds) { case 0: ui.echoerrf("unknown mapping: %s", ui.keyAcc) ui.keyAcc = "" ui.keyCount = "" ui.menu = "" return draw default: if ok { if ui.keyCount != "" { c, err := strconv.Atoi(ui.keyCount) if err != nil { log.Printf("converting command count: %s", err) } count = c } expr := keys[ui.keyAcc] if count != 0 { switch e := expr.(type) { case *callExpr: expr = &callExpr{name: e.name, args: e.args, count: count} case *listExpr: expr = &listExpr{exprs: e.exprs, count: count} } } ui.keyAcc = "" ui.keyCount = "" ui.menu = "" return expr } if gOpts.showbinds { // mode and already typed keys are obvious here; no need to clutter the menu ui.menu = listMatchingBinds(binds, ui.keyAcc) } return draw } case *tcell.EventMouse: if ui.cmdPrefix != "" { return nil } var button string switch tev.Buttons() { case tcell.Button1: button = "" case tcell.Button2: button = "" case tcell.Button3: button = "" case tcell.Button4: button = "" case tcell.Button5: button = "" case tcell.Button6: button = "" case tcell.Button7: button = "" case tcell.Button8: button = "" case tcell.WheelUp: button = "" case tcell.WheelDown: button = "" case tcell.WheelLeft: button = "" case tcell.WheelRight: button = "" case tcell.ButtonNone: return nil } if tev.Modifiers() == tcell.ModCtrl { button = "