Coder Social home page Coder Social logo

chetanxpro / focus Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 1.0 896 KB

A Rust CLI tool to block distracting websites for a specific time and help stay focused.

License: MIT License

Rust 100.00%
cli-tool distraction-blocker focus productivity rust

focus's Introduction

Focus CLI Tool

Focus is a command-line tool crafted to enhance your concentration during tasks by blocking distracting websites. This tool modifies your system's hosts file to restrict access to specified websites for a designated period. Once this period elapses, it automatically unblocks the restricted websites, allowing you to access them freely.

focus.mov

How It Works Under the Hood

System Hosts File

The hosts file is a plain text file on your computer that maps hostnames to IP addresses. It is commonly used to override DNS settings and redirect traffic locally. In Unix-based systems like macOS, the hosts file is located at /etc/hosts. Focus utilizes this file to control access to specific websites.

Blocking Mechanism

When you initiate a task using the sudo focus command, Focus edits the hosts file to redirect all specified distracting websites to the loopback address (127.0.0.1). This effectively blocks access to those websites on your system.

Automatic Unblocking

Once the predefined blocking time elapses, Focus automatically restores the original hosts file, unblocking the specified websites. This seamless process ensures that your access to distracting websites is only restricted during the designated focus period.

Features

  • Setup: Configure the list of distracting websites by running sudo focus setup --list <path to txt file>. Provide a text file containing the URLs you want to block.

  • Blocking Websites: Once set up, use the sudo focus command with the desired time and task parameters to initiate website blocking. For example, sudo focus --time 1h --task study will block distracting websites for one hour during your study session.

  • Safety Command: In case of a sudden system crash, unexpected shutdown, or accidental closure of the terminal, you can use the command sudo focus reset to manually unblock all websites and restore the original hosts file, ensuring uninterrupted access to the web.

Installation

For Unix-Based Systems (Linux and macOS):

To use Focus, follow these installation steps:

  1. Clone the repository to your local machine:
git clone https://github.com/ChetanXpro/focus
  1. Navigate to the project directory:
cd focus
  1. Build the project using Cargo:
 cargo build --release
  1. Move the binary to a directory in your system's PATH (e.g., /usr/local/bin/):
sudo mv target/release/focus /usr/local/bin/

For Windows:

After building project with command cargo build --release Copy the generated binary (focus.exe) from the target\release directory to a directory in your system's PATH.

For example, you can copy it to C:\Windows\System32 or any other directory that is included in the system's PATH.

Now, you can use the focus or focus.exe command to set up and manage website blocking. Remember to run the commands with administrative privileges on Windows.

Usage

Setup

First create a txt file like websites.txt and then copy its absolute path.

sudo focus setup --list /path/to/your/websites.txt

Replace /path/to/your/websites.txt with the absolute path to the text file containing the list of distracting websites. The file should have one website URL per line. For example:

  • my websites.txt:
youtube.com
twitter.com
facebook.com
instagram.com
linkedin.com
reddit.com
snapchat.com
quora.com
medium.com


Blocking Websites

sudo focus --time <duration> --task <task name>

Replace with the desired time for blocking websites (e.g., 1h for one hour, 50s for 50 seconds , 20m for 20 mins) and with a name representing the task you're focusing on.

Note: Running both setup and the main focus command requires the use of sudo as the tool modifies the system's hosts file.

Safety Command

sudo focus reset

Use this command to manually unblock all websites and restore the original hosts file in case of unexpected terminations.

Example

sudo focus setup --list /Users/chetan/Developer/rust/focus/websites.txt
sudo focus --time 45m --task coding

In this example, a list of distracting websites is set up from a file, and website blocking is initiated for 45 minutes during a coding session.

Important

  • Ensure that you have administrative privileges (sudo) to modify the system's hosts file.
  • Use responsibly, as blocking websites affects the entire system.

Troubleshooting Browser Cache Issues

If you find that websites are still accessible even after blocking them in the hosts file, it might be due to browser caching. Here are some solutions to address this issue:

  1. Clear Browser Cache:

    • Manually clear the browser cache. This process varies by browser, so check your browser's settings or history section for the option to clear cached data.
  2. Restart Browser:

    • Restart your browser after making changes to the hosts file. This simple step can help ensure that the changes take effect.
  3. Flush DNS Cache:

    • On Windows, open Command Prompt as Administrator and run ipconfig /flushdns.
    • On macOS/Linux, open Terminal and run sudo dscacheutil -flushcache or sudo systemctl restart nscd.
  4. Use Private Browsing Mode:

    • Access the blocked websites in a private or incognito browsing mode. This mode typically bypasses the cache and reflects the updated hosts file.
  5. System Reboot:

    • Perform a system reboot to ensure that the hosts file changes are fully applied.

Note: Changes to the hosts file may take some time to propagate, and the effectiveness of these solutions may vary based on the browser and operating system.

Stay focused with Focus! ๐Ÿš€

focus's People

Contributors

chetanxpro avatar giovanni-cutri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

giovanni-cutri

focus's Issues

Typo

There's a typo line 149 of main.rs

fs::read_to_string(websites_file_path).expect("Error while reading website contetn");

should read

fs::read_to_string(websites_file_path).expect("Error while reading website content");

(content instead of contetn)

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.