Git Product home page Git Product logo

Comments (10)

rafaelrinaldi avatar rafaelrinaldi commented on August 30, 2024

@oranja @edouard-lopez what's the best way to achieve this?

from pure.

oranja avatar oranja commented on August 30, 2024

For bare fish (not using any framework), I believe the most standard way is to rearrange the variable assignments to serve as defaults in case the variable is not yet set.
For example:

# Symbols
set -q symbol_prompt; or set -g symbol_prompt ""
set -q symbol_git_down_arrow; or set -g symbol_git_down_arrow ""
set -q symbol_git_up_arrow; or set -g symbol_git_up_arrow ""
set -q symbol_git_dirty; or set -g symbol_git_dirty "*"
set -q symbol_horizontal_bar; or set -g symbol_horizontal_bar ""

This way, any of these variables can be defined in config.fish without being overwritten by pure's fish_prompt.fish.

If it gets too tedious, you can define a helper function:

function set_default -S -a var value
    set -q $var; or set -g $var $value
end

Then the previous block becomes:

# Symbols
set_default symbol_prompt ""
set_default symbol_git_down_arrow ""
set_default symbol_git_up_arrow ""
set_default symbol_git_dirty "*"
set_default symbol_horizontal_bar ""

In the price of indirection you get a more compact block, and it also helps to avoid bugs from typos...

from pure.

rafaelrinaldi avatar rafaelrinaldi commented on August 30, 2024

@oranja that looks pretty decent and straightforward. Will give it a try! Thanks man.

from pure.

 avatar commented on August 30, 2024

My two cents.

Mostly agree with @oranja, but please always prefix any global variables with the name of your plugin/theme/prompt/snippet/etc so as to not conflict with other global variables of the same name.

I personally recommend against using and or or. Long one-liners are difficult to maintain and modify in the future. But that's just my opinion.

My alternative is this:

set -l symbol_prompt ❯

if set -q pure_symbol_prompt
    set symbol_prompt $pure_symbol_prompt
end

Some may look at that and think "why do I need to create an extra variable and do in 4 LOC what I can do in 1?". The answer, again, has to do with readability and maintainability. Refactoring the code I have proposed is very easy and it's harder to botch causing a bug.

It's like a fortress you are building to protect yourself from your future self.

from pure.

rafaelrinaldi avatar rafaelrinaldi commented on August 30, 2024

Great, I will keep that in mind. Thanks.

Rafael Rinaldi
http://rinaldi.io

On Feb 13, 2016, at 15:27, Jorge Bucaran [email protected] wrote:

Agree with @oranja, but please always prefix any global variables with the name of your plugin/theme/prompt/snippet/etc so as to not conflict (hardly, but still) with other global variables of the same name.


Reply to this email directly or view it on GitHub.

from pure.

edouard-lopez avatar edouard-lopez commented on August 30, 2024

Thanks for your knowledge!

@oranja good proposal
@bucaran nice improvement! Love future-proof argument!
don't we need -g to set globally?

from pure.

oranja avatar oranja commented on August 30, 2024

What @bucaran is suggesting is to have two variables for each customization options.
The first is local and is the one actually used in the theme code.
The second can have any scope that affects the prompt - universal, global, or local to an outer function (i.e. a test function).
To clarify, here's @bucaran's suggestion again, with annotations. (hoping I won't get sued for copyright infringement here).

set -l symbol_prompt ❯    # This is the local variable that will be used in the prompt's code.

if set -q pure_symbol_prompt    # Here we check for any previous definition of `pure_symbol_prompt`. Not necessarily global.
    set symbol_prompt $pure_symbol_prompt    # We override the local variable with the external value, if it's present.
end

from pure.

oranja avatar oranja commented on August 30, 2024

In the same file fish also has several of these set -q var; or set var
one liners, rendering your choice argument weaker.
Having said that, both are fine, but personally, I think that if you have
many of these in a row, it's better to use the shorter form. It's an
equally acceptable fish idiom.

On Tue, Feb 16, 2016, 00:22 Jorge Bucaran [email protected] wrote:

One more thing that I don't know whether it supports my argument or not,
is that fish does this too, so it's a way to stay consistent across the
universe.

https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_config_interactive.fish#L18

don't we need -g to set globally?

Depends. I was assuming this variable will only be used in the file where
it was declared.


Reply to this email directly or view it on GitHub
#2 (comment).

from pure.

Shougo avatar Shougo commented on August 30, 2024

I think this issue should be closed by #30.

from pure.

rafaelrinaldi avatar rafaelrinaldi commented on August 30, 2024

@Shougo That's true. Thanks!

from pure.

Related Issues (20)

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.