GithubHelp home page GithubHelp logo

mfhepp / cdf Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 82 KB

cdf: Change to favorite directories in Bash

Home Page: https://mfhepp.github.io/cdf/

License: MIT License

Shell 100.00%
bash cd cli linux osx terminal utility

cdf's People

Contributors

mfhepp avatar

Stargazers

 avatar

Watchers

 avatar

cdf's Issues

Feature: Check if a shortcut for this target already exists

In order to avoid redundant bookmarks, it would be good if you check all bookmarks for identical link targets to PWD.
One may then offer to rename the existing shortcut or to abort.

Basically just loop over all links in the shortcuts folder and check if their realpath matches $PWD.

Refactoring: Make code more robust and more portable

The code could be improved to be more consistent and more portable (mostly ChatGPT 4 recommendations):

  1. Unify the use of echo vs. printf.
  2. Quote Variables: Always quote your variables to handle spaces and special characters correctly. For instance, use "$PWD" instead of $PWD etc.
  3. Consistent if Statement Syntax: For portability and readability, use a consistent style for if statements. For example, instead of [[ "$CURRENT_DIR" = "$CDFPATH" || "$CURRENT_DIR" == "$CDFPATH"/* ]], use [ "$CURRENT_DIR" = "$CDFPATH" ] || [ "$CURRENT_DIR" = "$CDFPATH"/* ].
  4. Avoid Using [[ for Portability: The [[ syntax is not POSIX-compliant and might not work in shells other than Bash and Zsh. Use [ or test for portability.
  5. Handle realpath and ln Commands for Portability: The realpath and ln commands might not be available or behave differently on some systems. You can write a function to manually resolve real paths or use readlink as an alternative. Similarly, for ln, check if the command exists and handle errors.
  6. Handle cd Command Failure: The cd command can fail (e.g., due to permission issues). Always check its exit status to ensure the subsequent commands operate in the correct directory.
  7. Avoid Using find for Autocomplete: The find command in _cdf_autocomplete may not be efficient or necessary. Consider using a simpler approach, like a for loop over the contents of $CDFPATH.
  8. Sanitize Input: Ensure that input (like shortcut names) is sanitized to prevent potential command injection or other security issues or conflicts. Maybe simply restrict the format of the shortcut name via regexes.
  9. Check for Command Existence: Before using commands like find, realpath, or ln, check if they exist on the system for better portability.
  10. Test with zsh and other Non-Bash shells.

Update autocomplete if shortcut is being added or removed

Currently, the list of shortcuts for autocompletion is updated only when you either open new terminal or reload the shell profile.

It would be better to update the list of shortcuts when a new entry is added or an existing entry has been removed.

Support for zsh

The code should basically be portable as it stands; the tests run at least with zsh 5.9 on OSX. The only required changes for official support would be

  1. Fixing the autocomplete mechanism, as complete is not available in zshand the function might also not work as it stands. A minimal solution would be no autocomplete in zsh.
  2. Adding .zshrc etc. to the installation script, observing potential differences between OSX and Linux.
  3. Cleaning up .bashrc vs. .bash_profile in the installer - clarifying where to put the functions best.

Useful resources:

Feature: Check if link target is broken

The symbolic links will break if the link target has been deleted or moved. It would be good to

  1. warn prior to cdf NAME if the target does not exist
  2. offer a safe mechanism to delete just the symbolic link or move it to a trash, so that people do not need to operate with unconstrained rm :-(
  3. add a warning to --list
  4. maybe add a mechanism to remove all broken symbolic links (output should be a list so that one can recreate them manually).

Caveats and considerations:

  1. Link targets may only be broken temporarily, e.f. for mounted volumes, network drives, ...
  2. What will be more intuite?
    • cdf --remove foo
    • addfav --remove foo
    • Consolidate all under the cdf command, like
      • cdf foo
      • cdf --add foo
      • cdf --list
      • cdf --remove foo
      • cdf --remove --broken

AFAIK, there is no easy way to fix broken symbolic links automatically, as OSX Aliases would do.

Feature: Automatic installer for `zsh` and support for missing `.bashrc`

It may be useful to add automatic installation for ZSH to the installer.

Blockers:

  1. It is non-trivial to decide for the users as for which shell or shells they want to install it to.
  2. The absence of a .zshrc or .bashrc is no reliable indicator of the proper shell, as it may have to be created.
  3. The usage of the proper one of the several files is a bit complex and may not be platform-independent (e.g. .bashrc vs .bash_profile on OSX).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.