Destructuring assignment in JS

Simple ones javascript const [a, b, ...c] = [10, 20, 100,200] // a= 10, b=20, c=[100,200] const { first, second } = { first: 1, second: 2 } // first = 1, second = 2

Crazier one javascript const resp = { data: { message: 'hello', random: 'xxx', test:'hehe' } } const { data, data: { message, ...rest } = resp // data = { message: 'hello' }, message = 'hello' // rest = { random: 'xxx', test: 'hehe' }

Tino

Run rubocop against your current branch

$ git diff develop feature/one-signal --diff-filter=M --name-only -- *.rb | egrep -v 'db|config|spec' | xargs rubocop


2
Ali

Use `host` to get more information about domains

You might be using dig to find out A/CNAME/MX etc records of a domain from DNS servers. But if you just want to get a quick overview, use host:

$ host oozou.com
oozou.com has address 52.89.55.136
oozou.com has address 35.165.206.160
oozou.com has address 34.212.209.65
oozou.com mail is handled by 1 aspmx.l.google.com.
oozou.com mail is handled by 5 alt1.aspmx.l.google.com.
oozou.com mail is handled by 5 alt2.aspmx.l.google.com.
oozou.com mail is handled by 10 aspmx2.googlemail.com.
oozou.com mail is handled by 10 aspmx3.googlemail.com.


2
Const

Attaching file to ActiveStorage without HTTP

If you want to attach a file you generated on disk or downloaded from a user-submitted URL, you can do it like this.

@model.image.attach(
    io: File.open('/path/to/file'),
  filename: 'file.pdf',
  # content type is optional
  content_type: 'application/pdf'
)


2
Tino

Zsh function to list file tree

When navigating through directories on the command line, it’s often helpful to see the structure of directories and subdirectories in a "tree" format. While there are plenty of tools out there, such as tree, sometimes you might want a simple custom solution integrated directly into your shell. In this post, we’ll walk through creating a simple Zsh function to list a directory’s structure in a tree format.

Creating a Zsh Function to List File Trees


If you're using Zsh as your default shell, adding a custom function to your .zshrc file can help you create a quick and efficient command for viewing directories in a tree structure. This function works similarly to the popular tree command but is built using ls and sed, making it easy to integrate directly into your Zsh environment.

Step 1: Add the lst() Function to .zshrc

The first step is to define the lst() function inside your .zshrc file. This function takes an optional argument, which can be any directory path, and lists the directory tree starting from that point. If no directory is specified, it defaults to the current directory.

Open your .zshrc file using your preferred text editor and add the following function:

lst() {
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        ls -R $1 | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    else
        ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    fi
}


Step 2: Reload Your Zsh Configuration

After adding the function, you'll need to reload your .zshrc file for the changes to take effect. You can do this by running the following command in your terminal:

sh
source ~/.zshrc

Step 3: Using the lst() Function

Once you've added the function to your .zshrc, you can start using it right away. The lst() function allows you to list all the subdirectories in a specified directory in a tree format. Here’s how to use it:

To list the subdirectories of a folder (e.g., spec), run:
sh
lst spec

This command will generate a tree-like structure that lists all the subdirectories inside the spec folder.

If you want to see the tree structure of the current directory, simply run:
sh
lst

The lst() function will output the folder structure, making it easier to understand the hierarchy of your directories.

Example Output

Let’s say you have the following directory structure:

bash
spec/
├── controllers/
│   └── users/
├── models/
└── views/
Running the command lst spec would produce output similar to the following:

css
   spec
   |--controllers
   |   |--users
   |--models
   |--views
This simple tree structure makes it easy to visualize the relationships between directories and subdirectories.

How It Works

Let’s break down the function step-by-step:

  1. ls -R $1: The ls -R command recursively lists the contents of the directory (or current directory if no argument is provided).
  2. grep ":$": Filters the results to only show directory names (lines ending with :).
  3. sed Commands:
    • 's/:$//': Removes the colon at the end of each directory name.
    • 's/[^-][^\/]*\//--/g': Replaces forward slashes (/) with dashes (--), converting the structure into a tree-like format.
    • 's/^/ /': Adds indentation to the directory tree.
    • 's/-/|/': Replaces dashes with vertical pipes (|), further enhancing the tree format.

Step 4: Customize the lst() Function

You can easily customize the lst() function to suit your needs. For example, you can change the formatting of the tree structure or add additional options for listing files. If you frequently need to list files as well as directories, you could modify the function to include files by removing the grep filter that only shows directories.

Alternative: Using the tree Command

If you prefer, you can use the tree command, which is designed specifically for displaying directory structures. tree is a widely available utility and can be installed on most Unix-like systems. To install tree, use one of the following commands depending on your system:

On Ubuntu or Debian-based systems:
sh
sudo apt-get install tree

On macOS:
sh
brew install tree

Once installed, you can use the tree command to generate similar output:
sh
tree spec

The tree command provides more features out of the box, such as the ability to display file sizes, permissions, and more. However, if you prefer to keep things simple or if tree is not available, the lst() function is a great lightweight alternative.

Conclusion

The Zsh function we’ve created provides a simple and efficient way to list directory structures in a tree format. By adding the lst() function to your .zshrc file, you can quickly visualize the subdirectories of any folder on your system. This can be incredibly useful for navigating complex directory structures and understanding the hierarchy of files and folders in your projects.

If you're looking for more functionality, consider using the tree command for more advanced features. However, for lightweight, custom solutions integrated directly into your Zsh shell, the lst() function is a perfect choice.

FAQs


1. What is the lst() function used for?

The lst() function is a custom Zsh function that displays the directory structure in a tree format. It uses ls and sed to format the output, making it easy to visualize folder hierarchies directly in the terminal.

2. How do I add the lst() function to my Zsh configuration?

You can add the lst() function by editing your .zshrc file and pasting the function definition into the file. After that, run source ~/.zshrc to reload the configuration and make the function available.

3. Can I customize the output of the lst() function?

Yes, you can customize the function by modifying the sed commands that format the output. You can change the tree structure, add more details, or even include files in the listing.

4. What is the alternative to using the lst() function?

The tree command is a widely available utility that displays directory structures in a tree format. It offers more features than the lst() function and can be installed on most Unix-like systems.

5. Can I use the lst() function on systems other than Zsh?

The lst() function is written for Zsh, but it can be easily adapted for other shells like Bash by adding it to the appropriate configuration file (.bashrc for Bash).

By integrating the lst() function into your Zsh workflow, you can enhance your command-line experience and make directory navigation much easier.

Tino

Change system names on macOS

From term (or equivalent), copy and paste:

To change your public Host Name (i.e., jeff.oozou.com): 
sudo scutil --set HostName "New Name" 

To change your local Host Name (i.e., jeff.local): 
sudo scutil --set LocalHostName "New Name"

To change your Finder Computer Name (i.e., Jeff's MBP): 
sudo scutil --set ComputerName "New Name"

Finally, flush your DNS cache to force refresh: 
dscacheutil -flushcache 

1.07 Thousand
Jeff

Create your own git commands (aliases)

Ever wanted to shorten a git command? You can go with shell alilas for sure but you have to always include context so that you don't forget what each ailas stands for but you can define your own commands in git using aliases.

git config --global alias.  


For example; I'm too lazy to type git status so I want to shorten this to git s

git config --global alias.s status


And here we go now you can just type git s and get the status of current repository.

Ali

Amazon AWS S3 CLI copy with set perms

Open term (or equivalent) and paste the following:

S3 copy

  • to upload a world-readable file:
aws s3 cp source.file s3://bucket/path/dest.file --acl public-read


Options for --acl parameters are:

  • private - visibility to owner only
  • public-read - readable by all
  • public-read-write - readable and writable by all
Jeff

Add new types to Rails 5 attributes API

The Rails 5 attributes API allows us to build form objects very easily.

I had previously been using an after_initialize callback to downcase emails, with the following code I can define an :email date type and values are automatically cast when set.

This has the advantage of casting subsequent calls to set the email value.

class EmailType < ActiveModel::Type::String
  def cast(value)
    return super unless value.is_a?(String)
    super(value.downcase)
  end
end

ActiveModel::Type.register(:email, EmailType)


class Foo
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :email, :email
end


> Foo.new(email: 'EMAIL@MAIL.com').email
=> 'email@mail.com'


1
Joe