我的 command prompt 設定

2018-01-29 更新

適用於 macOS 10.13.2

來一點嘻嘻哈哈的配色吧!

1
PS1="\[\033[1;31m\]\t\[\033[0m\] \[\033[1;33m\]\u\[\033[0m\] \[\033[1;32m\]\h\[\033[0m\] \[\033[1;34m\]\w\[\033[0m\] ? "

但建議還是把 emoji 拿掉,我在 iTerm2 換行會怪怪的

2015-07-13

因為買了新電腦,想把command prompt改得比較實用一點,發現自己完全忘記怎麼改了,再度驗證了考LPIC是沒有用的XD

Mac的個人command prompt檔案位置在 ~/.bash_profile
如果沒看到的話,自己打開隱藏檔,如果還是沒看到的話就自己nano一個新的

sudo nano ~/.bash_profile

重新讀取 .bash_profile 指令:

source ~/.bash_profile

目前的prompt:

export PS1=”[$(date +%k:%M:%S)] \u@\h \w $”

範例顯示:

[ 1:55:58] [email protected] ~ $

總共有哪些指令要另外爬文找了,參考文獻裡面有

—參考文獻—
[1] How to reload .bash_profile from the command line? – http://stackoverflow.com/questions/4608187/how-to-reload-bash-profile-from-the-command-line
[2] How to suppress (or customize) Mac Terminal shell prompt – http://stackoverflow.com/questions/14416274/how-to-suppress-or-customize-mac-terminal-shell-prompt
[3] Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie – http://www.thegeekstuff.com/2008/09/bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie/
[4] Customize Your Shell & Command Prompt – http://blog.taylormcgann.com/tag/prompt-color/
[5] [Unix] 讓Bash增添色彩 與 多字元換行問題 – http://blog.xuite.net/sphjlc062218/thinking/63628944-%5BUnix%5D+%E8%AE%93Bash%E5%A2%9E%E6%B7%BB%E8%89%B2%E5%BD%A9+%E8%88%87+%E5%A4%9A%E5%AD%97%E5%85%83%E6%8F%9B%E8%A1%8C%E5%95%8F%E9%A1%8C

在Mac上利用 .NET Core SDK 跨平台開發 C Sharp

基本相依套件

先裝好以下套件:

Homebrew(http://brew.sh/index_zh-tw.html)
X Code Command Line Tool(自己去App Store抓)

1. 安裝相依套件

brew update
brew install openssl
ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/

2. 安裝 .NET Core for Mac

下載pkg來安裝:
https://www.microsoft.com/net/download

3. OS X 10.11 會發現不能跑dotnet指令

處理一下預設的路徑即可:

ln -s /usr/local/share/dotnet/dotnet /usr/local/bin

4. 測試

dotnet new
dotnet restore
dotnet run

Hello World 就出現拉~

然後就安裝Visual Studio Code,測試一下那IDE的自動完成功能。

其實VSC很聰明,基於ATOM,裝什麼就去商店抓,相依還會自己檢查,我後來想,會用這個工具主要是Debug,可以設中斷點、看堆疊,不然其實Build在CMD處理即可,PHP這種Debug可以硬幹,直接顯示在網頁上,但是我想現在應該不能這樣了,現在可是後端工程師XD!

::安裝C#套件並啟用

這個微軟的套件其實是 C# for Visual Studio Code (powered by OmniSharp)
http://www.omnisharp.net/

也會有一些相依性的東西在默默安裝

不過目前Debug區出現了一些警告,我還沒研究這是什麼…

—— 參考文獻 ——
[1] .NET – Powerful Open Source Cross Platform Development : https://www.microsoft.com/net/
[2] core/known-issues.md at master · dotnet/core · GitHub – https://github.com/dotnet/core/blob/master/cli/known-issues.md

[golang] Heroku 平台操作練習

從PHP轉戰golang,超級不習慣的,因為之前也沒有MVC的概念,學習之路更是艱辛
於是我出了個題目給自己:

Heroku + Golang + Git command deploy

我,三種全部都不會!!

沒關係,這就是學習的好機會,來吧!

*環境:Mac OS X El Capitan 10.11.6

Step 1. 安裝Go

先把golang環境架起來吧!
到官網下載golang,我習慣自己處理套件,不裝包好的pkg

sudo tar -C /usr/local -xzf go1.7.3.darwin-amd64.tar.gz

那個版本之類的要記得自己調整啊,我打這篇的時候版本是1.7.3
接著是設定環境變數

nano ~/.bash_profile

加入這幾行

#Go Lang
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/Desktop/Trees/Go
export GOBIN=$GOPATH/bin
export PATH=$GOPATH:$GOBIN:$PATH

我解說一下,第一個export把go本身執行檔、編譯之類的弄進環境變數,第二個是GOPATH,是整個Workspace的位置,go跟某些語言一樣有自動套件相依檢查機制,所以整個Workspace就是你的套件庫。第三跟第四個是export我們利用go get從repository抓下來的工具。

然後你就可以挑一個你喜歡的editor來玩囉,我是開始學習使用Visual Studio Code來玩golang拉,那GUI的特性帶來的許多方便,雖然我還是覺得他比Sublime Text 3肥了些。

Step 2. 安裝Heroku

這段就直接安裝Heroku包好的pkg了

https://devcenter.heroku.com/articles/heroku-command-line

然後,別跟我說你沒裝git…

Step 3. 測試Heroku

首先介紹幾個heroku常用指令:

heroku login : 登入heroku
heroku ps : 目前使用中的Process,也會顯示剩餘的dyno hour(heroku的計價單位)
heroku create : 創造空的app,create後面可以帶參數,會成為app的name
heroku logs : 查看log檔

假設我們已經申請好Heroku帳號,下一步就直接登入了:

heroku login

untitled

登入之後,我們可以先從遠端拉個Hello World來玩玩看Heroku的部署,輸入以下指令:

go get github.com/heroku/go-getting-started

你會發現,有一包資料夾叫做go-getting-started,出現在你設定的GOPATH裡的src資料夾,沒錯,你已經學會利用go get抓git上面的repository了!
我們切換到該reposiory中,準備部署至heroku上看看

cd $GOPATH/src/github.com/heroku/go-getting-started
heroku create
git push heroku master

這邊講解一下,我們切換到repository之後在做heroku create的動作,目的是因為heroku他會自動針對此repository,將遠端分支”heroku”設定為heroku即將產生的app git位置,在鍵入heroku create之後,他會給你兩個網址:

獨立網址,例如:https://fierce-beyond-60579.herokuapp.com/
Git Repository,例如:https://git.heroku.com/fierce-beyond-60579.git

獨立網址就是此app的唯一位置,他的git就是你之後deploy的對象。

是的就是這麼簡單,你已經部署了hello world上去heroku囉!…什麼?你說你失敗了?
我猜你一定沒有把SSH KEY給heroku!

ssh-keygen -t rsa
heroku keys:add

是的,跟github一樣,要把ssh加到網站上面才能開始一切操作喔!

Heroku也提供本地測試,只需要輸入:

heroku local web

他自己就會監聽5000 port了!
如果是自製的Project,遇到heroku local不能跑的時候,記得檢查有沒有產生Procfile檔案(請參考heroku文件,他很簡單的就只是告訴heroku你的app在process裡叫做什麼名字),或是檢查你的自製Project有沒有執行過 go install -v

Step 4. 部署自己的App

相信你已經準備好自己的Hello World專案準備弄上這個免費雲端,但是!部署前要先知道遊戲規則,要在Heroku上面部署自己的app的話,你需要裝官方認得的相依性套件管理工具,我們會以godep來做範例。

Godep相當的方便,跟go get差不多,但是它是把所有跟你的專案有關的套件都抓到你的app資料夾裡,可以獨立運作也方便管理,同時也會自動產生一個相依列表”Godeps.json”,Heroku就是以這個檔案來當作識別,抓取你app的相關資料。

安裝指令:

go get github.com/tools/godep

使用方式:

godep save ./

好的,到這裡為止,應該準備的差不多了,不過我這邊要特別註解一下關於git的部分,因為小弟平常用git都是GUI操作,只知道幾個關鍵名詞但是對command line操作git完全沒概念,所以就來記錄一下!

我們先介紹幾個常用的指令:

git init : 初始化你的project,主要是產生 .git 資料夾
git remote add <remote> <url> : 新增遠端git位置
git remote remove <remote> : 移除遠端git位置
git remote -v : 查看遠端詳細位置
git add -A . : 一次新增所有檔案,準備commit
git commit -m <title> -m <message> : commit 加上訊息 git status : 顯示目前repo狀態
git push : PUSH到指定的遠端與分支

我們假設你已經寫好你的Hello World了…什麼?你還沒寫?好吧,我的借你抄一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    http.HandleFunc("/", hello)
    fmt.Println("listening...")
    err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
    if err != nil {
        panic(err)
    }
}

func hello(res http.ResponseWriter, req *http.Request) {
    fmt.Fprintln(res, "hello, world")
}

稍微說一下,一般我們在處理Listen Port時會直接寫上例如:5000這樣,但是在Heroku線上,他只開80 Port給你,並且寫好了環境變數,使用方式就如我的範例那樣,os.Getenv(“PORT”)就可以拿到80這個數字,不包含冒號喔!

跑完godep、跑完git init,準備好Procfile檔案(反正就是在專案根目錄生一個叫做Procfile的檔案,裡面寫著 web: go-getting-started 即可,詳情請看https://devcenter.heroku.com/articles/getting-started-with-go#define-a-procfile),就可以準備部署了,我們可以先檢查一下自己的git remote 分支 heroku 目前被指向誰,輸入 git remote -v:

untitled

請注意看我的heroku目前被指向 https://git.heroku.com/young-ridge-89894.git ,這代表我如果在這個專案進行部署,會被部署到heroku上的young-ridge-89894這支app,如果你確定這是你的目標,那接下來就好處理了,但是如果不是,記得鍵入以下指令:

git remote remove heroku 

並加入遠端目標,例如:

git remote add heroku https://git.heroku.com/polar-sierra-53616.git

如果你是想要部署在新的app上面,只要在remove之後直接heroku create即可唷!
如果你的遠端目前已經被某個別的project部署過,只要在部署時加上 -f 就可以強制部署了!

部屬就跟git command操作一樣,先add、再commit、再push到遠端
範例指令:

git add -A .
git commit -m "Test"
git push heroku master

untitled

登登登登~完成了!

後記

我覺得應該可以開一篇關於go framework的東西XD
在範例專案 github.com/heroku/go-getting-started 中,他是使用了gin-gonic這個framework來開發的網頁,包含了html template的使用方法在裡面,感覺很好玩!

還有在利用Visual Studio Code的時候,發現了Git History這個套件(https://github.com/DonJayamanne/gitHistoryVSCode),他可以直接在Visual Studio Code中看到Git Log的Graph唷!使用方式就是 CMD + shift + P ,選擇 git view history (log) 。

阿對了,如果你是從Sublime Text轉來使用的人,可以打開一個功能,絕對會讓你有回到家的感覺,於選單的「使用者設定」中,插入以下:

"editor.renderIndentGuides": true

瞧瞧他打開了什麼?是垂直整理線!

——參考文獻——
[1] Getting Started on Heroku with Go – https://devcenter.heroku.com/articles/getting-started-with-go
[2] Go Dependencies via Godep – https://devcenter.heroku.com/articles/go-dependencies-via-godep
[3] Heroku Local – https://devcenter.heroku.com/articles/heroku-local
[4] Godep – https://github.com/tools/godep
[5] Managing Your SSH Keys – https://devcenter.heroku.com/articles/keys
[6] Deploying a Golang Project to Heroku – http://www.zhubert.com/blog/2014/03/08/deploying-golang-to-heroku/
[7] Manage Dependencies With GODEP – https://www.goinggo.net/2013/10/manage-dependencies-with-godep.html
[8] Git 教學(1) : Git 的基本使用 – http://gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/
[9] Getting Started with Go on Heroku – https://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html
[10] [NodeJS] 將程式部署到 Heroku 上 – NodeJS on Heroku – http://cire.pixnet.net/blog/post/37369672-%5Bnodejs%5D-%E5%B0%87%E7%A8%8B%E5%BC%8F%E9%83%A8%E7%BD%B2%E5%88%B0-heroku-%E4%B8%8A—nodejs-on–heroku
[11] Gin Gonic – https://gin-gonic.github.io/gin/
[12] How to attach my repo to heroku app – http://stackoverflow.com/questions/6877915/how-to-attach-my-repo-to-heroku-app
[13] Overwriting an existing Heroku app – http://stackoverflow.com/questions/14437903/overwriting-an-existing-heroku-app
[14] 在Heroku部署Rails專案結合AWS S3存放上傳圖片 – http://springok-blog.logdown.com/posts/2015/10/29/heroku-deploy-a-rails-project-with-aws-s3-storage-upload-pictures

讓 WordPress 使用者只看見自己上傳的媒體內容

快把以下這段加到你的 functions.php 裡,立馬見效!

1
2
3
4
5
6
7
8
9
10
11
function restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    if( !is_a( $current_user, 'WP_User') )
    return;
    if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
    return;
    if( !current_user_can('manage_media_library') )
    $wp_query_obj->set('author', $current_user->ID );
    return;
}
add_action('pre_get_posts','restrict_media_library');

macOS Sierra 10.12 使用心得與技巧

2016-09-20,macOS新版本一上架我就立刻下載安裝了,忍受El Capitan的kernel爆炸太久了,就賭這新版本應該會比以前更好用(吧)。

安裝完立刻把Siri關掉,我沒事是不會跟電腦聊天的,接著陸續安裝好基本需求工具(謎之音:X-Force全系列都開不起來,有這方面需求的人要注意),以及分割100GB給Bootcamp,大致上完成了應用方面的軟體安裝。

目前使用上沒有太大問題,但感覺系統還沒有很穩定,像是有一次從休眠模式醒來後,顯示卡延遲了快30秒才能正常顯示顏色,還有就是使用Safari時,如果已經安裝好Chrome,他會詢問是否要從Chrome匯入書籤,我明明就按不要結果他全部都匯入進來了…整個崩潰,現在書籤超亂。開發環境的部分,因為我想把開發環境轉移到虛擬機器上,所以就不針對開發工具做測試了。接下來有幾個小地方想特別介紹一下:

1. 打開「允許任何來源的App安裝」這功能竟然不見了

如果有在 Mac OS X 測試安裝過非官方認證的App,一定看過這個畫面

file

但是在macOS Sierra被拿掉了,沒關係,只要在Terminal輸入以下指令就可以變出這個功能:

sudo spctl --master-disable

2. 新功能「大寫鎖定鍵自動切換輸入法」目前跟Chrome 53.0.2785.116 (64-bit)有衝突

目前的版本macOS Sierra 10.12有個自動切換輸入法的功能,就是當你使用中文輸入法,想輸入英文時,除了直接切換成英文輸入法之外,還可以按下caps lock鍵來暫時輸入英文,在macOS Sierra中,按下caps lock後還可以自動幫你切換成英文輸入法,但是這功能在Chrome中會造成當機,可以暫時先把它關閉:

typer

但我不知道是要等Chorme更新還是Sierra更新就是了XD

3. 「螢幕擷取」終於可以選擇輸出檔案格式

這真是太感動了,直接看圖就知道

window

實在是困擾我超級久,之前每次螢幕截圖都還要轉檔超麻煩的

WordPress 轉址與中文網址在 IIS 上會遇到的問題

以下包含暫時解法,因為是直接動WordPress Kernel

1. WordPress 網址無法轉址(Permalinks)

要修改 web.config 檔

 <?xml version="1.0" encoding="UTF-8"?>

<configuration>

 <system.webServer>

 <rewrite>

 <rules>

 <rule name="wordpress" stopProcessing="true">

 <match url=".*" />

 <conditions logicalGrouping="MatchAll">

 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

 </conditions>

 <action type="Rewrite" url="index.php" />

 </rule>

 </rules>

 </rewrite>

 </system.webServer>

</configuration>

2. WordPress 中文網址無法正常轉址

修改 wp-includes/ 底下的 class-wp.php

$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';

改成

$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? mb_convert_encoding( $_SERVER['PATH_INFO'], 'UTF-8', 'Big5') : '';

然後

list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );

改成

list( $req_uri ) = explode( '?', mb_convert_encoding( $_SERVER['REQUEST_URI'], 'UTF-8', 'Big5' )  );

3. WordPress 中文檔案上傳錯誤
修改 wp-admin/includes/ 底下的 file.php

$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );

改成

$fileTypeNameArr = explode( '.', $filename );
$countNum = count( $fileTypeNameArr ) - 1;
$fileExt = $fileTypeNameArr[$countNum];
$filename = time() . '-' . rand( 0,999999999 ) . '.' . $fileExt;

—-參考文獻—-
[1] Using Permalinks – https://codex.wordpress.org/Using_Permalinks
[2] [WordPress]中文網址,發生404錯誤,教你如何解決? – http://dev.brucelulu.com/topics/180
[3] How to Setup WordPress Permalinks – https://gist.github.com/jonahvsweb/5479580
[4] IIS permalinks compatibility – web.config not working? – http://stackoverflow.com/questions/27988710/iis-permalinks-compatibility-web-config-not-working
[5] 解決 WordPress 在 IIS 中無法使用固定網址的問題 – http://www.minitw.com/archives/server/solved-can-not-be-used-in-iis-wordpress-permalink-issue.htm?variant=zh-tw
[6] WordPress小技巧(1):解決中文檔名圖片無法上傳問題 – http://www.cc.net.tw/2013/07/434/